我想從數據庫裝載一個UserReference對象,但是從覈查屬性我想加載只有ID,名字和姓氏,從而使userReference看起來會是像這樣:如何加載子只有指定的屬性在@NamedEntityGraph
{
"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
}
}
我用於UserReference類的實體圖,但我一個負載使用該用戶具有,包括電子郵件,用戶名,辦公室和部門中的所有信息。 有什麼辦法來指定類似EntityGraphType.FETCH到子圖,以便它將只加載ID,名字和lastName的驗證?
這是我UserReferenceRepository:
public interface UserReferenceRepository extends JpaRepository<UserReference, Long>{
@EntityGraph(value = "userReferenceGraph" , type = EntityGraphType.FETCH)
UserReference findOne(Long id);
}
的UserReference類:
@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;
}
和用戶:
@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;
}