本教程每节课都附带源码,强烈大家建议配合源码学习。
本节源码:http://download.csdn.net/detail/e421083458/5253707
Annotation: @0ne20ne(mappedBy=”…”)
规律:凡是双向关联,必设mappedBypackage com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; @Entity public class Husband { private int id; private String name; private Wife wife; @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; } @OneToOne @JoinColumn(name="wifeId") public Wife getWife() { return wife; } public void setWife(Wife wife) { this.wife = wife; } }
package com.bjsxt.hibernate; public class Student { private int id; private String name; private int age; private String sex; private boolean good; private StuIdCard stuIdCard; public StuIdCard getStuIdCard() { return stuIdCard; } public void setStuIdCard(StuIdCard stuIdCard) { this.stuIdCard = stuIdCard; } 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; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } 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; public class StuIdCard { private int id; private String num; private Student student; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }
package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class Wife { private int id; private String name; private Husband husband; @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; } @OneToOne(mappedBy="wife") public Husband getHusband() { return husband; } public void setHusband(Husband husband) { this.husband = husband; } }
<?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="C_student" dynamic-update="true"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="age" /> <property name="sex" /> <property name="good" type="yes_no"></property> <one-to-one name="stuIdCard" property-ref="student"></one-to-one> </class> </hibernate-mapping>
<?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.StuIdCard"> <id name="id"> <generator class="native"></generator> </id> <property name="num"/> <many-to-one name="student" column="studentId" unique="true"></many-to-one> </class> </hibernate-mapping>
package com.bjsxt.hiberante; import java.util.Date; import org.hibernate.Query; 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.Husband; import com.bjsxt.hibernate.StuIdCard; import com.bjsxt.hibernate.Student; import com.bjsxt.hibernate.Wife; 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(); } // @Test public void testSchemaExport(){ new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); } @Test public void testOneTOOne_Wife(){ Session session = sf.getCurrentSession(); session.beginTransaction(); Wife wife = new Wife(); wife.setName("wife"); Husband husband = new Husband(); husband.setName("husband"); husband.setWife(wife); session.save(wife); //先保存妻子 session.save(husband); //再保存丈夫 session.getTransaction().commit(); } @Test public void testOneToOne_Stu(){ Session session = sf.getCurrentSession(); session.beginTransaction(); Student stu = new Student(); stu.setName("student"); stu.setAge(20); stu.setGood(false); stu.setSex("man"); StuIdCard idcard = new StuIdCard(); idcard.setStudent(stu); idcard.setNum("什马卡?"); session.save(stu); session.save(idcard); session.getTransaction().commit(); } public static void main(String args[]){ sf = new AnnotationConfiguration().configure().buildSessionFactory(); Session session = sf.getCurrentSession(); session.beginTransaction(); Student stu = new Student(); stu.setName("student"); stu.setAge(20); stu.setGood(false); stu.setSex("man"); StuIdCard idcard = new StuIdCard(); idcard.setStudent(stu); idcard.setNum("什马卡?"); session.save(stu); session.save(idcard); session.getTransaction().commit(); sf.close(); } }