注意,要在外键字段上加
@OneToOne(cascade=CascadeType.ALL) @JoinColumn(name=”pid”,unique=”true”)
保存时先保存外键对象,再保存主对象
前提是加入相应的jar包,主要是多了一个3.8版本的junit.jar..
步聚如下:
1、pojo类,Students.java
package com.demo.pojo.dxwj; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; @Entity public class Students { private int sid; private String sname; private IdCard cardId; @Id @GeneratedValue(strategy=GenerationType.AUTO) public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } @OneToOne(cascade=CascadeType.ALL) @JoinColumn(name="pid",unique=true) public IdCard getCardId() { return cardId; } public void setCardId(IdCard cardId) { this.cardId = cardId; } }
Idcard.java类
package com.demo.pojo.dxwj; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class IdCard { private String pid; private String province; @Id public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } }
2、Hibernate配置文件,这里注意的是有用到getCurrentSession和SchemaExport,所以需要在配置文件中加入相应的配置,保存时先保存外键对象,再保存主对象
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name="mysql"> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/test</property> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- sessionFactory.getCurrentSession用到的配置文件 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 有四个值create,create-drop,update,validate --> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> <mapping class="com.demo.pojo.dxwj.Students"/> <mapping class="com.demo.pojo.dxwj.IdCard"/> </session-factory> </hibernate-configuration>
3、测试类,先执行testSchemaExport测试方法,以生成数据库中的表,然后在执行testSave测试方法,以插入数据库
package com.demo.pojo.dxwj; import junit.framework.TestCase; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class TestStudent extends TestCase{ private SessionFactory sf; public void testSave() { sf = new AnnotationConfiguration().configure().buildSessionFactory(); Session s = sf.getCurrentSession(); Transaction tx = s.beginTransaction(); tx.begin(); IdCard idCard = new IdCard(); idCard.setPid("11111111"); idCard.setProvince("fujian"); Students student = new Students(); //因为是主键类型是Auto的,所以不用setId.. student.setSname("zhangsan"); student.setCardId(idCard); //先保存外键所在的对象 s.save(idCard); //再保存主键所在的对象 s.save(student); tx.commit(); } public void testSchemaExport() { SchemaExport se = new SchemaExport(new AnnotationConfiguration().configure()); se.create(true, true); } protected void setUp() throws Exception { System.out.println("setUp()...."); } protected void tearDown() throws Exception { System.out.println("tearDown()...."); } }