-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Problems with updating map
PostPosted: Thu Feb 05, 2004 8:38 am 
Newbie

Joined: Thu Feb 05, 2004 7:21 am
Posts: 2
Hi,

I've modelled a map containing user objects as value and their id's as map keys in a class named Project. The mapping therefore in class Project looks like this (no bidirectional association):

<map name="assignedUsers"
table="R_PROJECT_USER"
lazy="false"
sort="unsorted"
inverse="false"
cascade="none">
<key column="PROJECT_ID"/>
<index column="ASSIGNEDUSERSKEY"
type="string"/>
<many-to-many class="pacakge.User"
column="USER_ID"
outer-join="auto"/>
</map>

Searching in the forum and the FAQs, I found no similar problem. The case is when I add a new user object to the map and update the project, it works fine and the new row is inserted in the R_PROJECT_USER table. Removing an user element from the map in the project and updating the project doesn't works. No error appears and the project will be updated, but the row representing the removed user element will not be deleted from the table. Enabling showing SQL, generates an insert statements by updating the project with a new added user element in the map, but doesn't generates an delete statement when removing a user and updating the project. Deleting the project the whole project, deletes all to the deleted project referring rows in the R_PROJECT_USER table. Reading out the map by loading a project works also fine.

Please help, I don't have an explanation nor a solution for this behavior / problem.

Thanks,

Poelki


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 8:58 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
can you show a simplified code.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Simplified sample code
PostPosted: Thu Feb 05, 2004 9:33 am 
Newbie

Joined: Thu Feb 05, 2004 7:21 am
Posts: 2
Here are simplified code snippets of the project class, the projectHandler and for inserting and removing a user with updating project:

Class Project which contains the map:

public class Project {
private Map assignedUsers = new HashMap();
...
public Map getAssignedUsers() {
return assignedUsers;
}

private void setAssignedUsers(Map assignedUsers) {
this.assignedUsers = assignedUsers;
}

public Object addUserToAssignedUsers(User user) {
return assignedUsers.put(user.getId(), user);
}

public Object removeUserFromAssignedUsers(User user) {
return assignedUsers.remove(user);
}
...
}

ProjectHandler which encapsulates persistence methods for project:

public class RDBProjectPersistenceHandler {
...
public Project updateProject(Project project) throws Exception {
try {
session = getSessionFactory().openSession();
transaction = session.beginTransaction();

session.update(project);
session.flush();

transaction.commit();

} catch (HibernateException he) {
...
}

return project;
}
...
private SessionFactory getSessionFactory() {
SessionFactory sessionFactory = null;
Configuration configuration = null;

try {
configuration = new Configuration().addClass(Project.class);
configuration.setProperty("hibernate.default_schema", "SA");
configuration.setProperty("hibernate.show_sql", "false");
sessionFactory = configuration.buildSessionFactory();
} catch (HibernateException he) {
...
}

return sessionFactory;
}
}

Inserting a user into map in project and updating project:

project.addUserToAssignedUsers(user); // add user to map in project

try {
updatedProject = projectHandler.updateProject(project); // updates project and inserts new user in table R_PROJECT_USER, works fine
} catch ...

Removing a user from map in project and updating project:

project.removeUserFromAssignedUsers(user); // removes user from map in project

try {
updatedProject = projectHandler.updateProject(project); // updates project and should remove user from table R_PROJECT_USER, but doesn't works
} catch ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 12:49 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Ahem
As per the Java API specification
Code:
remove(Object key)
          Removes the mapping for this key from this map if present.



This is not
Code:
remove(Object value)
          Removes the mapping for this object from this map if present.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.