简体   繁体   中英

Hibernate self referencing entity cascade

This is my entity class:

    @Entity
    @Table(name="APPEAL")
    public class Appeal 
    {

        @Id 
        @Column(name="ID_APPEAL")   
        @GeneratedValue(strategy = GenerationType.AUTO ,generator="SQ_APPEAL")  
        @SequenceGenerator(name="SQ_APPEAL", sequenceName="SQ_APPEAL")
        private Long idAppeal;

        @ManyToOne
        @JoinColumn(name="ID_USER")
        @OnDelete(action = OnDeleteAction.CASCADE)
        private User user;

        @ManyToOne
        @JoinColumn(name="ID_APPEAL_PARENT")
        @OnDelete(action = OnDeleteAction.CASCADE)
        private Appeal parent;
...

Now, to generate the SQL to create my tables I'm using this code:

for (@SuppressWarnings("rawtypes") final Class entity : classes) {
            ejb3Configuration.addAnnotatedClass(entity);
        }

        dialectProps = new Properties();
        dialectProps.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");

        hibernateConfiguration = ejb3Configuration.getHibernateConfiguration();

        final StringBuilder script = new StringBuilder();

        final String[] creationScript = hibernateConfiguration.generateSchemaCreationScript(Dialect
                .getDialect(dialectProps));

But hibernate is not getting the cascade for the self reference:

create table APPEAL (ID_APPEAL bigint not null auto_increment, ID_USER bigint, ID_APPEAL_PARENT bigint, primary key (ID_APPEAL)) ENGINE=InnoDB;
alter table APPEAL add index FK_kcwnikcyoq8pskhdhnmtc0h9f (ID_USER), add constraint FK_kcwnikcyoq8pskhdhnmtc0h9f foreign key (ID_USER) references USER (ID_USER) on delete cascade;
alter table APPEAL add index FK_5ay1y0vn1nyeb9vgkpdb98q18 (ID_APPEAL_PARENT), add constraint FK_5ay1y0vn1nyeb9vgkpdb98q18 foreign key (ID_APPEAL_PARENT) references APPEAL (ID_APPEAL);

How should I define the cascade for the self referencing column? Thanks

MySQL does not support it.

Deviation from SQL standards: If ON UPDATE CASCADE or ON UPDATE SET NULL recurses to update the same table it has previously updated during the cascade, it acts like RESTRICT. This means that you cannot use self-referential ON UPDATE CASCADE or ON UPDATE SET NULL operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM