One-to-many unidirectional 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 java. util. Set;
5 import javax. persistence. Column;
6 import javax. persistence. Entity;
7 import javax. persistence. GeneratedValue;
8 import javax. persistence. Id;
9 import javax. persistence. onetoence;
10 /**
11 *-----------------------------------------
12 * @ describe farm
13 * @ author fancy
14 * @ mailbox fancydeepin@yeah.net
15 * @ 2012-10-21 <p>
16 *-----------------------------------------
17 */
18 @ Entity
19 public class Farm implements Serializable {
20
21 private static final long serialVersionUID = 1L;
22
23 private Integer id;
24 private String name;
25 private Set <Plant> plantSet;
26
27 @ Id
28 @ GeneratedValue
29 public Integer getId (){
30 return id;
31}
32 @ Column (length = 18, nullable = false)
33 public String getName (){
34 return name;
35}
36 @ OneToMany (cascade = CascadeType. ALL, fetch = FetchType. LAZY)
37 public Set <Plant> getPlantSet (){
38 return plantSet;
39}
40 public void setId (Integer id ){
41 this. id = id;
42}
43 public void setName (String name ){
44 this. name = name;
45}
46 public void setPlantSet (Set <Plant> plantSet ){
47 this. plantSet = plantSet;
48}
49}


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 plants
11 * @ author fancy
12 * @ email fancydeepin@yeah.net
13 * @ 2012-10-21 <p>
14 *-----------------------------------------
15 */
16 @ Entity
17 public class Plant 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
40}

Junit test:

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

Execute the unit test above. The table structure diagram generated in the database:

 

From the figure above, we can see that there is an extra middle table, which is redundant and does not need to exist. The reason why it exists is that hibernate by default,

Onetoworkflow is considered as a special case of manytoworkflow, so an intermediate table is generated. To remove this intermediate table, you only need to add the @ JoinColumn annotation:

1 @ OneToMany (cascade = CascadeType. ALL, fetch = FetchType. LAZY)
2 @ JoinColumn (name = "farm_id ")
3 public Set <Plant> getPlantSet (){
4 return plantSet;
5}

Run the unit test again (note that you must manually delete the previously generated table. Because there is an intermediate table, the intermediate table references the primary table associated with it, in this case, hibernate cannot help us Delete the table)

 


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 Plant tomato = new Plant ();
13 tomato. setName ("tomato ");
14 Plant cabbage = new Plant ();
15 cabbage. setName ("cabbage ");
16 Set <Plant> plantSet = new HashSet <Plant> ();
17 plantSet. add (tomato );
18 plantSet. add (cabbage );
19 Farm farm = new Farm ();
20 farm. setName ("fancy-farm ");
21 farm. setPlantSet (plantSet );
22 session. beginTransaction ();
23 session. persist (farm );
24 session. getTransaction (). commit ();
25} catch (Exception e ){
26 e. printStackTrace ();
27}
28}


 

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.