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. |