【问题标题】:Hibernate self referencing entity cascadeHibernate自引用实体级联
【发布时间】:2014-02-25 04:55:21
【问题描述】:

这是我的实体类:

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

我应该如何定义自引用列的级联? 谢谢

【问题讨论】:

    标签: java mysql hibernate annotations entity


    【解决方案1】:

    MySQL does not support it.

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多