📄 30f17aaeae14001c14248ef356693e05
字号:
package edu.neu.sspp.hibernate;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Transaction;
public class Test {
public TAdminDAO adminDAO = new TAdminDAO();
public TTeacherDAO teacherDAO = new TTeacherDAO();
public TCommentDAO commentDAO = new TCommentDAO();
public TProjectDAO projectDAO = new TProjectDAO();
public TUserDAO userDAO = new TUserDAO();
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t = new Test();
t.testFind();
t.testDelete();
t.testOneToMany();
// t.testCascade();
}
public void insertAdmin() {
Transaction transaction =
HibernateSessionFactory.getSession().beginTransaction();
TAdmin admin = new TAdmin();
admin.setAdminName("wangzhong");
admin.setPassword("00000");
adminDAO.save(admin);
transaction.commit();
}
public void insertTeacher() {
Transaction transaction =
HibernateSessionFactory.getSession().beginTransaction();
TTeacher teacher = new TTeacher();
teacher.setName("shenzhenzhong");
teacher.setPassword("11111");
teacher.setEmail("shenzhenzhong@sspp.edu");
teacher.setRealName("szz");
teacherDAO.save(teacher);
transaction.commit();
}
public void insertUser() {
Transaction transaction =
HibernateSessionFactory.getSession().beginTransaction();
TUser user = new TUser();
user.setName("mashanhu");
user.setPassword("22222");
user.setNick("hu");
user.setEmail("mashanhu@sspp.edu");
transaction.commit();
}
public void testFind() {
List teachers = teacherDAO.findByName("mashanhu");
TTeacher teacher = (TTeacher)teachers.get(0);
System.out.println(teacher.getRealName());
}
public void testDelete() {
Transaction transaction =
HibernateSessionFactory.getSession().beginTransaction();
List tonny = teacherDAO.findByName("tonny");
for(int i = 0; i < tonny.size(); i++) {
teacherDAO.delete((TTeacher)tonny.get(i));
}
transaction.commit();
}
public void testOneToMany() {
Transaction transaction =
HibernateSessionFactory.getSession().beginTransaction();
TUser user = new TUser();
user.setClass_("class 1");
user.setCount(0);
user.setEmail("@");
user.setIsOpen(new Byte((byte)0));
user.setName("laolang");
user.setNick("wangyang");
user.setPassword("123455");
user.setSex("m");
user.setTel("2423424");
user.setUserName("user");
userDAO.save(user);
TProject project = new TProject();
project.setName("project");
project.setIntro("i'm a project!");
project.setUrl("null");
project.setSrcUrl("null");
project.setLimits(new Integer(0));
project.setTUser(user);
projectDAO.save(project);
TComment comment1 = new TComment();
comment1.setContent("comment1!");
comment1.setDate(new Date());
comment1.setIsUser(new Byte((byte)0));
comment1.setNick("unknow");
comment1.setTProject(project);
commentDAO.save(comment1);
TComment comment2 = new TComment();
comment2.setContent("comment2!");
comment2.setDate(new Date());
comment2.setIsUser(new Byte((byte)1));
comment2.setNick("sskk");
comment2.setTProject(project);
commentDAO.save(comment2);
transaction.commit();
project = (TProject)projectDAO.findByName("project").get(0);
Set<TComment> comments = project.getTComments();
Iterator<TComment> commIterator = comments.iterator();
while(commIterator.hasNext()) {
System.out.println(commIterator.next().getNick());
}
}
public void testCascade() {
Transaction transaction =
HibernateSessionFactory.getSession().beginTransaction();
TProject project =
(TProject)projectDAO.findByName("project").get(0);
projectDAO.delete(project);
transaction.commit();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -