多对一的 Hibernate 标准,在检索数据时忽略列?
Hibernate criteria for many to one, ignore a column when retrieving data?
我有 3 个实体。
- 员工。
- 门票。
- 评论。
他们每个人彼此之间都是一对多的关系。我需要检索单个 Ticket 的记录。但是,当我获取数据时,员工的数据就会映射到它。在即将到来的员工数据中,我不希望密码字段数据与其他字段一起被检索。那么这个
的条件查询必须是什么
员工CLASS
@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;
}
}
门票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;
}
}
评论CLASS
@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;
}
Query i am using is return getCurrentSession().get(Ticket.class, id);
This is the toString of the Ticket object i am getting
Ticket [ticketId=5, title=WFH, message=i need to work from home tomorrow, priority=IMMEDIATE, status=RAISED, type=WFH_REQUEST, owner=Employee [empId=1, employeeCode=123, fullName=emp, emaillAddress=emp, password=emp, role=2, tickets=], comments=[]]
您可以将@Transient
用作
@Transient
private String password;
此注释指定 属性 或字段不是持久的。它用于注释 属性 或实体 class 的字段、映射的超 class 或可嵌入的 class.
您可以为同一 table 名员工创建两个不同的 Employee
实体。
在其中一个实体中映射列 password
,在另一个实体中不映射 password
。
因此,当您打算在没有密码的情况下检索实体时,请使用这个新实体 EmployeeWithoutPassword
。对于其余情况(插入、更新等),只需使用包含所有字段的常规实体。
您也可以 use customized DTOs 在不创建新实体的情况下完成此操作,只需返回您想要的字段。
也许你可以试试这个注解:
@JsonIgnoreProperties(value = "password")
对我来说效果很好。
我有 3 个实体。
- 员工。
- 门票。
- 评论。
他们每个人彼此之间都是一对多的关系。我需要检索单个 Ticket 的记录。但是,当我获取数据时,员工的数据就会映射到它。在即将到来的员工数据中,我不希望密码字段数据与其他字段一起被检索。那么这个
的条件查询必须是什么员工CLASS
@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;
}
}
门票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;
}
}
评论CLASS
@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;
}
Query i am using is return getCurrentSession().get(Ticket.class, id);
This is the toString of the Ticket object i am getting
Ticket [ticketId=5, title=WFH, message=i need to work from home tomorrow, priority=IMMEDIATE, status=RAISED, type=WFH_REQUEST, owner=Employee [empId=1, employeeCode=123, fullName=emp, emaillAddress=emp, password=emp, role=2, tickets=], comments=[]]
您可以将@Transient
用作
@Transient
private String password;
此注释指定 属性 或字段不是持久的。它用于注释 属性 或实体 class 的字段、映射的超 class 或可嵌入的 class.
您可以为同一 table 名员工创建两个不同的 Employee
实体。
在其中一个实体中映射列 password
,在另一个实体中不映射 password
。
因此,当您打算在没有密码的情况下检索实体时,请使用这个新实体 EmployeeWithoutPassword
。对于其余情况(插入、更新等),只需使用包含所有字段的常规实体。
您也可以 use customized DTOs 在不创建新实体的情况下完成此操作,只需返回您想要的字段。
也许你可以试试这个注解:
@JsonIgnoreProperties(value = "password")
对我来说效果很好。