Hibernat之关系的处理一对一处理

第一步:编写两个pojo,比如一个学生表一个班级表  这里使用注解。

需要

公司表:

 1 package com.qcf.pox;
 2 
 3 import javax.persistence.CascadeType;
 4 import javax.persistence.Entity;
 5 import javax.persistence.GeneratedValue;
 6 import javax.persistence.GenerationType;
 7 import javax.persistence.Id;
 8 import javax.persistence.OneToOne;
 9 
10 @Entity
11 public class Company {
12     @Id
13     @GeneratedValue(strategy=GenerationType.AUTO)
14     private int id;
15     private String name;
16     @OneToOne(cascade=CascadeType.ALL)
17     private Address address;
18     public int getId() {
19         return id;
20     }
21     public void setId(int id) {
22         this.id = id;
23     }
24     public String getName() {
25         return name;
26     }
27     public void setName(String name) {
28         this.name = name;
29     }
30     public Address getAddress() {
31         return address;
32     }
33     public void setAddress(Address address) {
34         this.address = address;
35     }
36     public Company(int id, String name, Address address) {
37         super();
38         this.id = id;
39         this.name = name;
40         this.address = address;
41     }
42     public Company() {
43         super();
44     }
45     
46 }
View Code

 

地址表:

 1 package com.qcf.pox;
 2 
 3 import javax.persistence.CascadeType;
 4 import javax.persistence.Entity;
 5 import javax.persistence.GeneratedValue;
 6 import javax.persistence.GenerationType;
 7 import javax.persistence.Id;
 8 import javax.persistence.OneToOne;
 9 
10 @Entity
11 public class Address {
12     @Id
13     @GeneratedValue(strategy=GenerationType.AUTO)
14     private int id;
15     private String name;
16     @OneToOne(cascade=CascadeType.ALL,mappedBy="address")
17     private Company company;
18     public int getId() {
19         return id;
20     }
21     public void setId(int id) {
22         this.id = id;
23     }
24     public String getName() {
25         return name;
26     }
27     public void setName(String name) {
28         this.name = name;
29     }
30     public Company getCompany() {
31         return company;
32     }
33     public void setCompany(Company company) {
34         this.company = company;
35     }
36     public Address(int id, String name, Company company) {
37         super();
38         this.id = id;
39         this.name = name;
40         this.company = company;
41     }
42     public Address() {
43         super();
44     }
45     
46 }
View Code

 

第二步:在hibernate.cfg.xml文件中引入这两个po类

 

1         <mapping class="com.qcf.pox.Address"/>
2         <mapping class="com.qcf.pox.Company"/>
View Code

 

第三步:编写测试代码

 

 1 package com.qcf.test;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.Transaction;
 6 import org.hibernate.cfg.AnnotationConfiguration;
 7 import org.hibernate.cfg.Configuration;
 8 
 9 import com.qcf.pox.Address;
10 import com.qcf.pox.Company;
11 
12 public class TestOneToOne {
13     public static void main(String[] args) {
14         Configuration configuration=new AnnotationConfiguration().configure();
15         SessionFactory factory=configuration.buildSessionFactory();
16         Session session=factory.openSession();
17         Transaction transaction=session.beginTransaction();
18         Address address=new Address();
19         address.setName("beijing");
20         Company company=new Company();
21         address.setCompany(company);
22         company.setName("zhandaf");
23         company.setAddress(address);
24         session.save(address);
25         session.save(company);
26         transaction.commit();
27         session.close();
28         
29     }
30 
31 }
View Code

 

posted @ 2014-11-20 18:57  老瞿  阅读(210)  评论(0编辑  收藏  举报