当前位置: 首页 > 知识库问答 >
问题:

JPA/Hibernate复合主Foriegn密钥多对一映射

融渊
2023-03-14

notification_detail-单个主键

我想使用JPA持久化和读取这些对象,但无法使ManyToOne映射工作。

许多通知映射到一个notification_detail。

create table notification(
notification_user_id        int             not null,
notification_detail_id     int              not null,
notification_status_cd      int             not null,
primary key(notification_user_id, notification_detail_id),
constraint pkey_notification_detail foreign key(notification_detail_id) references notification_detail(notification_detail_id),
constraint pkey_notification_status_cd foreign key(notification_status_cd) references lk_notification_status(notification_status_id)
);

create table notification_detail (
notification_detail_id  int             auto_increment,
notification_type_cd    int             not null,
notification_message    varchar(50)     not null,
primary key(notification_detail_id),
constraint pkey_notification_type_cd foreign key(notification_type_cd) references lk_notification_type(notification_type_id)
);
class NotificationId implements Serializable {
    private static final long serialVersionUID = 1L;
    Integer notificationUserId;
    Integer details;
    public Integer getNotificationUserId() {
        return notificationUserId;
    }
    public void setNotificationUserId(Integer notificationUserId) {
        this.notificationUserId = notificationUserId;
    }
    public Integer getDetails() {
        return details;
    }
    public void setDetails(Integer details) {
        this.details = details;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((details == null) ? 0 : details.hashCode());
        result = prime * result + ((notificationUserId == null) ? 0 : notificationUserId.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        NotificationId other = (NotificationId) obj;
        if (details == null) {
            if (other.details != null)
                return false;
        } else if (!details.equals(other.details))
            return false;
        if (notificationUserId == null) {
            if (other.notificationUserId != null)
                return false;
        } else if (!notificationUserId.equals(other.notificationUserId))
            return false;
        return true;
    }
}

@Entity
@Table(name="notification")
@IdClass(NotificationId.class)
public class Notification implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @NotNull
    private Integer notificationUserId;

    @Id
    @NotNull
    @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    @JoinColumn(name="notification_detail_id")
    private NotificationDetail details;

    @NotNull
    private Integer notificationStatusCd;

    @NotNull
    private Integer updateUserId;

    public Integer getNotificationUserId() {
        return notificationUserId;
    }

    public void setNotificationUserId(Integer notificationUserId) {
        this.notificationUserId = notificationUserId;
    }

    public NotificationDetail getDetails() {
        return details;
    }

    public void setDetails(NotificationDetail details) {
        this.details = details;
    }

    public Integer getNotificationStatusCd() {
        return notificationStatusCd;
    }

    public void setNotificationStatusCd(Integer notificationStatusCd) {
        this.notificationStatusCd = notificationStatusCd;
    }

    public Integer getUpdateUserId() {
        return updateUserId;
    }

    public void setUpdateUserId(Integer updateUserId) {
        this.updateUserId = updateUserId;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((details == null) ? 0 : details.hashCode());
        result = prime * result + ((notificationStatusCd == null) ? 0 : notificationStatusCd.hashCode());
        result = prime * result + ((notificationUserId == null) ? 0 : notificationUserId.hashCode());
        result = prime * result + ((updateUserId == null) ? 0 : updateUserId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Notification other = (Notification) obj;
        if (details == null) {
            if (other.details != null)
                return false;
        } else if (!details.equals(other.details))
            return false;
        if (notificationStatusCd == null) {
            if (other.notificationStatusCd != null)
                return false;
        } else if (!notificationStatusCd.equals(other.notificationStatusCd))
            return false;
        if (notificationUserId == null) {
            if (other.notificationUserId != null)
                return false;
        } else if (!notificationUserId.equals(other.notificationUserId))
            return false;
        if (updateUserId == null) {
            if (other.updateUserId != null)
                return false;
        } else if (!updateUserId.equals(other.updateUserId))
            return false;
        return true;
    }

}

@Entity
@Table(name="notification_detail")
public class NotificationDetail implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer notificationDetailId;

    @NotNull
    private Integer notificationTypeCd;

    @NotNull
    private String notificationMessage;

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="details")
    private Set<Notification> notifications;

    @NotNull
    private Integer updateUserId;

    public Integer getNotificationDetailId() {
        return notificationDetailId;
    }

    public void setNotificationDetailId(Integer notificationDetailId) {
        this.notificationDetailId = notificationDetailId;
    }

    public Integer getNotificationTypeCd() {
        return notificationTypeCd;
    }

    public void setNotificationTypeCd(Integer notificationTypeCd) {
        this.notificationTypeCd = notificationTypeCd;
    }

    public String getNotificationMessage() {
        return notificationMessage;
    }

    public void setNotificationMessage(String notificationMessage) {
        this.notificationMessage = notificationMessage;
    }

    public Set<Notification> getNotifications() {
        return notifications;
    }

    public void setNotifications(Set<Notification> notifications) {
        this.notifications = notifications;
    }

    public void addNotification(Notification notification){
        if(notifications == null)
            notifications = new HashSet<Notification>();
        notifications.add(notification);
    }

    public Integer getUpdateUserId() {
        return updateUserId;
    }

    public void setUpdateUserId(Integer updateUserId) {
        this.updateUserId = updateUserId;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((notificationDetailId == null) ? 0 : notificationDetailId.hashCode());
        result = prime * result + ((notificationMessage == null) ? 0 : notificationMessage.hashCode());
        result = prime * result + ((notificationTypeCd == null) ? 0 : notificationTypeCd.hashCode());
        result = prime * result + ((notifications == null) ? 0 : notifications.hashCode());
        result = prime * result + ((updateUserId == null) ? 0 : updateUserId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        NotificationDetail other = (NotificationDetail) obj;
        if (notificationDetailId == null) {
            if (other.notificationDetailId != null)
                return false;
        } else if (!notificationDetailId.equals(other.notificationDetailId))
            return false;
        if (notificationMessage == null) {
            if (other.notificationMessage != null)
                return false;
        } else if (!notificationMessage.equals(other.notificationMessage))
            return false;
        if (notificationTypeCd == null) {
            if (other.notificationTypeCd != null)
                return false;
        } else if (!notificationTypeCd.equals(other.notificationTypeCd))
            return false;
        if (notifications == null) {
            if (other.notifications != null)
                return false;
        } else if (!notifications.equals(other.notifications))
            return false;
        if (updateUserId == null) {
            if (other.updateUserId != null)
                return false;
        } else if (!updateUserId.equals(other.updateUserId))
            return false;
        return true;
    }

}
NotificationDetail details = new NotificationDetail();
details.setMessage("Hello");
details.setTypeCd(NotificationTypeEnum.INFO);
detail.setUpdateUserId(userId);

Notification notif = new Notification();
notif.setNotifiedUserId(userId);
notif.setStatusCd(NotificationStatusEnum.UNREAD);
notif.setDetails(details);

notificationRepository.save(notif);
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'notificati0_.details_notificationDetailId' in 'field list'

我试过所有类型的不同配置。我能想到的最好的办法是,我的情况很复杂,因为父对象“notification_detail_id”有一个复合键“notification_detail_id”,它也是子对象的主键。这使得Hibernate很难先持久化子项,然后拿密钥保存给父项。

我意识到我的注释在某个地方是不正确的,但就是弄不明白。

共有1个答案

阴焱
2023-03-14

看起来你正在处理一个“派生身份”。将类更改为如下所示:

class NotificationId implements Serializable {
    private static final long serialVersionUID = 1L;
    Integer notifiedUserId;
    Integer details;
    ...
}


@Entity
@IdClass(NotificationId.class)
@Table(name="notification")
public class Notification implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @NotNull
    @Column(name="notification_user_id")
    private Integer notifiedUserId;

    @Id
    @NotNull
    @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    @JoinColumn(name="notification_detail_id")
    private NotificationDetail details;

    @NotNull
    @Column(name="notification_status_cd")
    private Integer statusCd;

    @Column(name="update_timestamp")
    private Date timestamp;

    ...
}

注意:

  • NotificationId.NotificationDetailId已重命名为Details以匹配Notification
  • 中的相应字段
  • notification.notificationdetailid已删除
  • notification.details已标记为@id
 类似资料:
  • 我有以下两个类(和他们的pk类)与这些注释。我删除了setters/getters/hashcode/equals来压缩示例 我最终得到了这个错误 我的猜测是,因为“年”是在联接表中共享的,所以我弄乱了一些关联实体的语法。请注意,这些视图的实体之间存在隐式关系,我正试图在注释中对其建模。我尝试了一些JPA建模工具,这些工具在建模后给了我同样的错误。我已尝试将联接列设置为,并将设置为。 我当然可以只

  • 用户详细信息 有人能看出我做错了什么吗?

  • 对于和,我有如下表格结构。的复合键有三列——,,,但部门只有两列与Employee的复合键匹配——,

  • 我正在努力解决hibernate映射问题,即在订单表的主键和带有一些额外列的产品购物车的复合键之间映射一对多关系 产品购物车表具有组合键购物车ID和产品ID 我试着跟踪但没有成功 有人能帮我实现这一点吗?下面是错误消息 原因:组织。冬眠AnnotationException:非法尝试使用mappedBy关联pcoPK定义@JoinColumn。组织pk。冬眠cfg公司。Ejb3JoinColumn

  • 问题内容: 我在为某些实体设置jpa映射时遇到麻烦。我有一个如下定义的父实体。 然后,我有一个带有复合键的子实体,以及此表的主键的外键,如下所示: 我知道子实体不正确,但是我不确定如何将其设置为具有复合PK。我知道我需要设置一个PK类,但是当一个字段是父类的外键时,我不确定该怎么做。一旦设置好,父级将如何引用子级实体? 任何帮助表示赞赏。 问题答案: 这受JPA 2规范的 第2.4.1节“与派生身

  • 我基于以下示例实现了这一点:https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/