当前位置:编程学习 > JAVA >>

hibernate annotation 之 多对一单向外键关联

假设,一个农场产出多种植物,具体的某一植物产于某一农场。

 1 package net.yeah.fancydeepin.po;
 2
 3 import java.io.Serializable;
 4 import javax.persistence.Column;
 5 import javax.persistence.Entity;
 6 import javax.persistence.GeneratedValue;
 7 import javax.persistence.Id;
 8 /**
 9  * -----------------------------------------
10  * @描述  农场
11  * @作者  fancy
12  * @邮箱  fancydeepin@yeah.net
13  * @日期  2012-10-21 <p>
14  * -----------------------------------------
15  */
16 @Entity
17 public class Farm implements Serializable{
18
19     private static final long serialVersionUID = 1L;
20    
21     private Integer id;
22     private String  name;
23    
24     @Id
25     @GeneratedValue
26     public Integer getId() {
27         return id;
28     }
29     @Column(length = 18, nullable = false)
30     public String getName() {
31         return name;
32     }
33     public void setId(Integer id) {
34         this.id = id;
35     }
36     public void setName(String name) {
37         this.name = name;
38     }
39 }


 1 package net.yeah.fancydeepin.po;
 2
 3 import java.io.Serializable;
 4 import javax.persistence.CascadeType;
 5 import javax.persistence.Column;
 6 import javax.persistence.Entity;
 7 import javax.persistence.FetchType;
 8 import javax.persistence.GeneratedValue;
 9 import javax.persistence.Id;
10 import javax.persistence.ManyToOne;
11 /**
12  * -----------------------------------------
13  * @描述  植物
14  * @作者  fancy
15  * @邮箱  fancydeepin@yeah.net
16  * @日期  2012-10-21 <p>
17  * -----------------------------------------
18  */
19 @Entity
20 public class Plant implements Serializable{
21
22     private static final long serialVersionUID = 1L;
23    
24     private Integer id;
25     private String    name;
26     private Farm     farm;
27    
28     @Id
29     @GeneratedValue
30     public Integer getId() {
31         return id;
32     }
33     @Column(length = 18, nullable = false)
34     public String getName() {
35         return name;
36     }
37     @ManyToOne(cascade = CascadeType.PERSIST, optional = false, fetch = FetchType.LAZY)
38     public Farm getFarm() {
39         return farm;
40     }
41     public void setId(Integer id) {
42         this.id = id;
43     }
44     public void setName(String name) {
45         this.name = name;
46     }
47     public void setFarm(Farm farm) {
48         this.farm = farm;
49     }
50
51 }

Junit 测试 :

1 @Test
2     public void createTable(){
3        
4         new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
5     }

执行上面的单元测试,数据库中生成的表的结构图 :

 

级联保存 :

 1     private static Session session;
 2    
 3     @BeforeClass
 4     public static void beforeClass(){
 5        
 6         session = new AnnotationConfiguration().configure().buildSessionFactory().getCurrentSession();
 7     }
 8    
 9     @Test
10     public void insert(){
11         try {
12             Farm farm = new Farm();
13             farm.setName("fancy-farm");
14             Plant tomato  = new Plant();
15             tomato.setName("番茄");
16             tomato.setFarm(farm);
17             Plant cabbage = new Plant();
18             cabbage.setName("卷心菜");
19             cabbage.setFarm(farm);
20             session.beginTransaction();
21             session.persist(tomato);
22             session.persist(cabbage);
23             session.getTransaction().commit();
24         } catch (Exception e) {
25             e.printStackTrace();
26         }
27     }


 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,