当列预计同时为 FK 和 PK 时,嵌入不起作用
我正在尝试学习 JPA,目前遇到了可嵌入关键字的问题。
我的目标是病史,我想将firstName和lastName作为主键和外键,但是我遇到了错误消息。
这是我遇到的错误消息。
Caused by: org.hibernate.AnnotationException: Unable to find column reference in the @MapsId mapping: first_name
at org.hibernate.cfg.CopyIdentifierComponentSecondPass.createSimpleProperty(CopyIdentifierComponentSecondPass.java:203) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.cfg.CopyIdentifierComponentSecondPass.doSecondPass(CopyIdentifierComponentSecondPass.java:112) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1691) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1623) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:295) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]
... 16 common frames omitted
这是我试图使其工作的示例代码。
@Embeddable
public class PersonId implements Serializable {
String firstName;
String lastName;
}
@Entity
public class Person implements Serializable {
@EmbeddedId
PersonId personId;
}
@Entity
public class MedicalHistory implements Serializable {
@EmbeddedId PersonId id;
@MapsId
@JoinColumns({
@JoinColumn(name = "firstName", referencedColumnName = "firstName"),
@JoinColumn(name = "lastName", referencedColumnName = "lastName")
})
@OneToOne
Person patient;
}
不确定我做错了什么?
- 注意:我正在使用这个学习材料“JSR 338:JavaTM Persistence API,版本2.2”
I am trying to learn JPA, I'm currently encountering issues with the embeddable keywords.
My Goal is on Medical History, I would want to make firstName and lastName as Primary Key and Foreign Key however I am encountering an error message.
This is the error message that I am encountering.
Caused by: org.hibernate.AnnotationException: Unable to find column reference in the @MapsId mapping: first_name
at org.hibernate.cfg.CopyIdentifierComponentSecondPass.createSimpleProperty(CopyIdentifierComponentSecondPass.java:203) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.cfg.CopyIdentifierComponentSecondPass.doSecondPass(CopyIdentifierComponentSecondPass.java:112) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1691) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1623) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:295) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494) ~[hibernate-core-5.6.4.Final.jar:5.6.4.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]
... 16 common frames omitted
This is the sample code that i am trying to make work.
@Embeddable
public class PersonId implements Serializable {
String firstName;
String lastName;
}
@Entity
public class Person implements Serializable {
@EmbeddedId
PersonId personId;
}
@Entity
public class MedicalHistory implements Serializable {
@EmbeddedId PersonId id;
@MapsId
@JoinColumns({
@JoinColumn(name = "firstName", referencedColumnName = "firstName"),
@JoinColumn(name = "lastName", referencedColumnName = "lastName")
})
@OneToOne
Person patient;
}
not sure what I am doing wrong?
- note: I am using this learning material "JSR 338: JavaTM Persistence API, Version 2.2"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@chris 是绝对正确的,Hibernate 正在将列名从“firstName”转换为“first_name”,我猜“lastName”的列名也会相同。
您还可以更改列名称以匹配
@Embeddable
上的命名,并调整@JoinColumn
上的名称来实现这一目的。如果您不自动生成数据库架构,则还必须重命名数据库中的列。
@chris is absolutley right, Hibernate is turning the column name fron 'firstName' to 'first_name' and i guess it will be the same for 'lastName'.
You can also change the column names to match the naming like
On the
@Embeddable
and adapt the name on the@JoinColumn
that should do the trick.If you dont generate your database schema automaticly, you will have to rename also the columns in the database.