I want to load an UserReference object from the database, but from the verifier attribute I want to load only the id, firstName and lastName, so that the userReference would look something like this:
{
"id": 1,
"company": "company1",
"companyContactName": "some name",
"companyPosition": "programmer",
"referenceDate": "02/04/2005",
"verifier": {
"id":1
"firstName": "Jane",
"lastName": "Smith"
"email":null,
"username":null,
"office:null,
"department":null
}
}
I used an entity graph for the UserReference class, but the one I used loads all the information that the user has, including the email, username, office and department. Is there any way to specify something like EntityGraphType.FETCH to the subgraph, so that it will load only the id, firstName and lastName for the verifier?
This is my UserReferenceRepository:
public interface UserReferenceRepository extends JpaRepository<UserReference, Long>{
@EntityGraph(value = "userReferenceGraph" , type = EntityGraphType.FETCH )
UserReference findOne(Long id);
}
The UserReference class:
@Getter
@Setter
@EqualsAndHashCode (exclude = {"id", "verifier"})
@ToString(exclude = {"id"})
@Entity
@NamedEntityGraphs({
@NamedEntityGraph(
name = "userReferenceGraph",
attributeNodes = {
@NamedAttributeNode(value = "verifier", subgraph = "verifierGraph")
},
subgraphs = {
@NamedSubgraph(
name = "verifierGraph",
type = User.class,
attributeNodes = {
@NamedAttributeNode(value = "id"),
@NamedAttributeNode(value = "firstName"),
@NamedAttributeNode(value = "lastName")})})
})
public class UserReference {
@Id
@GeneratedValue
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", referencedColumnName = "user_id", foreignKey = @ForeignKey (name = "FK_UserReference_UserHRDetails_user_id"))
@JsonIgnore
private UserHRDetails hrDetails;
private String company;
private String companyContactName;
private String companyPosition;
private Date referenceDate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "verifier_id")
private User verifier;
}
and User:
@Getter @Setter
@EqualsAndHashCode(exclude = {"id", "department", "company", "authorities", "hrDetails"})
@ToString(exclude = {"password"})
@Entity
@AllArgsConstructor
@Builder
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Access(value = AccessType.PROPERTY)
private Long id;
@Size(max = 50)
@Column(name = "first_name", length = 50)
private String firstName;
@Size(max = 50)
@Column(name = "last_name", length = 50)
private String lastName;
@Column(length = 100, unique = true, nullable = false)
private String email;
@Column(length = 50, unique = true, nullable = false)
private String username;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "department_id")
private Department department;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "office_id")
private Office office;
}