简体   繁体   English

ManyToMany映射关系表是空白的(Hibernate新手)

[英]ManyToMany mapping relationship table is blank (Hibernate newbie)

iam working on an app where the association between entities is as follows . 我正在开发一个应用程序,其中实体之间的关联如下。 Here comment is the owner and iteration and user are the inverse tables . 这里注释是所有者和迭代,用户是反向表。 The requirement is - A user can have many comments similarly a comment can be given by multiple users . 要求是 - 用户可以有很多评论,同样可以由多个用户给出评论。 Also an iteration can have multiple comments and a single comment can belong to multiple iterations . 迭代也可以有多个注释,单个注释可以属于多个迭代。 在此输入图像描述

The code is as follows - 代码如下 -

Comments entity - 评论实体 -

@Table(name="RCOMMENTS")
@Entity
public class RComment{

@Id
@Column(name="COMMENTID")
@GeneratedValue(strategy=GenerationType.AUTO)
private long commentid;

@Column(name="DESCRIPTION")
private String description;

@Column(name="TYPE")
private String type;

@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(name = "COMMENTS_USERS", joinColumns = { @JoinColumn(name = "COMMENTID") }, 
    inverseJoinColumns = { @JoinColumn(name = "USERID") })
private Set <RUsers> users = new HashSet<RUsers>();

@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(name = "COMMENTS_ITERATIONS", joinColumns = { @JoinColumn(name = "COMMENTID") }, 
    inverseJoinColumns = { @JoinColumn(name = "ITERATIONID") })
private Set <RIteration> iteration = new HashSet<RIteration>();

Users- 用户 -

@Entity
@Table(name = "RUSER")
public class RUsers {
@Id
@Column(name = "USERID")
@GeneratedValue(strategy=GenerationType.AUTO)
private long userid;
@Column(name = "USERNAME")
private String username;
@Column(name = "PASSWORD")
private String password;

@ManyToMany(mappedBy="users")
private Set <RComment> comment ;

Iteration - 迭代 -

@Table(name = "RITERATION")
@Entity
public class RIteration {  
 @Id
 @Column(name = "iterationid")
 @GeneratedValue(strategy=GenerationType.AUTO)
private long iterationid;
private Date startdate;
private Date enddate;
private long iter;

@ManyToMany(mappedBy="iteration")
private Set <RComment> comment = new HashSet<RComment>();

Test code - 测试代码 -

RUsers user = (RUsers) session.get(RUsers.class, this.userid);
Set<RComment> comments = user.getComment();

Now the issue that iam facing is the mapping tables are empty . 现在我面临的问题是映射表是空的。 Also user.getComment() yields empty set user.getComment()也会产生空集

Before i start my web app my db entity tables prepopulated with dummy values . 在我启动我的Web应用程序之前,我的数据库实体表预先填充了虚拟值。 But during run time when i debug the code user.getcomment returns empty set . 但是在运行期间我调试代码时user.getcomment返回空集。

Could anyone please help me out what could be the issue . 有谁可以帮我解决可能出现的问题。

If you have an bidirectional relationship between your Entities, then Hibernate will pay attention read only one of the two sides when it stores them in the database. 如果你的实体之间存在双向关系,那么当Hibernate将它们存储在数据库中时,它会注意只读取双方中的一方。

The side of the relation that is the important one, is the one that's ManyToMany annotation does NOT contain the mappedBy variable. 重要关系的一侧是ManyToMany注释不包含mappedBy变量的那一方。 And that is the side where you need to assign the value: 这就是您需要分配值的一面:

RUsers user = ...
RComment comment = ...

comment.users.add(user);  <--- that is the assignment hat hibernate will store!

user.comments.add(comment); <--- that assigment will NOT been stored by hibernate, but hibernate will load it from database 

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

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