Mappings between Hibernate4.x and hibernate4.x

Source: Internet
Author: User

Mappings between Hibernate4.x and hibernate4.x

The relationship between n-n is divided into the following two types:

 

1. Unidirectional n-n association:

  • The join table must be used for the n-n association.
  • Similar to 1-n ing, the key sub-element must be added to the set element, and the foreign key of the CATEGORIES_ITEMS table referenced in the CATEGORIES table is CATEGORIY_ID. Different from 1-n Association ing, when a n-n association is established, the elements in the Set use counter-to-minus. The class attribute of the feature-to-consume sub-element specifies that the Item object is saved in items inoculation, and the column attribute specifies that the foreign key of the ITEMS table in the CATEGORIES_ITEMS table is ITEM_ID.

  

------------------------------ Code --------------------------------------------------------

Category. hbm. xml

1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <! -- Generated 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 --> 5 

Item. hbm. xml

 1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 --> 5 

 

2. Bidirectional n-n association

  • Two-way n-n association requires both ends to use the set attribute
  • Two-way n-n join must use a connection table
  • The key child element should be added to the set attribute to map the foreign key column, and the corresponding-to-minus child element should be added to the set element to associate the object class.
  • You must specify the table name and the column name of the foreign key column of the connected table on both sides of the bidirectional n-n association. The values of the table element in the set element must be specified and must be the same. The column attribute must be specified for two sub-elements of the set element: key and pair-to-pair, key and keep-to-join define the foreign key column names of the persistence class and associated class in the connection table respectively. Therefore, the column attributes of the keys and keep-to-join classes on both sides are the same. That is to say, if the column value of the key of one set element is a, and the column value of the key of the set element of the other side is B, the column value of replicate-to-sequence is.
  • For bidirectional n-n associations, the inverse of one end must be set to true. Otherwise, the two ends maintain the association relationship, which may cause a conflict.

 

Category. hbm. xml

1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <! -- Generated 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 --> 5 

 

Item. hbm. xml

 1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 --> 5 

 

Test class:

1 package com. yl. hibernate. n2n; 2 3 4 import java. util. set; 5 6 import org. hibernate. session; 7 import org. hibernate. sessionFactory; 8 import org. hibernate. transaction; 9 import org. hibernate. cfg. configuration; 10 import org. hibernate. service. serviceRegistry; 11 import org. hibernate. service. serviceRegistryBuilder; 12 import org. junit. after; 13 import org. junit. before; 14 import org. junit. test; 15 16 public class HibernateTest {17 18 private SessionFactory sessionFactory; 19 private Session session; 20 private Transaction transaction; 21 22 @ Before23 public void init () {24 Configuration configuration Configuration = new Configuration (). configure (); 25 ServiceRegistry serviceRegistry = 26 new ServiceRegistryBuilder (). applySettings (configuration. getProperties () 27. buildServiceRegistry (); 28 29 sessionFactory = configuration. buildSessionFactory (serviceRegistry); 30 31 session = sessionFactory. openSession (); 32 33 transaction = session. beginTransaction (); 34} 35 @ After36 public void destory () {37 transaction. commit (); 38 39 session. close (); 40 41 sessionFactory. close (); 42} 43 44 @ Test45 public void testSave () {46 Category category1 = new Category (); 47 category1.setName ("C-AA "); 48 49 Category category2 = new Category (); 50 category2.setName ("C-BB"); 51 52 53 Item item1 = new Item (); 54 item1.setName ("I-AA "); 55 56 Item item2 = new Item (); 57 item2.setName ("I-BB"); 58 59 // sets the correlation between 60 category1.getItems (). add (item1); 61 category1.getItems (). add (item2); 62 63 category2.getItems (). add (item1); 64 category2.getItems (). add (item2); 65 66 item1.getCategories (). add (category1); 67 item1.getCategories (). add (category2); 68 69 item2.getCategories (). add (category1); 70 item2.getCategories (). add (category2); 71 72 // execute the save operation 73 session. save (category1); 74 session. save (category2); 75 76 session. save (item1); 77 session. save (item2); 78} 79 80 81 @ Test82 public void testGet () {83 Category category = (Category) session. get (Category. class, 1); 84 System. out. println (category. getName (); 85 // you need to connect to the 86 Set <Item> items = category in the intermediate table. getItems (); 87 System. out. println (items. size (); 88 89} 90 91}

 

  

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.