繁体   English   中英

Hibernate自引用实体级联

[英]Hibernate self referencing entity cascade

这是我的实体类:

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

但是休眠并没有获得级联供自我参考:

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

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

MySQL不支持它。

与SQL标准的偏差:如果递归使用ON UPDATE CASCADE或ON UPDATE SET NULL来更新它在级联期间已更新的同一表,则它的行为类似于RESTRICT。 这意味着您不能使用自引用的ON UPDATE CASCADE或ON UPDATE SET NULL操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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