Hardeek sharma
Hardeek sharma

Reputation: 19

Hibernate criteria for many to one, ignore a column when retrieving data?

I have 3 Entities.

  1. Employee.
  2. Ticket.
  3. Comment.

Each of them has one-to-many relationships with each other. I need to retrieve a record of single Ticket. But when I am getting the data along with it comes the data of the employee mapped to it. In the employee data that is coming, I don't want the password field data to be retrieved along with the other fields. SO WHAT MUST BE THE CRITERIA QUERY FOR THIS

EMPLOYEE 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;
    }
}

TICKET 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;
    }
}

COMMENT 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=[]]

Upvotes: 1

Views: 3749

Answers (3)

Ady Hnat
Ady Hnat

Reputation: 73

Maybe you can try this annotation:

@JsonIgnoreProperties(value = "password")

It works fine for me.

Upvotes: 0

Dherik
Dherik

Reputation: 19060

You can create two different Employee entities for the same table Employee.

In one of them you map the column password and in the other entity you not map the password.

So when your intention is retrieve the entity without the password, use this new entity EmployeeWithoutPassword. For the rest of the cases (insert, update, etc), just use the regular entity with all fields.

You can also use customized DTOs to accomplish this without create a new entity, just returning the fields you want.

Upvotes: 2

sahil0021
sahil0021

Reputation: 77

You can use @Transient as

@Transient
private String password;

This annotation specifies that the property or field is not persistent. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class.

Upvotes: 0

Related Questions