views:

502

answers:

2

hello good fellas! here i am again in my self learning hibernate and personal experiment project to gain more understanding. Here is the description of my environment: i have a super model entity that all my model inherit from.it has only id property i use the genericDAO pattern found on hibernate web site. Now my problem is that i use list instead of set for my one to many mapping (there is no need to avoid duplication here) and when i reference the super entity id in list index or list-index property i have this error : org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session here is the super entity interface

public interface Model extends Serializable {
public Long getId();
public void setId(Long id);
}

//here is its implementation.they are not in the same physical file
public abstract class ModelImpl implements Model {
private Long id;
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}

}

here is it's mapping file

<class abstract="true" name="ModelImpl">
<!--i removed doctype and stuffs-->
<id name="id">
  <generator class="assigned"/>
</id>
</class>

here is on pojo interface this is my parent

public interface Message extends Model {
Date getDateSent();
String getGlobalStatus();

} //its implementation is here but on different physical file public class MessageImpl extends ModelImpl implements Message{ private String globalStatus; private List response = new ArrayList(); private Date dateSent; //setters and getters.... }

its mapping file is like so :

<union-subclass extends="ModelImpl" name="MessageImpl">
<property name="globalStatus"/>
<property name="dateSent" type="timestamp"/>

-->

i commented the set because its was given a casting error which makes me realize my error so the Response class which is the parent is here :

public interface Response extends Model {
String getGatewayMessageId();
Message getMessageId();
}
//its implementation but in different physical file
public class ResponseImpl extends ModelImpl implements Response{

private static final long serialVersionUID = 1L;

private Message messageId;
private String gatewayMessageId;
//setters and getters..
}

so that is basically that.now during my test when i try to save message with its children it throws this :

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session

i can do all persitent methods exception the one i just talked about. i'm a bit lost here coz all of them are inheriting Model id so what am i doing wrong? thanks for reading.I dont' really understand the problem here.

A: 

Sounds like you're trying to save an entity with an ID that already exists. You realize that as you're using the union-subclass approach, all subclasses that you may have share the same ID space, do you?

Buy the way, do you have a reason to manually assign IDs, or would generated IDs help?

Henning
yes it does actually.its a requirement of what i'm trying to do.you made me discover that it matters to give the same id to different pojo objects.Now my test run fine individually.but when building it fails for the findbyexample method of the ResponseDAOImpl (like i said runs fine when you run the ResponseDAOImplTest class alone).the reason being :org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of model.ResponseImpl.messageId. i must admit i'ma bit lost
black sensei
That just literally means your setter method threw an IllegalArgumentException. You should be able to see why in your own code there.
Henning
A: 

solve it.i have a manager(which creates pojos and daos) that inject new Id to every newly create POJO.and during testing i add the id manually.thanks for your help

black sensei