多对一的Hibernate标准,在检索数据时忽略一列?

问题描述 投票:1回答:2

我有3个实体。

  1. 雇员。
  2. 票。
  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

票证[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 = []]

hibernate spring-mvc spring-data-jpa hibernate-mapping hibernate-criteria
2个回答
1
投票

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

在其中一个中,您映射列password,而在另一个实体中,您不映射password

因此,当您的意图是在没有密码的情况下检索实体时,请使用此新实体EmployeeWithoutPassword。对于其他情况(插入,更新等),只需使用包含所有字段的常规实体。

您也可以use customized DTOs完成此操作而无需创建新实体,只返回您想要的字段。


0
投票

你可以使用@Transient作为

@Transient
private String password;

此批注指定属性或字段不是持久的。它用于注释实体类,映射的超类或可嵌入类的属性或字段。

© www.soinside.com 2019 - 2024. All rights reserved.