Hibernate 休眠多对一的条件,在检索数据时忽略列?
我有3个实体。Hibernate 休眠多对一的条件,在检索数据时忽略列?,hibernate,spring-mvc,spring-data-jpa,hibernate-mapping,hibernate-criteria,Hibernate,Spring Mvc,Spring Data Jpa,Hibernate Mapping,Hibernate Criteria,我有3个实体。 雇员 票 评论 它们之间都有一对多的关系。我需要检索单张票证的记录。但是,当我获取数据时,会出现映射到该数据的员工的数据。在即将到来的员工数据中,我不希望将密码字段数据与其他字段一起检索。那么,该查询的条件必须是什么 @Entity @NamedQuery(name = "getUserByEmail", query = "from Employee where emaillAddress = :emailAddress") public class Employee imple
@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;
}
员工类别
@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;
}
我使用的查询是返回getCurrentSession().get(Ticket.class,id)强>
这是我得到的票证对象的toString
Ticket[ticketId=5,title=WFH,message=我明天需要在家工作,优先级=立即,状态=提升,type=WFH\u请求,所有者=员工[empId=1,employeeCode=123,全名=emp,emaillAddress=emp,密码=emp,角色=2,tickets=],注释=[]
您可以使用
@Transient
作为
@Transient
private String password;
此批注指定属性或字段不是持久的。它用于注释实体类、映射超类或可嵌入类的属性或字段。您可以为同一个表Employee创建两个不同的
Employee
实体
在其中一个实体中映射列密码
,在另一个实体中不映射密码
因此,当您打算检索不带密码的实体时,请使用此新实体EmployeeWithoutPassword
。对于其他情况(插入、更新等),只需将常规实体与所有字段一起使用即可
您也可以在不创建新实体的情况下完成此操作,只返回所需的字段。@Transient不会将密码值保存在数据库中。我只是不想要密码,当我读票证的id,随着它的emp也来了,在该emp我不想要密码,因为你可以看到我粘贴的对象的字符串。Thanx,我现在做的DTO的用户界面