Hibernate两次嵌入实体

时间:2017-05-15 11:28:24

标签: java hibernate jpa mapping

我有Hibernate 5.2.10版本和hibernate-jpa-2.1-api版本1.0.0.Final。我使用MairaDB作为数据库。在persistance.xml中,将属性hibernate.ejb.naming_strategy设置为DefaultComponentSafeNamingStrategy但仍然收到相同的错误: 实体映射中的重复列。我不想使用@attributeoverrides hibernate,我尝试了不同的方法,但仍然是同样的错误。我想要两个或更多嵌入式enities。

由于

1 个答案:

答案 0 :(得分:0)

你不能将DefaultComponentSafeNamingStrategy与Hibernate 5一起使用,因为它是来自Hibernate 4的旧NamingStrategy接口的实现。

您可能知道,Hibernate 5使用了两个新接口ImplicitNamingStrategyPhysicalNamingStrategy

您可以使用此隐式命名策略:org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl。 您需要设置hibernate.implicit_naming_strategy属性( hibernate.ejb.naming_strategy)。

对于这些实体

@Embeddable
public class AuthorInfo {

    @Column
    private String authorInfo;

    @OneToOne
    private Book bestBook;

}

@Entity
public class Book {

    @Id
    private Long pid;

    @Embedded
    private AuthorInfo firstAuthor;

    @Embedded
    private AuthorInfo secondAuthor;

}

它创建了这个架构

create table Book (
        pid bigint not null,
        firstAuthor_authorInfo varchar(255),
        secondAuthor_authorInfo varchar(255),
        firstAuthor_bestBook_pid bigint,
        secondAuthor_bestBook_pid bigint,
        primary key (pid)
)

检查架构的单元测试:TwoEmbeddedStrategyTest.java