Multiple-to-one foreign key Association in hibernate annotation

Source: Internet
Author: User

Assume that a farm produces multiple plants. A specific plant is produced on a farm.

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 * @ describe farm
11 * @ author fancy
12 * @ email 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 * @ describe plants
14 * @ author fancy
15 * @ email 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 test:

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

Run the unit test above. The structure of the table generated in the database is as follows:

 

Cascade storage:

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 ("tomato ");
16 tomato. setFarm (farm );
17 Plant cabbage = new Plant ();
18 cabbage. setName ("cabbage ");
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}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.