我有以下实体类UserEntity和TicketEntity。用户拥有许多票证,许多票证可以属于用户。我的问题是,有没有办法通过使用Hibernate自动加载属于相关用户的所有票证,还是我必须手动加载来自数据库的所有实体关系?我认为.load()会这样做,但我不太确定。在我的情况下,我可以做类似
的事情userEntity.load()
感谢任何帮助,谢谢
UserEntity.java
package com.issuetracking.domain;
/**
*/
import java.util.List;
import javax.persistence.*;
@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.LAZY)
private List<TicketEntity> tickets;
//Getters/Setters
public List<TicketEntity> getTickets() {
return tickets;
}
public void setTickets(List<TicketEntity> tickets) {
this.tickets = tickets;
}
public int getRole_id() {
return role_id;
}
public void setRole_id(int role_id) {
this.role_id = role_id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirmpassword() {
return confirmpassword;
}
public void setConfirmpassword(String confirmpassword) {
this.confirmpassword = confirmpassword;
}
public boolean isVerified() {
return verified;
}
public void setVerified(boolean verified) {
this.verified = verified;
}
}
TicketEntity.java
package com.issuetracking.domain;
import java.util.Date;
import javax.persistence.*;
@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(name="user_id", insertable=false, updatable=false)
private UserEntity belongs_to;
@ManyToOne
@JoinColumn(name="user_id", insertable=false, updatable=false)
private UserEntity assigned_to;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getDate_created() {
return date_created;
}
public void setDate_created(Date date_created) {
this.date_created = date_created;
}
public int getStatus_id() {
return status_id;
}
public void setStatus_id(int status_id) {
this.status_id = status_id;
}
public int getUrgency_id() {
return urgency_id;
}
public void setUrgency_id(int urgency_id) {
this.urgency_id = urgency_id;
}
public UserEntity getBelongs_to() {
return belongs_to;
}
public void setBelongs_to(UserEntity belongs_to) {
this.belongs_to = belongs_to;
}
public UserEntity getAssigned_to() {
return assigned_to;
}
public void setAssigned_to(UserEntity assigned_to) {
this.assigned_to = assigned_to;
}
}
答案 0 :(得分:0)
用户拥有许多票证,许多票证可以属于用户。
在这种情况下,关系应该是ManyToMany
我的问题是,有没有办法自动加载属于相关用户的所有票据
使用EAGER FetchType代替LAZY,比如
@OneToMany(fetch = FetchType.EAGER)
private List<TicketEntity> tickets;