Folgende Situation,
ich habe die Klasse GroupObject und AuthorisationObject wobei zwischen den beiden eine n:m Beziehung besteht. Das ganze sieht so aus:
Code:
public class GroupObject extends HibernateDatabaseObject {
/**
*
*/
private static final long serialVersionUID = -7225994169601497795L;
private int gid;
private String name;
private boolean locked;
private Collection<HibernateDatabaseObject> authorisation;
public GroupObject() {
super();
this.locked = false;
this.authorisation = new HashSet<HibernateDatabaseObject>();
}
// die getter & setter spar ich mir mal :)
}
und
Code:
public class AuthorisationObject extends HibernateDatabaseObject {
public static final String LOGIN_ALLOWED = AuthorisationObject.class.getSimpleName()+".LOGIN_ALLOWED";
public static final String ADMIN_ALLOWED = AuthorisationObject.class.getSimpleName()+".ADMIN_ALLOWED";
private static final long serialVersionUID = 7935713947537379167L;
private int auid;
private String name;
public AuthorisationObject() {
this(null);
}
public AuthorisationObject(String name) {
super();
this.name = name;
}
// getter & setter
}
Die zugehoerigen Hibernate-Dateien sind:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="moe.web.tavern.map.GroupObject" table="GroupObject">
<id name="gid" type="int" column="GID" >
<generator class="increment"/>
</id>
<property name="name" not-null="true">
<column name="NAME" />
</property>
<property name="locked" type="java.lang.Boolean" not-null="true">
<column name="LOCKED" />
</property>
<set name="authorisation" table="GroupAuthorisationJoin" inverse="true">
<key column="KEY_GROUP"/>
<many-to-many column="KEY_AUTHORISATION" class="moe.web.tavern.map.AuthorisationObject"/>
</set>
</class>
</hibernate-mapping>
und
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="moe.web.tavern.map.AuthorisationObject" table="AuthorisationObject">
<id name="auid" type="int" column="AUID" >
<generator class="increment"/>
</id>
<property name="name" not-null="true">
<column name="NAME" />
</property>
</class>
</hibernate-mapping>
Das Gruppen-Objekt haelt also Referenzen auf die Authoritaeten, wenn ich das aber versuche zu speicher wird zwar das GroupObject und das AuthorisationObject gespeichert, die Tabelle GroupAuthorisationJoin gleibt aber leer
Beim speichern kommen folgende Funktionen zum Einsatz:
Code:
public static void store(SessionFactory factory,
HibernateDatabaseObject storeable) throws Throwable {
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
if (storeable.getCollections() != null)
DbUtils.saveList(storeable.getCollections(), session);
session.save(storeable);
transaction.commit();
session.close();
}
private static void saveList(Collection<HibernateDatabaseObject> list,
Session session) throws Throwable {
for (HibernateDatabaseObject object : list) {
session.save(object);
}
}
und als Beispiel:
Code:
AuthorisationObject ao = new AuthorisationObject(AuthorisationObject.LOGIN_ALLOWED);
DbUtils.store(this.factory, ao);
AuthorisationObject hdo = (AuthorisationObject)DbUtils.load(this.factory, AuthorisationObject.class, ao.getAuid());
GroupObject go = new GroupObject();
go.setName("GROUP.LoginMembers");
go.setLocked(true);
go.addAuthorisation(hdo);
DbUtils.store(this.factory, go);
Nur will das nicht so funktionieren wie es soll.
Hibernate kennt die Datentypen auch, Mapping files sind korrekt angegeben. Fehlermeldungen bzw. Warnmeldungen krieg ich auch keine (log4j ist konfiguriert).
Hat vielleicht jemand den klareren Durchblick als ich in der Sache?
mfg
nul
Lesezeichen