Hallo,
ich habe eine Tabelle "User" und eine Tabelle "Group". Mehrere Benutzer können der gleichen Gruppe angehören und andererseits kann ein Benutzer in mehreren Gruppen angemeldet sein. Spricht man dann von einer many-to-many-Beziehung?
Da ich davon ausgehe, dass dem so ist, habe ich dementsprechende Mapping-Dateien angelegt.
Danach musste ich nun ja noch die Klassen mit gettern und settern anlegen.
Aber nachdem ich dies getan habe und nun dem Set eines Users eine Gruppe hinzufüge und das User-Objekt dann mittels getHibernateTemplate().saveOrUpdate(myUser); speichere, entsteht KEIN neuer Eintrag inmeiner "UserGroup"-Tabelle.
Und das ist mein Problem! Ich kann zwar Benutzer und Gruppen anlegen, aber keine Beziehungen zwischen ihnen.
Stimmt etwas mit den gettern und settern nicht? Fehlt da vielleicht was in der Klasse "UserGroup"?
Es folgen die Mapping-Dateien ....
Für den User:
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 package="de.tfhberlin.eclipsophone.server.beans" auto-import="true">
<class name="User" table="Users">
<id name="id" column="id" type="long" unsaved-value="0">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="password" column="password" type="string"/>
<set name="subscribed_groups" outer-join="true" table="UserGroups" cascade="all">
<key column="user_ID"/>
<many-to-many class="Group" column="group_ID"/>
</set>
</class>
</hibernate-mapping>
Für Group:
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 package="de.tfhberlin.eclipsophone.server.beans" auto-import="true">
<class name="Group" table="Groups">
<id name="id" column="id" type="long" unsaved-value="0">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="description" column="description" type="string"/>
<set name="participating_users" outer-join="true" inverse="true" table="UserGroups">
<key column="group_ID"/>
<many-to-many class="User" column="user_ID"/>
</set>
</class>
</hibernate-mapping>
Für UserGroup:
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 package="de.tfhberlin.eclipsophone.server.beans" auto-import="true">
<class name="UserGroup" table="UserGroups">
<id name="id" column="id" type="long" unsaved-value="0">
<generator class="native"/>
</id>
<property name="user_ID" column="user_ID" type="long"/>
<property name="group_ID" column="group_ID" type="long"/>
</class>
</hibernate-mapping>
Und die Klassen:
User:
Code:
package de.tfhberlin.eclipsophone.server.beans;
import java.util.Iterator;
import java.util.Set;
public class User
{
private String name;
private String password;
private long id;
private Set<Group> subscribed_groups = new HashSet<Group>();
public User()
{
}
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public Set<Group> getSubscribed_groups() {
return subscribed_groups;
}
public void setSubscribed_groups(Set<Group> subscribed_groups) {
this.subscribed_groups = subscribed_groups;
}
// Helper method which maintains bi-directional relationship
public void addSubscribed_group(Group subscribed_group) {
this.subscribed_groups.add(subscribed_group);
subscribed_group.addParticipating_user(this);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("User {id=" + id + ", name=" + name + ", groups={");
for (Iterator<Group> i = subscribed_groups.iterator(); i.hasNext(); )
sb.append("(" + i.next() + "), ");
if (subscribed_groups.size() > 0)
sb.delete(sb.length()-2, sb.length());
sb.append("}}");
return sb.toString();
}
}
Group:
Code:
package de.tfhberlin.eclipsophone.server.beans;
import java.util.Set;
public class Group
{
private long id;
private String name;
private String description;
private Set<User> participating_users = new HashSet<User>();
public Group()
{
}
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public Set<User> getParticipating_users() {
return participating_users;
}
public void setParticipating_users(Set<User> participating_users) {
this.participating_users = participating_users;
}
// Helper method which maintains bi-directional relationship
public void addParticipating_user(User participating_user) {
this.participating_users.add(participating_user);
participating_user.addSubscribed_group(this);
}
public String toString()
{
return "Group {id=" + id + ", name=" + name + ", description=" + description + "}";
}
}
UserGroup:
Code:
package de.tfhberlin.eclipsophone.server.beans;
public class UserGroup
{
private long user_ID;
private long group_ID;
private long id;
public UserGroup()
{
}
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public long getUser_ID()
{
return user_ID;
}
public void setUser_ID(long user_ID)
{
this.user_ID = user_ID;
}
public long getGroup_ID()
{
return group_ID;
}
public void setGroup_ID(long group_ID)
{
this.group_ID = group_ID;
}
public String toString()
{
return "UserGroup {id=" + id + ", userid=" + user_ID + ", groupid=" + group_ID + "}";
}
}