no use not composite id's
the JasBean is the parent, the KeyValueTuple the child:
but i said something wrong, i call JasBean::set() two times (see source), and in the end 1 JasBean is persistent and 3 KeyValueTuples
that means everytime, when i update the JasBean, all KeyValueTuples related to the JasBean are save with new id's in the database
(add first - 1 is saved
add second 2 are saved - at all 3 are there
)
(i just put in the relations, id and the hybernate methods)
JasBean:
Code:
...
private Set keyValueTuples = new HashSet();
private long uniqueSequence;
...
/**@hibernate.id generator-class="native" type="long"
*/
private Long getUniqueSequence() {
return uniqueSequence;
}
private void setUniqueSequence(Long puniqueSequence) {
uniqueSequence = puniqueSequence;
}
...
public void set(String key, String value) {
try {
KeyValueTuple keyValueTuple = new KeyValueTuple();
keyValueTuple.setKey(key);
keyValueTuple.setValue((String) value);
if (keyValueTuples.isEmpty()) {
save(this);
}
addKeyValueTuple(keyValueTuple);
update(this);
} catch (Exception e) {
}
}
//RELATIONS
/**
*
* @hibernate.set role="KeyValueTuples"
* lazy="false"
* cascade="all"
* inverse=true"
* @hibernate.collection-key column="fkJasBean"
* @hibernate.collection-one-to-many class="org.weta.jas.hibernate.KeyValueTuple"
*/
public Set getKeyValueTuples() {
return keyValueTuples;
}
public void setKeyValueTuples(Set pset) {
keyValueTuples = pset;
}
public void addKeyValueTuple(KeyValueTuple pkeyValueTuple) {
pkeyValueTuple.setJasBean(this);
keyValueTuples.add(pkeyValueTuple);
}
KeyValueTuple:
Code:
...
private long uniqueSequence;
/**@hibernate.id generator-class="native" type="long" */
public long getUniqueSequence() {
return uniqueSequence;
}
public void setUniqueSequence(long puniqueSequence) {
uniqueSequence = puniqueSequence;
}
...
//RELATIONS
/**
* @hibernate.many-to-one class="org.weta.jas.hibernate.JasBeanHiber"
* cascade="all"
* column="fkJasBean"
* not-null="true"
*/
public JasBeanHiber getJasBean() {
return jasBean;
}
public void setJasBean(JasBeanHiber pjasBean) {
jasBean = pjasBean;
}
The hibernate methods :
Code:
public void save(Object object) throws Exception {
openSession();
session.save(object);
closeSession();
}
public void update(Object object) throws Exception {
openSession();
session.update(object);
closeSession();
}
private void openSession() throws Exception {
sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
}
private void closeSession() throws Exception {
session.close();
}
bye