Hibernate自引用实体级联

时间:2014-02-01 12:23:15

标签: java mysql hibernate annotations entity

这是我的实体类:

    @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;
...

现在,要生成SQL以创建我的表,我正在使用此代码:

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));

但是hibernate没有获得自引用的级联:

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);

如何定义自引用列的级联? 感谢

1 个答案:

答案 0 :(得分:1)

MySQL does not support it.

  

与SQL标准的偏差:如果ON UPDATE CASCADE或ON UPDATE SET NULL recurses更新它在级联期间先前更新的同一个表,它就像RESTRICT一样。这意味着您不能使用自引用ON UPDATE CASCADE或ON UPDATE SET NULL操作。