我有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;
}
}
TICKET CLASS
@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;
}
我使用的查询是返回getCurrentSession()。get(Ticket.class,id);
这是我正在获取的Ticket对象的toString
票证[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,tickets =],comments = []]
答案 0 :(得分:1)
您可以为同一个表Employee创建两个不同的Employee
实体。
在其中一个中,您可以映射列password
,而在另一个实体中则不映射password
。
因此,当您的意图是在没有密码的情况下检索实体时,请使用此新实体EmployeeWithoutPassword
。对于其他情况(插入,更新等),只需使用包含所有字段的常规实体。
您也可以use customized DTOs在不创建新实体的情况下完成此操作,只返回您想要的字段。
答案 1 :(得分:0)
您可以将@Transient
用作
@Transient
private String password;
此批注指定属性或字段不是持久的。它用于注释实体类,映射的超类或可嵌入类的属性或字段。