IllegalStateException with Hibernate 4 and ManyToOne cascading

后端 未结 9 2182
梦如初夏
梦如初夏 2020-12-01 16:06

I\'ve got those two classes

MyItem Object:

@Entity
public class MyItem implements Serializable {

    @Id
    private Integer id;
    @ManyToOne(casc         


        
9条回答
  •  庸人自扰
    2020-12-01 16:23

    Same here, check your equals() method. Most probably is badly implemented.

    Edit: I have verified that a merge operation will not work if you don't implement your Entity's equals() and hashCode() methods correctly.

    You should follow these guidelines for implementing equals() and hashCode():

    http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch04.html#persistent-classes-equalshashcode

    "It is recommended that you implement equals() and hashCode() using Business key equality. Business key equality means that the equals() method compares only the properties that form the business key. It is a key that would identify our instance in the real world (a natural candidate key)"

    That means: You should NOT use your Id as part of your equals() implementation!

提交回复
热议问题