【问题标题】:Hibernate criteria for many to one, ignore a column when retrieving data?多对一的休眠条件,在检索数据时忽略一列?
【发布时间】:2018-09-08 17:13:33
【问题描述】:

我有 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

Ticket [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, ticket=], cmets=[]]

【问题讨论】:

    标签: hibernate spring-mvc spring-data-jpa hibernate-mapping hibernate-criteria


    【解决方案1】:

    你可以使用@Transient作为

    @Transient
    private String password;
    

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

    【讨论】:

    • @Transient 不会将密码的值保存在数据库中。当我通过 id 读取票证时,我只是不想要密码,随之而来的 emp 也随之而来,在那个 emp 中我不想要密码字段,因为你可以看到我粘贴的对象的 toString。
    【解决方案2】:

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

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

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

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

    【讨论】:

    • 谢谢,我现在正在为 UI 制作 DTO
    【解决方案3】:

    也许你可以试试这个注解:

    @JsonIgnoreProperties(value = "password")
    

    对我来说很好用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多