1 Reply Latest reply on Dec 18, 2006 1:39 PM by epbernard

    NonUniqueObjectException

    kvbisme

      OK, this is too common a scenario to be this confusing.

      I have two tables in a database, we will call them Table A and Table B

      Table A has an ID field as the primary key and other information
      Table B has a composite key of three fields ? let?s call them column B1, B2, and B3 and of course other information.

      The relationship of the tables is that an entity in Table A can point to zero or more entities in Table B. And multiple entries in Table A can reference the same entity in table B

      Table B knows nothing about Table A.

      I have to create my own tables (using the @Table) to appease the DBA. Anyway, I create a join table ? lets call it A_Joins_B with columns ID, B1, B2, and B3.

      In my Entity Bean I have:

      @OneToMany (cascade=CascadeType.ALL, fetch=FetchType.LAZY)
      @JoinTable (name=?A_joins_B?, joinColumns=@JoinColumn(name=?ID?), inverseJoinColumns={@JoinColumn(name=?B1?), @JoinColumn(name=?B2?), @JoinColumn(name=?B3?)}

      (p.s. I did try a @ManyToMany as well with the same results)

      As soon as I receive a series of records to update ( they arrived batched up in a big collection ) I iterate through the collection, convert each to an entity bean, and then attempt to merge them into the database with the em.merge(obj);

      This works until I encounter an update that contains a B entity that has already been referenced by a previous A entity. Then I get the org.hibernate.NonUniqueObjectEception with a message of:

      ?a different object with the same identifier value was already associated with the session: [org.myexample.entity.B#org.myexample.entity.key.B_PrimaryKey@44 ]?

      What am I missing?