休眠条件多对一,在检索数据时忽略列?

哈迪克·夏尔马

我有3个实体。

  1. 雇员。
  2. 票。
  3. 评论。

他们每个人之间都有一对多的关系。我需要检索单张的记录但是,当我随身携带数据时,就会映射到员工的数据。在即将到来的员工数据中,我不希望将密码字段数据与其他字段一起检索。因此,对此的标准查询

员工班

@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);

这是我得到的票证对象的toString

工单[ticketId = 5,title = WFH,消息=我明天需要在家工作,优先级=立即,状态= RAISED,类型= WFH_REQUEST,所有者=员工[empId = 1,employeeCode = 123,fullName = emp,emaillAddress = emp,密码= emp,角色= 2,票证=],评论= []]

德里克

您可以Employee为同一表Employee创建两个不同的实体。

在其中一个中,您映射了列password,在另一个实体中,您未映射password

因此,当您打算在没有密码的情况下检索实体时,请使用此新实体EmployeeWithoutPassword对于其余情况(插入,更新等),只需将常规实体与所有字段一起使用。

您也可以使用自定义的DTO来完成此操作,而无需创建新实体,只需返回所需的字段即可。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章