本教程每节课都附带源码,强烈大家建议配合源码学习。
本节源码:http://download.csdn.net/detail/e421083458/5253687
在开始新课之前先讲一下:创建测试专用Source Folder进行测试。
1.新建Source Folder名称为testHibernateQLTest.java package com.bjsxt.hiberante; import java.util.Date; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.Session; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.bjsxt.hibernate.Category; import com.bjsxt.hibernate.Gender; import com.bjsxt.hibernate.Student; import com.bjsxt.hibernate.StudentPK; import com.bjsxt.hibernate.TTeacher; import com.bjsxt.hibernate.TTeacherPK; import com.bjsxt.hibernate.Teacher; public class HibernateQLTest { private static SessionFactory sf; @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("BeforeClass"); sf = new AnnotationConfiguration().configure().buildSessionFactory(); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("AfterClass"); sf.close(); } }
package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Category { private int id; private String name; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
@Test public void testSave(){ Session session = sf.openSession(); session.beginTransaction(); for(int i=0;i<10;i++){ Category c = new Category(); c.setName("c"+i); session.save(c); } session.getTransaction().commit(); session.close(); System.out.println("yes!!!"); }
package com.bjsxt.hibernate; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name = "_teacher") public class Teacher { private String id; private String name; private String title; private String yourWifeName; private Date birthDate; @Id @GeneratedValue(generator="teacherUUID") @GenericGenerator(name="teacherUUID", strategy="uuid") public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getYourWifeName() { return yourWifeName; } public void setYourWifeName(String yourWifeName) { this.yourWifeName = yourWifeName; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } }
@Test public void testTeacher(){ Session session = sf.openSession(); session.beginTransaction(); Teacher t = new Teacher(); t.setName("t1"); t.setTitle("middle"); t.setYourWifeName("up"); t.setBirthDate(new Date()); session.save(t); session.getTransaction().commit(); session.close(); }
package com.bjsxt.hibernate; public class Student { private StudentPK pk; private String sex; private boolean good; public StudentPK getPk() { return pk; } public void setPk(StudentPK pk) { this.pk = pk; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public boolean isGood() { return good; } public void setGood(boolean good) { this.good = good; } }
package com.bjsxt.hibernate; import java.io.Serializable; public class StudentPK implements Serializable { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o){ if(o instanceof StudentPK){ StudentPK pk = (StudentPK) o; if(this.id == pk.getId()&&this.name.equals(pk.getName())){ return true; } } return false; } @Override public int hashCode(){ return this.name.hashCode(); } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bjsxt.hibernate.Student" table="__student"> <composite-id name="pk" class="com.bjsxt.hibernate.StudentPK"> <key-property name="id"></key-property> <key-property name="name"></key-property> </composite-id> <property name="sex" /> <property name="good" type="yes_no"></property> </class> </hibernate-mapping>
@Test public void testStudent(){ Session session = sf.openSession(); session.beginTransaction(); StudentPK pk = new StudentPK(); pk.setId(1); pk.setName("zhangsan"); Student t = new Student(); t.setPk(pk); t.setSex("man"); t.setGood(false); session.save(t); session.getTransaction().commit(); session.close(); }
package com.bjsxt.hibernate; import java.util.Date; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.Id; @Entity public class TTeacher { private TTeacherPK ttpk; private String title; private String yourWifeName; private Date birthDate; private boolean good=true; private Gender gender; @Id public TTeacherPK getTtpk() { return ttpk; } public void setTtpk(TTeacherPK ttpk) { this.ttpk = ttpk; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getYourWifeName() { return yourWifeName; } public void setYourWifeName(String yourWifeName) { this.yourWifeName = yourWifeName; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public boolean isGood() { return good; } public void setGood(boolean good) { this.good = good; } @Enumerated(EnumType.STRING) public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } }
package com.bjsxt.hibernate; import java.io.Serializable; import javax.persistence.Embeddable; @Embeddable public class TTeacherPK implements Serializable { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o){ if(o instanceof StudentPK){ StudentPK pk = (StudentPK) o; if(this.id == pk.getId()&&this.name.equals(pk.getName())){ return true; } } return false; } @Override public int hashCode(){ return this.name.hashCode(); } }
@Test public void testTTeacher(){ Session session = sf.openSession(); session.beginTransaction(); TTeacherPK ttpk = new TTeacherPK(); ttpk.setId(1); ttpk.setName("name"); TTeacher tt = new TTeacher(); tt.setTtpk(ttpk); tt.setBirthDate(new Date()); tt.setGender(Gender.MALE); tt.setGood(true); tt.setTitle("title"); tt.setYourWifeName("wife"); session.save(tt); session.getTransaction().commit(); session.close(); }