Hibernate One-to-One在创建时生成删除

时间:2017-12-20 11:31:48

标签: java hibernate hibernate-mapping

在使用One-to-One级联创建hibernate对象时,我遇到了一个奇怪的问题。 当不需要删除时,会生成Delete语句。

                myEmitter.on(contextGuid, ()=>{
                    if(!socket.disconnected){
                        getUserListing();
                    }
                });

角色可以翻译和扩展:

@Entity
@Table(name = "ROLE")
public class Role extends Translatable {
    //business fields
}

包由一组不同的翻译组成

@MappedSuperclass
public class Translatable{

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "TRANSLATION_ID", updatable = false)
    @Getter @Setter
    private TranslationPack translationPack;
}

UPD:

@Entity
@Table(name = "TRANSLATION_PACK")
@NoArgsConstructor @AllArgsConstructor
public class TranslationPack{
    //id declaration skipped

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "translationPack", orphanRemoval = true)
    @Getter @Setter
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Translation> translations;
}

如果使用一个翻译保存角色,请获取以下内容:

@Entity
@Table(name = "TRANSLATION")
@AllArgsConstructor @NoArgsConstructor
public class Translation implements Serializable{

    @EmbeddedId
    @Getter @Setter
    private TranslationId translationId;

    @Column(name = "TRANSLATION", length = 255)
    @Getter  @Setter
    private String translation;

    @MapsId("languageCode")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(updatable = false, name = "LANGUAGE_CODE")
    @Getter  @Setter
    @JsonIgnore
    private LanguagePack languagePack;

    @MapsId("translationPackId")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(updatable = false, name = "TRANSLATION_PACK_ID")
    @Getter  @Setter
    private TranslationPack translationPack;

    @Embeddable
    @AllArgsConstructor @NoArgsConstructor
    public static class TranslationId implements Serializable {
        @Column(name = "LANGUAGE_CODE")
        @Getter @Setter
        String languageCode;

        @Column(name = "TRANSLATION_PACK_ID")
        @Getter  @Setter
        Long translationPackId;

        @Override
        public int hashCode() ; //skipping

        @Override
        public boolean equals(Object object); //skipping
   }

   @Override
   public int hashCode() ; //skipping

   @Override
   public boolean equals(Object object); //skipping
}
  

Oracle限制失败,在外键上进行TRANSLATION_ID验证   (o.h.e.jdbc.spi.SqlExceptionHelper - ORA-02292)。

我的映射有什么问题?或者使用一些临时ID来休眠? (我的hibernate版本是4.2.7,不幸的是无法升级)

更新: 改为

Hibernate: select ROLE_SEQ.nextval from dual
Hibernate: select TRANSLATION_PACK_SEQ.nextval from dual
Hibernate: insert into TRANSLATION_PACK (id) values (?)
Hibernate: insert into ROLE (TRANSLATION_ID, CODE, NAME, ROLE_TYPE, id) values (?, ?, ?, ?, ?)
Hibernate: insert into TRANSLATION_PACK (id) values (?)
Hibernate: insert into TRANSLATION (TRANSLATION, LANGUAGE_CODE, TRANSLATION_PACK_ID) values (?, ?, ?)
Hibernate: update ROLE set CODE=?, NAME=?, ROLE_TYPE=? where id=?
Hibernate: delete from TRANSLATION_PACK where id=?

(可更新 true

开始生成普通脚本:

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "TRANSLATION_ID", updatable = true)
    @Getter @Setter
    private TranslationPack translationPack;

我不认为它是一个合适的解决方案,因为我想控制id永远不会改变。

0 个答案:

没有答案