Hibernate and NonUniqueObjectException

I have an object that contains two other objects with the @ManyToOne relation.

@Entity
public class A extends Serializable{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @Cascade(CascadeType.SAVE_UPDATE)
    private B b;

    @ManyToOne
    @Cascade(CascadeType.SAVE_UPDATE)
    private C c;

}

If I try to save an instance of A with "B_ID" and "C_ID" of another A record, I get an exception:

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

For instance:

A table
|   ID  |   B_ID    |   C_ID    |
|    1  |      1    |   null    |    // this works
|    2  |   null    |      1    |    // this works
|    3  |      1    |      x    |    // this throws the exception
|    4  |      x    |      1    |    // this throws the exception

x=any value of existent B/C_ID 

B_ID and C_ID are not unique in my model and (B_ID + C_ID) are not a unique restriction!

What can I do?

Thanks in advance.

+5
source share
1 answer

Hibernate , , Session , , . - Hibernate , .

- dor, , "" . , ID , . , , .

"" ID ( Session.evict() Hibernate API EntityManager.detach() JPA 2.0 API), .

: , Hibernate, .

+15

All Articles