2017-05-15 44 views
0

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

感谢

回答

0

不能使用DefaultComponentSafeNamingStrategy与Hibernate 5,因为它是由Hibernate老NamingStrategy接口的实现4.

正如你可能知道,休眠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

+0

我使用的标注@column和使用该org.hibernate.boot。 model.naming.ImplicitNamingStrategyComponentPathImpl并且不起作用,但是在删除它的注释之后。 – Elhamo

+0

@Elhamo我用Hibernate SessionFactory检查了这个,而不是用PersistentContext。这可能是一个原因,但我不确定。你可能会错过一些东西。 –