无法在 org.hibernate.mapping.Table(user_details) 及其相关的超级表和辅助表中找到具有逻辑名称的列:user_details_id
Posted
技术标签:
【中文标题】无法在 org.hibernate.mapping.Table(user_details) 及其相关的超级表和辅助表中找到具有逻辑名称的列:user_details_id【英文标题】:Unable to find column with logical name: user_details_id in org.hibernate.mapping.Table(user_details) and its related supertables and secondary tables 【发布时间】:2019-08-17 14:54:42 【问题描述】:我正在尝试创建简单的单向映射。我正在使用 MappedSuperclass 和 @JoinColumn 可能有问题。看起来实体无法从超类中找到 id 字段。
例外:
Caused by: org.hibernate.MappingException: Unable to find column with logical name: user_details_id in org.hibernate.mapping.Table(user_details) and its related supertables and secondary tables
at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:832) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:256) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:101) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processEndOfQueue(InFlightMetadataCollectorImpl.java:1827) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1771) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1658) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:287) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
超类:
@Getter
@Setter
@MappedSuperclass
public abstract class BaseEntity
@Id
@GeneratedValue(
strategy = GenerationType.IDENTITY
)
private Long id;
消息实体(连接发送方和接收方的 id)
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity(name = "Message")
@Table(name = "message")
public class Message extends BaseEntity
private String title;
private String details;
private String message_container;
private LocalDate dataOfSending;
@ManyToOne(optional = false)
@JoinColumn(name = "sender_id", referencedColumnName = "user_details_id")
private UserDetails sender;
@ManyToOne(optional = false)
@JoinColumn(name = "receiver_id", referencedColumnName = "user_details_id")
private UserDetails receiver;
用户详细信息(不应该知道消息)
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "UserDetails")
@Table(name = "user_details")
public class UserDetails extends BaseEntity
private String firstName;
private String lastName;
private String motherName;
private String fatherName;
private String personalIdentityNum;
private LocalDate dateOfBirth;
@OneToOne
@JoinColumn(name = "user_id")
private User user;
@OneToOne(mappedBy = "userDetails")
private Address address;
@OneToOne(mappedBy = "userDetails")
private Contact contact;
@OneToOne(mappedBy = "userDetails")
private ProfileImage profileImage;
有什么想法吗?提前谢谢你。
【问题讨论】:
你能检查我的答案吗? 【参考方案1】:不确定它是否能解决您的问题,但我刚刚遇到了同样的问题。 诀窍是在 SpringBoot 中,您必须引用 logical id,而不是 db 列名。
我的情况:
PlayerEntity
具有 playerId
,它生成名为 player_id
的列。这是@NaturalId
。
在另一个实体中,我写了一个@ManyToOne
,设置referencedColumnName = "player_id"
失败:
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to find column with logical name: player_id in org.hibernate.mapping.Table(player) and its related supertables and secondary tables
由于是“逻辑名称”,我尝试将player_id
更改为playerId
(PlayerEntity
中的字段名称,现在一切正常:
@ManyToOne
// @JoinColumn(name = "player_id", referencedColumnName = "player_id") // fails
@JoinColumn(name = "player_id", referencedColumnName = "playerId") // works! What the...
private PlayerEntity player;
这可行,生成具有正确 FK 的列:
constraint FK6aw598iko4dicfrfa6sj0mni1
foreign key (player_id) references player (player_id)
这实际上看起来像是 Spring JPA 或 Hibernate 中的一个错误。数据库是 mysql 8.1。
【讨论】:
【参考方案2】:你能改变Message
类中的这个部分吗
@ManyToOne(optional = false)
@JoinColumn(name = "sender_id", referencedColumnName = "user_details_id")
private UserDetails sender;
@ManyToOne(optional = false)
@JoinColumn(name = "receiver_id", referencedColumnName = "user_details_id")
private UserDetails receiver;
到
@ManyToOne
@JoinColumn(name = "sender_id")
private UserDetails sender;
@ManyToOne
@JoinColumn(name = "receiver_id")
private UserDetails receiver;
并在UserDetails
中添加这些代码
@OneToMany(mappedBy = "sender")
private List<Message> senderMessage = new ArrayList<>();
@OneToMany(mappedBy = "receiver")
private List<Message> receiverMessage = new ArrayList<>();
【讨论】:
嗨。是的,这是一种可能的解决方案,但我只是想如果没有 UserDetails 上的映射就可以实现这一点 实际上代码可以在没有在 UserDetails 端指定映射的情况下工作。但为什么?用户详情中没有映射的情况下如何找到sender_id?以上是关于无法在 org.hibernate.mapping.Table(user_details) 及其相关的超级表和辅助表中找到具有逻辑名称的列:user_details_id的主要内容,如果未能解决你的问题,请参考以下文章
[org.hibernate.mapping.Column(authorities)]
无法确定类型:TIMESTAMP,表:BATCH_JOB_CONFIG_DTLS,列:[org.hibernate.mapping.Column(ADD_USER_DTM)]
无法在 org.hibernate.mapping.Table(user_details) 及其相关的超级表和辅助表中找到具有逻辑名称的列:user_details_id