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(); } }
时间: 2024-10-18 21:45:47