-->
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.  [ 6 posts ] 
Author Message
 Post subject: a different object with the same identifier ISSUE
PostPosted: Thu Mar 19, 2009 3:29 pm 
Newbie

Joined: Thu Mar 19, 2009 2:38 pm
Posts: 3
Hi all,
I want to integrate NHibernate Mapping object with asp.net MemberShip User object. Here is my case:
Project class has 2 properties: Creator and Participant which is User class.
Project class is mapping by NHibernate, but User is created by MemberShip function. I know User object is not persistent.
When i create a Project object with Creator and Participant, which are the same User. I encounter a error "a different object with the same identifier value was already associated with the session".
I use a class ProjectDB to Add New Project:
public class ProjectDB : BaseDB<Project, Guid>, IProjectDB
{
public Project AddProject(Project project)
{
project = this.Save(project);
this.CommitChanges();
return project;
}
}
Do you have any idea to solve this problem.
Any help much appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 7:14 am 
Regular
Regular

Joined: Wed Feb 11, 2009 10:58 am
Posts: 55
sounds like you do something like:

User Creator = new User();
User.Id = 1;
//Fill in User Data here

User Participant = new User();
User.Id = 1;
//Fill in User Data here

Project p = new Project();
p.Creator = Creator;
p.Participant = Participant;
//Fill in Project Data here

I think that can't work because you've got two different objects with the same key in one Session.
You can overwrite the Equals() function. Now NHibernate should know that both Users are the same (Object.Equals() is only true if it is the same Object).

Maybe like this:

class User
{
...
public override Equals(object o)
{
if(o.GetType() != typeof(User))
return false;

User other = (User)o;
if( other.Id == this.Id )
return true;
else
return false;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 20, 2009 8:24 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have a look here for more info about object identity with hibernate:

http://nhforge.org/blogs/nhibernate/archive/2008/09/06/identity-field-equality-and-hash-code.aspx

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 21, 2009 4:50 am 
Newbie

Joined: Thu Mar 19, 2009 2:38 pm
Posts: 3
Thank you Reflection and Wolli. I have try implement User for Equals, = operator, and GetHashCode but i get the same error.
Are you sure that resolve my problem?
My best.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 23, 2009 3:55 am 
Regular
Regular

Joined: Wed Feb 11, 2009 10:58 am
Posts: 55
well if you're users are not persisted something like:

Code:
Project p = new Project();
p.Creator = Creator;
p.Participant = Participant;


won't work anyway. How should NHibernate know what to do with those objects.
Maybe you could do something like:

Code:
Project p = new Project();
p.Creator = Creator.UserName;
p.Participant = Participant.UserName;


Where the properties of your Project aren't MembershipUsers but plain Strings containing the Username.

Maybe the mapping files and the class definitions of your Project would help.

Greetings,
Reflection


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 24, 2009 6:56 am 
Newbie

Joined: Thu Mar 19, 2009 2:38 pm
Posts: 3
It work, Reflection.
Here my Project define:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="smartSTATUS.DTO.Project, smartSTATUS.DTO" table="smartSTATUS_Projects" lazy="false">
<id name="ProjectId" column="ProjectId" type="Guid">
<generator class="guid" />
</id>
<many-to-one cascade="none" name="Creator" class="smartSTATUS.DTO.User, smartSTATUS.DTO" column="CreatorId" foreign-key="UserId" />
<property name="Name" type="string"/>
<property name="Description" type="string"/>
<property name="Status" type="smartSTATUS.DTO.ProjectStatus, smartSTATUS.DTO" />
<property name="CreationDate" type="DateTime"/>
<bag name="Clients" table="smartSTATUS_ClientsInProjects" cascade="all">
<key column="ProjectId" />
<many-to-many column="ClientId" foreign-key="UserId" class="smartSTATUS.DTO.User, smartSTATUS.DTO" />
</bag>
<bag name="Participants" table="smartSTATUS_ParticipantsInProjects" cascade="all">
<key column="ProjectId" />
<many-to-many column="ParticipantId" foreign-key="UserId" class="smartSTATUS.DTO.User, smartSTATUS.DTO" />
</bag>
</class>
</hibernate-mapping>

I have resolved my problem.
The fault is i don't close old session and re-use that session for saving project.
NHibernate doesn't allow to save Project with Creator and Participant, which have the same identifier, in this case. I don't know why. If anyone can explain me, much appreciated.
However, i want to tell many thanks with you, who help me with nice enthusiasm.
My very best.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.