Set Set mapping
Set set mapping is simple, I feel a bit like a one-to-many relationship (only a single PO's a-to-many relationship), because it can be a table and two hibernate necessary configuration, a run class on it.
The code is as follows (for example, people address)
First write the JavaBean model class (person)
Package one;
Import Java.util.Set;
public class Person {
private int id;
private String name;
Private set<string> address; Here are the collection properties to be mapped by hibernnate, and there are about seven types of Hibernate collection properties: 1,list 2,set 3,map 4,array (for Mapping Array collection properties) 5, Primitive-array (used to map an array of base data types) 6,bag (used to map unordered collections) 7,idbag (used to map unordered collections, but to enhance logical order for collections)
Get,set method omitted
}
Write the configuration file again
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<class name= "person" table= "person" >
<id name= "id" column= "id" >
<generator class= "native"/>
</id>
<property name= "name" column= "name" ></property>
<set name= "Address" >
<key column= "address_id" ></key> key corresponding to the primary key in the database
<element column= "Addressname" type= "string" ></element> element here corresponds to a field in the database
</set>
</class>
Finally finish running the class can be
Package one;
Import Java.util.HashSet;
Import Java.util.Set;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Import org.hibernate.classic.Session;
Import Org.junit.Test;
public class App {
Static sessionfactory SF;
static{
Sf=new Configuration (). Configure (). Buildsessionfactory ();
}
@Test
public void Test_add_onetomany () {
Session session = Sf.opensession ();
Transaction tx = Session.begintransaction ();
Person p = new person ();
P.setname ("Mr. Lin");
set<string> address = new hashset<string> ();
Address.add ("Dongguan");
Address.add ("Humen");
P.setaddress (address);
Session.save (P);
Tx.commit ();
Session.close ();
}
}
Show effect
Hibernate collection Mapping--->set mapping ">
Show foreign key relationships
Hibernate collection Mapping--->set mapping ">