gpt4 book ai didi

java - Hibernate JOIN 还获取相关实体

转载 作者:行者123 更新时间:2023-11-30 10:28:15 26 4
gpt4 key购买 nike

我正在使用 Spring Data REST、Hibernate 5.2.10。这是我的模型:

    @Entity
public class WorkSession extends AbstractEntity {
private static final long serialVersionUID = -5058296954283517829L;

@NotNull(message = "The user is mandatory")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private User agent;

@NotNull(message = "The checkpoint is mandatory")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private CheckPoint checkPoint;

@Column(nullable = false, updatable = false)
private LocalDateTime startDate = LocalDateTime.now();

private LocalDateTime endDate;

@Lob
private String notes;

public WorkSession() {
}

和用户:

@Entity
public class User extends AbstractEntity implements UserDetails {
private static final long serialVersionUID = 5745401123028683585L;
public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();

@NotNull(message = "The name of the user cannot be blank")
@Column(nullable = false)
private String name;

/** CONTACT INFORMATION **/
private String landlinePhone;

private String mobilePhone;

@NotNull(message = "The username cannot be blank")
@Column(nullable = false, unique = true)
private String username;

@Email(message = "The email address is not valid")
private String email;

@JsonIgnore
private String password;

@Column(nullable = false)
private String timeZone = "Europe/Rome";

@JsonIgnore
private LocalDateTime lastPasswordResetDate;

@Column(nullable = false, columnDefinition = "BOOLEAN default true")
private boolean enabled = true;

@Type(type = "json")
@Column(columnDefinition = "json")
private String[] roles = new String[] {};

在我的一项服务中,我创建了这个查询:

@RestResource(rel = "findActiveWorkSessionForUser", path = "findActiveWorkSessionForUser")
@Query("SELECT ws FROM WorkSession ws JOIN ws.agent u WHERE u.username= ?#{principal.username} AND ws.endDate IS NULL")
@Transactional(readOnly = true)
public List<WorkSession> findByAgentUsernameAndEndDateIsNull();

这个存储库是通过 Spring Data REST 公开的,当我调用这个端点时,结果还包含嵌套对象用户,就像你在这里看到的那样:

`{
"_embedded": {
"workSessions": [
{
"sid": "ed9f01d0-f18d-491b-aad9-4445d457d7eb",
"createdDate": "2017-06-27T13:13:24",
"lastModifiedDate": "2017-06-27T13:13:24",
"lastModifiedBy": "admin",
"startDate": "2017-06-27T13:13:24",
"endDate": null,
"notes": null,
"new": false,
"_embedded": {
"agent": {
"name": "Administrator",
"roles": [
"ROLE_ADMIN"
],
"username": "admin",
"activeWorkSession": "",
"_links": {
"self": {
"href": "http://localhost:8080/api/v1/users/1{?projection}",
"templated": true
}
}
}
},
"_links": {
"self": {
"href": "http://localhost:8080/api/v1/workSessions/1"
},
"workSession": {
"href": "http://localhost:8080/api/v1/workSessions/1"
},
"checkPoint": {
"href": "http://localhost:8080/api/v1/workSessions/1/checkPoint"
},
"agent": {
"href": "http://localhost:8080/api/v1/workSessions/1/agent"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/v1/workSessions/search/findActiveWorkSessionForUser"
}
}
}`

我想避免这种情况(事实上,该字段在我的配置中是惰性的)。有没有办法避免这种行为?

最佳答案

您可以通过在其上添加 JsonProperty 访问来阻止 User 的 JSON 序列化。

@JsonProperty(access=WRITE_ONLY)

在此处查看更多相关信息:https://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonProperty.Access.html

关于java - Hibernate JOIN 还获取相关实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44781374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com