简体   繁体   English

休眠关系映射问题

[英]Hibernate Relationship Mapping Issue

Hibernate novice here, running into issues trying to properly map the relationships between my Hibernate Entity classes. Hibernate新手,在尝试正确映射我的Hibernate Entity类之间的关系时遇到了问题。

I have a Ticket Entity and a User Entity. 我有一个票证实体和一个用户实体。 A user has many tickets, but a ticket only belongs to one unique user. 一个用户有许多票证,但是票证仅属于一个唯一用户。 What i've done is mapped the User - Tickets as a @OneToMany relationship and the Tickets - User as a @ManyToOne relationship. 我所做的是将用户-票证映射为@OneToMany关系,而将票务-用户映射为@ManyToOne关系。 However, I am not getting any tickets returned. 但是,我没有得到任何退票。

The Query Hibernate is running is: 查询休眠正在运行的是:

Hibernate: select tickets0_.user_user_id as user1_1_3_, tickets0_.tickets_ticket_id as tickets2_3_, ticketenti1_.ticket_id as ticket1_0_0_, ticketenti1_.assigned_to_user_id as assigned7_0_0_, ticketenti1_.belongs_to_user_id as belongs8_0_0_, ticketenti1_.date_created as date2_0_0_, ticketenti1_.description as descript3_0_0_, ticketenti1_.status_id as status4_0_0_, ticketenti1_.title as title0_0_, ticketenti1_.urgency_id as urgency6_0_0_, userentity2_.user_id as user1_1_1_, userentity2_.email as email1_1_, userentity2_.firstname as firstname1_1_, userentity2_.lastname as lastname1_1_, userentity2_.password as password1_1_, userentity2_.role_id as role6_1_1_, userentity2_.username as username1_1_, userentity2_.verified as verified1_1_, userentity3_.user_id as user1_1_2_, userentity3_.email as email1_2_, userentity3_.firstname as firstname1_2_, userentity3_.lastname as lastname1_2_, userentity3_.password as password1_2_, userentity3_.role_id as role6_1_2_, userentity3_.username as username1_2_, userentity3_.verified as verified1_2_ from user_ticket tickets0_ inner join ticket ticketenti1_ on tickets0_.tickets_ticket_id=ticketenti1_.ticket_id left outer join user userentity2_ on ticketenti1_.assigned_to_user_id=userentity2_.user_id left outer join user userentity3_ on ticketenti1_.belongs_to_user_id=userentity3_.user_id where tickets0_.user_user_id=?

The Entity Classes are: 实体类为:

TicketEntity.java TicketEntity.java

@Entity
@Table(name="ticket")
public class TicketEntity {

@Id
@Column(name="ticket_id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

@Column(name="title")
private String title;

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

@Column(name="date_created")
@Temporal( TemporalType.TIMESTAMP )
private Date date_created;

@Column(name="status_id")
private int status_id;

//private TicketStatus status;

@Column(name="urgency_id")
private int urgency_id;

@ManyToOne
@JoinColumn(insertable=false, updatable=false)
private UserEntity belongs_to;

@ManyToOne
@JoinColumn(insertable=false, updatable=false) 
private UserEntity assigned_to;

UserEntity.java UserEntity.java

@Entity
@Table(name="user")
public class UserEntity {

@Id
@Column(name="user_id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

@Column(name="firstname")
private String firstname;

@Column(name="lastname")
private String lastname;

@Column(name="username")
private String username;

@Column(name="email")
private String email;

@Column(name="password")
private String password;

@Transient
private String confirmpassword;

@Column(name="verified")
private boolean verified;

@Column(name="role_id")
private int role_id;

@OneToMany(fetch = FetchType.EAGER)
private List<TicketEntity> tickets;

Any help is appreciated, 任何帮助表示赞赏,

Thanks! 谢谢!

In UserEntity.java, you need to add "mappedBy" for List<TicketEntity> tickets , like this: 在UserEntity.java中,您需要为List<TicketEntity> tickets添加“ mappedBy”,如下所示:

@OneToMany(fetch = FetchType.EAGER, mappedBy="belongs_to")//why you have two UserEntity in TicketEntity.java?
private List<TicketEntity> tickets;

In TicketEntity.java, add (name = "user_id") to @JoinColumn for UserEntity belongs_to , like this: 在TicketEntity.java中,将(name =“ user_id”)添加到@Entity实体的UserEntity belongs_to ,如下所示:

@ManyToOne
@JoinColumn(name = "user_id", insertable=false, updatable=false)
private UserEntity belongs_to;

Same for private UserEntity assigned_to . private UserEntity assigned_to相同。 You may need add another list for it in UserEntity.java if you want to query it too. 如果您也想查询它,则可能需要在UserEntity.java中为其添加另一个列表。

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

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