首页 > 代码库 > hibernate双向一对一
hibernate双向一对一
hibernate双向一对一
package com.bjsxt.hibernate.oneToOne; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import com.bjsxt.hibernate.oneToOne.PersonInfo; @Entity public class Person { private int id; private String name; private String password; private PersonInfo personInfo; @Id @GeneratedValue public int getId() { return id; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", password=" + password + ", PersonInfo=" + personInfo + "]"; } public Person(int id, String name, String password, com.bjsxt.hibernate.oneToOne.PersonInfo personInfo) { super(); this.id = id; this.name = name; this.password = password; this.personInfo = personInfo; } public Person( String name, String password, com.bjsxt.hibernate.oneToOne.PersonInfo personInfo) { super(); this.name = name; this.password = password; this.personInfo = personInfo; } public void setId(int id) { this.id = id; } public Person() { super(); // TODO Auto-generated constructor stub } public String getName() { return name; } @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER) @JoinColumn(name="personInfo") public PersonInfo getPersonInfo() { return personInfo; } public void setPersonInfo(PersonInfo personInfo) { this.personInfo = personInfo; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.bjsxt.hibernate.oneToOne; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class PersonInfo { private int id; private String name; private int age; private Person person; @OneToOne(mappedBy="personInfo",cascade=CascadeType.ALL) public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public PersonInfo( String name, int age) { super(); this.name = name; this.age = age; } public PersonInfo(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public PersonInfo() { super(); // TODO Auto-generated constructor stub } public int getAge() { return age; } @Id @GeneratedValue public int getId() { return id; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } @Override public String toString() { return "PersonInfo [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
package com.bjsxt.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.BeforeClass; import com.bjsxt.hibernate.oneToOne.Person; import com.bjsxt.hibernate.oneToOne.PersonInfo; public class OneToOneTest { private static SessionFactory sessionFactory; @BeforeClass public static void beforeClass() { new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } public static void main(String[] args) { //AddPerson(); //getPerson(); //delPerson(); updatePerson(); } public static void getPerson(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); Person person=(Person) session.get(Person.class, 1); System.out.println(person.toString()); session.getTransaction().commit(); } public static void delPerson(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); Person person=(Person) session.get(Person.class, 1); session.delete(person); System.out.println(person.toString()); session.getTransaction().commit(); } public static void updatePerson(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); Person person=(Person) session.get(Person.class, 2); //先get数据,不写session.update(person)数据库也更新了记录 person.setName("王五"); //级联的数据直接修改,也不用update,hibernate自动完成修改 PersonInfo personInfo=person.getPersonInfo(); personInfo.setName("王五"); System.out.println(person.toString()); session.getTransaction().commit(); } public static void AddPerson(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); PersonInfo personInfo=new PersonInfo( "张三", 23); Person person=new Person("张三", "2", personInfo); personInfo.setPerson(person); //session.save(personInfo); session.save(person); session.getTransaction().commit(); } public static void delPersonByPersonInfo(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); Person person=(Person) session.get(Person.class, 2); session.delete(person.getPersonInfo()); System.out.println(person.toString()); session.getTransaction().commit(); } public static void AddPersonByPersonInfo(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); PersonInfo personInfo=new PersonInfo( "张三", 23); Person person=new Person("张三", "2", personInfo); personInfo.setPerson(person); session.save(personInfo); //session.save(person); session.getTransaction().commit(); } }
本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1876719
hibernate双向一对一
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。