在org.hibernate.mapping.Table(user_details)中找不到逻辑名称为user_details_id的列及其相关的超表和辅助表

时间:2019-03-27 05:37:30

标签: hibernate spring-boot orm spring-data-jpa

我正在尝试创建简单的单向映射。我正在使用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;

}

有什么想法吗?预先谢谢你。

2 个答案:

答案 0 :(得分:0)

您可以只在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<>();

答案 1 :(得分:0)

不确定它会解决您的问题,但我刚刚遇到了同样的问题。 诀窍是在 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 更改为 playerIdPlayerEntity 中的字段名称,现在一切正常:

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