Experience 8-hibernate Advanced Mapping

Source: Internet
Author: User

when making a persistent class, the rules:

1). There is a default method of construction;

2). All attributes have setter and Getter methods

3). There is an object identifier OID;

4. If you have a collection property, you must define it as an interface type: List, Set, Map. cannot be defined as class type HashSet, HashMap, ArrayList, etc.

1. Hibernate Advanced Mapping

<set> elements: You can map the properties of the Java.util.Set interface, and the elements are not sequential and do not allow duplicates .

<list> elements: You can map the properties of the Java.util.List interface, in order , to save the location of each element with an extra index in the table corresponding to the collection property.

<bag> <idbag> Elements : You can map the properties of the Java.util.Collection interface, the elements can be duplicated, but the order is not saved .

<map> elements: You can map the properties of the Java.util.Map interface, and the elements are stored in the form of key/value pairs, and are unordered .

<primitive-array> <array>: You can map array elements.

2. The main code for these advanced mappings

<set> Elements

Private set<string> Hobbies;

<set name= "Hobbies" table= "Student_hobby" >

<key column= "student_id"/>

<element type= "string" column= "Hobby_name" not-null= "true"/>

</Set>

Notice here that the set tag is different from the set tag in the relationship map described earlier, and I don't know if you noticed, there's a more element tag but no label for the relationship type; attribute not-null= of Element label True ' is not allowed field to null meaning

<list> Elements

Private list<string> Hobbies;

<list name= "Hobbies" table= "Student_hobby" >

<key column= "student_id"/>

<list-index column= "Posistion"/>

<element type= "string" column= "Hobby_name" not-null= "true"/>

</List>

here to note that:the list label and set label mapping file is the same, in fact, not the same, there is a point of difference is that the list label more List-index tag, the role of this tag is to add an index to the table, why there is more than one of this label it. is actually determined by the difference between the list and set, Li. St is in order, so there is an index position to uniquely identify, and set does not.

<bag> Elements

Private collection<string> Hobbies;

<bag name= "Hobbies" table= "Student_hobby" >

<key column= "student_id"/>

<element type= "string" column= "Hobby_name" not-null= "true"/>

</bag>

Note here:the function of the bag tag is to achieve the same function as the list label, but not redundant to produce an index location identification. The attribute type in the persisted class for the bag label is the collection collection type, which is the parent interface of the set and list, which can be achieved by the former. The main function of the bag is to make the data repeatable, but not to the field where the identity is generated.

<map> Elements

Private Map<long string> Hobbies;

<map name= "Hobbies" table= "Student_hobby" >

<key column= "student_id" >

<map-key column= "hobby_id" type= "Long"/>

<element type= "string" column= "Hobby_name" not-null= "true"/>

</map>

3. Case study

These are the parts of each xxx.hbm.xml code, but also the core code, the following list of test code.

Settest.java

Package com.hbsi.set;

Import Java.util.HashSet;

Import Java.util.Set;

Import org.hibernate.Session;

Import Org.junit.Test;

Import Com.hbsi.utils.HibernateSessionFactory;

public class Settest {

@Test

public void Add () {

Session session = Hibernatesessionfactory.getsession ();

Session.begintransaction ();

Person p = new person ();

P.setname ("Keven");

Set<string> Hobbies = new hashset<string> ();

Hobbies.add ("Sing");

Hobbies.add ("Dance");

P.sethobbies (hobbies);

Session.save (P);

Session.gettransaction (). commit ();

Hibernatesessionfactory.closesession ();

}

}

Listtest.java

Package com.hbsi.list;

Import java.util.ArrayList;

Import java.util.List;

Import org.hibernate.Session;

Import Org.junit.Test;

Import Com.hbsi.utils.HibernateSessionFactory;

public class Listtest {

@Test

public void Add () {

Session session = Hibernatesessionfactory.getsession ();

Session.begintransaction ();

Person p = new person ();

P.setname ("join");

List<string> Hobbies = new arraylist<string> ();

Hobbies.add ("Backetball");

Hobbies.add ("football");

P.sethobbies (hobbies);

Session.save (P);

Session.gettransaction (). commit ();

Hibernatesessionfactory.closesession ();

}

}

Bagtest.java

Package com.hbsi.bag;

Import java.util.ArrayList;

Import java.util.List;

Import org.hibernate.Session;

Import Org.junit.Test;

Import Com.hbsi.utils.HibernateSessionFactory;

public class Bagtest {

@Test

public void Add () {

Session session = Hibernatesessionfactory.getsession ();

Session.begintransaction ();

Person p = new person ();

P.setname ("Keven");

List<string> Hobbies = new arraylist<string> ();

Hobbies.add ("Sing");

Hobbies.add ("Dance");

P.sethobbies (hobbies);

Session.save (P);

Session.gettransaction (). commit ();

Hibernatesessionfactory.closesession ();

}

}

Maptest.java

Package com.hbsi.map;

Import Java.util.HashMap;

Import Java.util.Map;

Import org.hibernate.Session;

Import Org.junit.Test;

Import Com.hbsi.utils.HibernateSessionFactory;

public class Maptest {

@Test

public void Add () {

Session session = Hibernatesessionfactory.getsession ();

Session.begintransaction ();

Person p = new person ();

P.setname ("Keven");

Map<integer,string> Hobbies = new hashmap<integer,string> ();

Hobbies.put (1, "sing");

Hobbies.put (2, "Dance");

P.sethobbies (hobbies);

Session.save (P);

Session.gettransaction (). commit ();

Hibernatesessionfactory.closesession ();

}

}

Related Article

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.