【发布时间】:2018-09-08 17:13:33
【问题描述】:
我有 3 个实体。
- 员工。
- 票。
- 评论。
他们每个人都有一对多的关系。我需要检索单个 Ticket 的记录。但是,当我获取数据时,就会出现映射到它的员工数据。在即将到来的员工数据中,我不希望密码字段数据与其他字段一起被检索。那么这个的条件查询必须是什么
员工等级
@Entity
@NamedQuery(name = "getUserByEmail", query = "from Employee where emaillAddress = :emailAddress")
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id", updatable = false)
private int empId;
@JsonIgnore
@Column(name ="emp_code" ,unique = true, nullable = false)
private long employeeCode;
@Column(name = "full_name", nullable = false)
private String fullName;
@JsonIgnore
@Column(name = "email_address", nullable = false, unique = true)
private String emaillAddress;
@JsonIgnore
@Column(name = "password", nullable = false)
private String password;
@Column(name = "employee_role", nullable = false)
private int role;
@JsonIgnore
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private Collection<Ticket> tickets = new ArrayList<>();
public Employee() {
this.fullName = "";
this.password = "";
this.emaillAddress = "";
this.role = 2;
}
}
机票类
@Entity
public class Ticket {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int ticketId;
private String title;
private String message;
@Enumerated(EnumType.STRING)
private TicketPriority priority;
@Enumerated(EnumType.STRING)
private TicketStatus status;
@Enumerated(EnumType.STRING)
private TicketType type;
@JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "dd-MM-yyyy | HH:mm",timezone="Asia/Kolkata")
@Temporal(TemporalType.TIMESTAMP)
private Date timestamp;
@JsonIgnore
@ManyToOne
@JoinColumn(name = "owner_id")
Employee owner;
@OneToMany(mappedBy = "ticket", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private Collection<Comment> comments = new ArrayList<>();
public Ticket() {
super();
this.title = "";
this.message = "";
timestamp = new Date();
this.status = TicketStatus.RAISED;
}
}
评论类
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int commentId;
private String message;
@OneToOne
@JoinColumn(name="comment_owner")
Employee employee;
@ManyToOne
@JoinColumn(name="ticket_id")
Ticket ticket;
}
我使用的查询是 return getCurrentSession().get(Ticket.class, id);
这是我得到的 Ticket 对象的 toString
Ticket [ticketId=5,title=WFH,message=我明天需要在家工作,priority=IMMEDIATE,status=RAISED,type=WFH_REQUEST,owner=Employee [empId=1,employeeCode=123,fullName=emp , emaillAddress=emp, password=emp, role=2, ticket=], cmets=[]]
【问题讨论】:
标签: hibernate spring-mvc spring-data-jpa hibernate-mapping hibernate-criteria