[jboss-user] [EJB 3.0] - NonUniqueObjectException
Subhash.Bhushan
do-not-reply at jboss.com
Tue Mar 11 05:58:01 EDT 2008
Hi,
I am trying to use a OneToMany relationship between my MemberDetail Class and its addresses. Here is the code:
MemberDetail.java:
| public class MemberDetail implements java.io.Serializable {
| .
| .
| .
| private Collection<Address> addresses = new ArrayList<Address>();
| .
| .
| .
| @OneToMany(cascade={CascadeType.ALL}) @JoinColumn(name="LINKER_ID")
| public Collection<Address> getAddresses() {
| return addresses;
| }
| public void setAddresses(Collection<Address> addresses) {
| this.addresses = addresses;
| }
| .
| .
| .
|
Address.java:
| public class Address implements java.io.Serializable {
| .
| .
| .
| private int id;
| .
| .
| .
| @Id
| @Column(name = "ID", unique = true, nullable = false, insertable = true, updatable = true)
| public int getId() {
| return this.id;
| }
| public void setId(int id) {
| this.id = id;
| }
| .
| .
| .
|
The Address class does not have any reference to MemberDetail class.
Creating the Member Detail:
| memberDetail.setProfileId(profile.getProfileId());
| memberDetail.setStatus('R');
| memberDetail.setModificationNum(1);
|
| officeAddress.setActive('R');
| officeAddress.setModificationNum(1);
| personalAddress.setActive('R');
| personalAddress.setModificationNum(1);
| memberDetail.getAddresses().add(officeAddress);
| memberDetail.getAddresses().add(personalAddress);
| try {
| manager.persist(memberDetail);
| return true;
| }catch(Exception exc){
| exc.printStackTrace();
| return false;
| }
|
I get the following error upon persisting:
anonymous wrote :
| 19:37:16,671 ERROR [STDERR] javax.persistence.PersistenceException: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.justbooks.common.entities.Address#0]
| 19:37:16,687 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
| 19:37:16,687 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
| 19:37:16,687 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:182)
| 19:37:16,687 ERROR [STDERR] at com.justbooks.security.activities.ManageProfileBean.createProfile(ManageProfileBean.java:95)
|
I am using EJB 3.0 persistence with J2EE 1.4/J2SE 5. I assume that Address IDs should be autogenerated. I am using MySQL DB and I have marked the field for autogenerate.
I am sure I am doing something wrong, but not able to pinpoint the problem.
Thanks in advance. Any clues are welcome.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4135556#4135556
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4135556
More information about the jboss-user
mailing list