[hibernate-dev] Problems using self-ref

mailinglist at j-b-s.de mailinglist at j-b-s.de
Wed May 20 18:34:25 EDT 2009


Hi all!

I tried to use a self referencing entity but unfortunately the deletion fails with:
java.lang.IllegalStateException: java.lang.IllegalStateException: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [PInstrument#1]

Using google I only found some samples using HBM files but I want to use annotations. Is it necessary to declare both relations (manytoone and onetomany)? I tried this also but without success.

please see the hibernate model class below:

@Entity
@Table(name = "INSTRUMENT")
public class PInstrument implements Serializable
{
    @Id
    @Column(name = "INSTRUMENT_ID")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "instrument_seq_gen")
    @SequenceGenerator(name = "instrument_seq_gen", sequenceName = "INSTRUMENT__SEQ")
    private Long _id;

    @Column(name = "NAME", unique = true, nullable = false, length = 50)
    @NotNull
    @Length(min = 2)
    private String _name;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
    @Cascade({ org.hibernate.annotations.CascadeType.DELETE_ORPHAN, org.hibernate.annotations.CascadeType.ALL} )
    @JoinColumn(name="FK_INSTRUMENT_ID")
    @Index(name = "IDX_FK_MAIN_INSTRUMENT")
    private PInstrument _mainInstrument;

.... more attributes and set/get methods
}

I even added all possible cascade annotations, but the issue still remains. Any hint is realy appreciated

Thanks in advance 

jens



More information about the hibernate-dev mailing list