0

I have the following Entity. In this I want to fetch all data except phoneNumber. What will be the best solution? It would be fine if I could do it with annotation.

public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "employee_name")
private String name;

@Column(name = "gender")
private char gender;

@Column(name = "date_of_birth")
private String dob;

@Column(name = "skills")
private String[] skills;

@Column(name = "phone_number")
private String phoneNumber;

//getter setter

}
Sanket Patel
  • 227
  • 1
  • 14

4 Answers4

1

To tell what will be the best way to do this you have to say why you want to do this and what you want to achieve. There are many options:

  • omit the getter
  • use a projection (DTO or interface)
  • use inhreitance
  • use inheritance with @MappedSuperclass
Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119
0

Can you please use @Transient on your field. If it is a subclass of anyclass then on class level please use @Embedded.

If not then you need to read this as this is always lazy fetch From Hibernate, Chapter 19. Improving performance:

Lazy attribute fetching: an attribute or single valued association is fetched when the instance variable is accessed. This approach requires buildtime bytecode instrumentation and is rarely necessary.

Kunal Vohra
  • 2,703
  • 2
  • 15
  • 33
0

You can use @Basic(fetch = FetchType.LAZY) on phoneNumber and just don't read it or set to null to prevent any read

-1

You can use @Column(insertable = true, updatable = false) and I am not sure if we can ignore while fetching using entity. you can achieve your requirements using JPA Projections

Also,@JsonIgnore may be useful. it is used to tell Jackson to ignore a certain property of a Java object but