Skip to content

SET Collection properties

Sluo edited this page May 6, 2014 · 1 revision

Set properties with other Entity

First create bean

Create bean Event

   public class Event {}

Create bean Person

   public class Person{ set events = new HashSet();//Entity Event Set }

Set the .hbm.xml

   <set name="events" table="person_event">
    <key column="personId"/>
    <many-to-many column="eventid" class='xxx.Event'/>
   </set>

Desc

We can use Hibernate session tranction to manager the person's set (update), We can add records in DB table `person_event' during the session tranction.

..... beginTranction();
.... person.getEvents().add(event);
....session.getTranction().commit()

then will insert new records in DB talbe person_event Or, we can use two session tranction.

... begingTranction();
.... session.load(person);
...commit()

... beginTranction();
person.getEvents().add(event2);
....commit()

Set properties with base type String

   public class Person { Set<String> emails = new HashSet();}

Create the table email , primary key personID,mailAddr

{
personID,
mailAddr
}

add the hbm.xml setting for the base set

<set name="emails" table="email">
<key column="personID"/>
<element column="maildAddr" type="string"/>
</set>

table :the table to store the email, key the pk key of the table , element indetify the set element use which column and the type , type use the hibernate lowcase string not String, then we can use session to update DB table email records

...person
...person.getEmails().add("emalAddr");
...sesseion.getTranction().commit();

session will update DB table auto

Clone this wiki locally