简体   繁体   English

使用MySQL DB在Hibernate单向中进行一对一映射?

[英]One to One Mapping in Hibernate Unidirectional with MySQL DB?

Am new to Hibernate and MySQL, i have two Table like OFFER_TABLE and OFFER_LIKES_DISLIKES 是Hibernate和MySQL的新手,我有两个表,例如OFFER_TABLEOFFER_LIKES_DISLIKES

OFFER_TABLE Columns OFFER_TABLE栏

OFR_ID(PK)
OFR_MSG

OFFER_LIKES_DISLIKES Columns OFFER_LIKES_DISLIKES列

OFFER_LIKES_DISLIKES_ID
OFR_ID(FK)
LIKE
DISLIKE

I want to map OFFER_TABLE and OFFER_LIKES_DISLIKES, Access the OFFER_LIKES_DISLIKES data through OFFER_TABLE. 我想映射OFFER_TABLE和OFFER_LIKES_DISLIKES,通过OFFER_TABLE访问OFFER_LIKES_DISLIKES数据。 Am using One to One Mapping but its not Working. 我正在使用一对一映射,但无法正常工作。

Hibernate Annotation Mapping Java Class 休眠注释映射Java类

Offers.class 优惠等级

@Id
@GeneratedValue
@Column(name = "OFR_ID", length = 11, nullable = false)
private int offer_id;

@OneToOne
@JoinColumn(name="OFR_ID", unique = true)
@OneToOne(cascade = CascadeType.ALL)
private MessageLikeDislikesDAO likeDislikes;

LikeDislike.class LikeDislike.class

@GeneratedValue
@Column(name="LIKES_DISLIKES_ID", length = 11, nullable = false)
private int likes_dislikes_id;

@Expose
@Column(name="OFR_ID", length = 11, nullable = false)
private int offer_id;

When I get the Data of Offers, want Like and Dislike data associated with it. 当我获得要约数据时,想要与之关联的喜欢和不喜欢的数据。 In LikeDislike table OFR_ID is UNIQUE. 在LikeDislike表中,OFR_ID是UNIQUE。 Am used One to One. 被一对一使用。 But i didn't get the data of LikeDislike. 但是我没有得到LikeDislike的数据。 Which one is best way to took that data. 哪一种是获取该数据的最佳方法。 Help me to solve this issue. 帮我解决这个问题。

There are many problems: 有很多问题:

  1. You're creating an association with a DAO instead of creating an association with an entity 您正在与DAO创建关联,而不是与实体创建关联
  2. You're storing the ID of the offer in the LikeDislike entity instead of storing an association with the Offer entity 您将要约的ID存储在LikeDislike实体中,而不是存储与要约实体的关联
  3. You're saying that there is a join column named OFR_ID and referring to the LikeDislike entity in the OFFER table . 您是说有一个名为OFR_ID的连接列,并引用了OFFER表中的LikeDislike实体。
  4. You disrespect Java naming conventions 您不尊重Java命名约定
  5. You're setting two OneToOne annotations on the same field 您要在同一字段上设置两个OneToOne批注

The mapping should be: 映射应为:

Offer: 提供:

@Id
@GeneratedValue
@Column(name = "OFR_ID", length = 11, nullable = false)
private Integer offerId;

@OneToOne(mappedBy = "offer", cascade = CascadeType.ALL)
private LikeDislike likeDislike;

LikeDislike: 喜欢不喜欢:

@GeneratedValue
@Column(name="LIKES_DISLIKES_ID", length = 11, nullable = false)
private Integer likeDislikeId;

@Expose
@OneToOne
@JoinColumn(name="OFR_ID", length = 11, nullable = false)
private Offer offer;

Relevant documentation 相关文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM