本文介绍了无法在 org.hibernate.mapping.Table(user_details) 及其相关的超级表和辅助表中找到具有逻辑名称的列:user_details_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建简单的单向映射.我正在使用 MappedSuperclass 和 @JoinColumn,可能有问题.看起来实体无法从超类中找到 id 字段.

I am trying to create simple unidirectional mapping. I am using MappedSuperclass and @JoinColumn and there is probably a problem. Looks like entity cannot find id field from super class.

异常:

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)

Message entity (Joins id's of both sender and receiver)

@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;
}

用户详细信息(不应该知道消息)

User Details (should not know about messages)

@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;

}

有什么想法吗?提前致谢.

Any ideas? Thank you in advance.

推荐答案

你可以在 Message 类中更改此部分

Can you just change this section in Message class

   @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<>();

这篇关于无法在 org.hibernate.mapping.Table(user_details) 及其相关的超级表和辅助表中找到具有逻辑名称的列:user_details_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:39