5.6.4 ing set attributes: List set attributes

Source: Internet
Author: User

Hibernate. cfg. xml:

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->

Person. Java:

public class Person {private int id;private String name;private int age;private List<String> schools=new ArrayList<String>();public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public List<String> getSchools() {return schools;}public void setSchools(List<String> schools) {this.schools = schools;}}

Person. HBM. xml:

<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

Test. Java:

Public class test {public static void main (string [] ARGs) {person p1 = new person (); p1.setname ("Tom"); p1.setage (20 ); list <string> schools1 = new arraylist <string> (); schools1.add (""); schools1.add (""); schools1.add (""); p1.setschools (schools1 ); person P2 = new person (); p2.setname ("Jack"); p2.setage (23); List <string> schools2 = new arraylist <string> (); schools2.add (""); schools2.add (""); schools2.add (""); schools2.add (""); p2.setschools (schools2); Session session = hibernatesessionfactory. getsession (); transaction TXT = session. begintransaction (); Session. save (P1); Session. save (P2); TXT. commit (); hibernatesessionfactory. closesession ();}}

After running test. Java, view the database:

For the same Persistent object, the index of the collection elements contained in it will not be repeated. Therefore, the list set attribute can use the foreign key associated with the persistent object and the SET index columnJoint primary key. See.

For list set attributes, use<List.../>Element ing, which has three child elements:

Sub-element of <list.../>
Name Description
<Key.../> The value of the set attribute cannot be stored in the same table as that of the persistence class,The Set attributes are stored in another table,
Therefore, you must add the <key.../> element to map the foreign key column.
<List-index.../> Maps the index columns of the list set.
<Element.../> This element is used when the set element is of the basic type, its packaging class, string type, and date type.

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.