Hiberante学习10---基于Annotation配置的一对一双向外键关联
来源:互联网 发布:苹果cms h站 编辑:程序博客网 时间:2024/05/18 01:11
在单向外键关联中,我们是在student类中保存idcard的引用,这里在双向外键关联中是互相包含对方的引用,但这样子会造成对象不能保存,
所以要设置如下属性:
@OneToOne(mappedBy=”stu”)//设置将控制权交给哪一方?
双向关联,必须设置mappedBy属性,因为双向关联只能交给一方去控制,不可能在双方都设置外键保存关联关系,否则双方都无法保存。
步骤:
1、pojo类,这里我们将控制权交给Idcard类
package com.demo.pojo.sxwj;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToOne;@Entitypublic class IdCard {private String pid;private String province;private Students stu;@OneToOne(cascade=CascadeType.ALL)@JoinColumn(name="sid")public Students getStu() {return stu;}public void setStu(Students stu) {this.stu = stu;}//不指定主键生成策略时,默认为GeneratedValue.AUTO@Idpublic 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;}}
package com.demo.pojo.sxwj;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.OneToOne;@Entitypublic class Students {private int sid;private String sname;private IdCard cardId;@Idpublic 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(mappedBy="stu")public IdCard getCardId() {return cardId;}public void setCardId(IdCard cardId) {this.cardId = cardId;}}
2、配置文件没有变
3、测试类
package com.demo.pojo.sxwj;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 TestStudentByAnno extends TestCase{private SessionFactory sf;public void testSave(){sf = new AnnotationConfiguration().configure().buildSessionFactory();Session s = sf.getCurrentSession();Transaction tx = s.beginTransaction();tx.begin();Students student = new Students();student.setSname("zhangsan");IdCard idCard = new IdCard();idCard.setPid("11111111");idCard.setProvince("fujian");idCard.setStu(student);s.save(student);s.save(idCard);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()....");}}
- Hiberante学习10---基于Annotation配置的一对一双向外键关联
- hibernate Annotation一对一双向外键关联
- Hiberante学习08---基于Annotation配置的一对一单向外键关联
- Hibernate学习——(六)基于Annotation的一对一双向外键映射
- hibernate学习:一对一双向外键关联
- 037:hibernate一对一双向外键关联(Annotation)
- 【hibernate框架】一对一双向外键关联(Annotation实现)
- Hibernate学习笔记_07_一对一双向外键关联
- hibernate一对一双向外键关联
- Hibernate_一对一双向外键关联
- hibernate一对一双向外键关联_Annotation
- hibernate一对一双向外键关联_XML
- hibernate一对一双向外键关联
- hibernate一对一双向外键关联
- Hibernate:一对一双向外键关联
- hibernate annotation 之 一对多、多对一双向外键关联
- Hibernate关系映射(二)一对一双向外键关联@OneToOne Annotation方式
- Hibernate关系映射(2)_一对一双向外键关联
- bash命令
- 遇到难题(bug)的解决方法心得
- google Calendar Api 的使用方法
- Hibernate中多属性构成复合主键映射
- CI登陆限制的session
- Hiberante学习10---基于Annotation配置的一对一双向外键关联
- 2013年9月4日第一篇CSDN个人博客
- jbjlkb hjchgcyjhcycuktvbklb;kbho
- Qt由ui文件生成.h和.cpp文件的方法
- Java信息安全加密系统环境搭建
- MySQL分区之RANGE分区
- Sql Server 函数的操作实例!(返回一条Select语句查询后的临时表)
- poj3176 Cow Bowling DP
- 异步信号安全和线程安全