这里是Hibernate新手,遇到了试图正确映射我的Hibernate Entity类之间关系的问题。
我有一个Ticket Entity和一个User Entity。用户拥有许多票证,但票证仅属于一个唯一用户。我所做的是将用户 - 门票映射为@OneToMany
关系,将门票 - 用户映射为@ManyToOne
关系。但是,我没有收到任何退票。
Query Hibernate运行的是:
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=?
实体类是:
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
@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;
感谢任何帮助,
谢谢!
答案 0 :(得分:0)
在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;
在TicketEntity.java中,将{name =“user_id”)添加到UserEntity belongs_to
的@JoinColumn,如下所示:
@ManyToOne
@JoinColumn(name = "user_id", insertable=false, updatable=false)
private UserEntity belongs_to;
private UserEntity assigned_to
相同。如果您想查询它,可能需要在UserEntity.java中为它添加另一个列表。