Java 休眠两次嵌入实体
我有Hibernate 5.2.10版本和Hibernate-jpa-2.1-api版本1.0.0.Final。我使用MairaDB作为数据库。在persistence.xml中,将属性hibernate.ejb.naming_strategy设置为DefaultComponentSafeNamingStrategy,但仍然收到相同的错误: 实体映射中的重复列。我不想使用@attributeoverrides hibernate,我尝试了不同的方法,但仍然是相同的错误。我想要两个或两个以上的嵌入式设备Java 休眠两次嵌入实体,java,hibernate,jpa,mapping,Java,Hibernate,Jpa,Mapping,我有Hibernate 5.2.10版本和Hibernate-jpa-2.1-api版本1.0.0.Final。我使用MairaDB作为数据库。在persistence.xml中,将属性hibernate.ejb.naming_strategy设置为DefaultComponentSafeNamingStrategy,但仍然收到相同的错误: 实体映射中的重复列。我不想使用@attributeoverrides hibernate,我尝试了不同的方法,但仍然是相同的错误。我想要两个或两个以上的嵌入
谢谢您不能在Hibernate 5中使用
DefaultComponentSafeNamingStrategy
,因为它是Hibernate 4中旧的NamingStrategy
接口的实现
您可能知道,Hibernate 5使用了两个新接口ImplicitNamingStrategy
和PhysicalNamingStrategy
您可以使用这种隐式命名策略:org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl
。
您需要设置hibernate.implicit\u naming\u策略
属性(不hibernate.ejb.naming\u策略
)
对于这些实体
@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)
)
检查架构的单元测试:我使用了annotation@column,并使用了org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl,但不起作用,但在删除批注后,它起作用了。@Elhamo我已经用hibernate SessionFactory检查了这一点,而不是用PersistentContext。这可能是一个原因,但我不确定。你可能会错过一些东西。@v.ladynev我的经验是,如果你使用
org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl
策略,那么@Column
将被忽略(特别是名称值)。有没有一种方法可以在保留@Column
的同时使用此策略?这个问题可能是@Elhamo所描述的。@mbmast@Column被PhysicalNamingStrategy
忽略。正如我所记得的,它不应该被隐式NamingStrategy
忽略。PhysicalNamingStrategy只包含关于该列的信息。如何检测命名是否应用于特定类或可嵌入类型的特定字段。没有有用的信息。只需通过标识符获取列名。getText()。