java - Spring JPA 存储库仅在结果已存在时获取 id 而不是完整对象

标签 java spring spring-boot spring-data-jpa

在 SpringBoot Rest 应用程序中,我有两个类,如下所示:

用户.java 和 Message.java。

消息具有“from-”字段(用户),并且“to-”的类型为(用户)。

所以我做了这样的:

在 User.java 中:

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, 
property="id") 
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

private String firstName;
private String lastName;
private String email;

@JsonIgnore
@OneToMany(mappedBy = "to")
private List<Message> receivedMessages;

@OneToOne
@JoinColumn(name = "type")
private UserType type;

在 Message.java 中:

@Entity
public class Message {

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;

@ManyToOne
@JoinColumn(name = "from_user_id")
private User from;

@ManyToOne
@JoinColumn(name = "to_user_id")
private User to;

private String subject;
private String message;
private Date sentTime;
private Date readTime;

private Integer replyTo;

(setters & getters, etc)

显然它有效! -但是- 假设我有 3 条消息,其中前两条消息发送给同一个用户,只有这两条消息中的第一 strip 有完整的用户对象,第二条消息只有它的 id,如下所示:

[
{
    "id": 16,
    "from": {
        "id": 1,
        "firstName": "Ale",
        "lastName": null,
        "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e3f26383b3b3f1e39333f3732703d3133" rel="noreferrer noopener nofollow">[email protected]</a>",
        "username": null,
        "password": "123456",
        "avatar": "https://..............jpg",
        "type": null
    },
    "to": 1,
    "subject": "sub",
    "message": "hola",
    "sentTime": null,
    "readTime": null,
    "replyTo": null
},
{
    "id": 17,
    "from": {
        "id": 2,
        "firstName": "Carlos",
        "lastName": "Perez",
        "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="65000300030025030003004b060a08" rel="noreferrer noopener nofollow">[email protected]</a>",
        "username": null,
        "password": "fe",
        "avatar": "https://..................jpg",
        "type": null
    },
    "to": 1,
    "subject": "sub1",
    "message": "chau",
    "sentTime": null,
    "readTime": null,
    "replyTo": null
},
{
    "id": 18,
    "from": 2,
    "to": 1,
    "subject": "efefae",
    "message": "oooook",
    "sentTime": 1503249653000,
    "readTime": null,
    "replyTo": null
}

]

如果第三条消息来自非重复用户,则它带有完整的对象。

我总是需要完整的对象。

而且-顺便说一句-在数据库中它们看起来都很好而且是一样的。

有什么想法吗?

先谢谢大家了!

最佳答案

由于您已指定注释 JsonIdentityInfo,Jackson 将按照生成的 JSON 序列化对象。

注释的 Javadoc 指定:

In practice this is done by serializing the first instance as full object and object identity, and other references to the object as reference values.

因此,如果您不希望出现这种行为,请删除注释。

关于java - Spring JPA 存储库仅在结果已存在时获取 id 而不是完整对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45785037/

相关文章:

java - 无法使我的 spring boot 应用程序与 jsp 一起工作

java - 带 header 的 RSA 私钥和 OPENSSH 私钥有什么区别?

java - 如何在 Java2D 中创建更大尺寸的图像

java - 在字符串中使用正则表达式而不是 contains() 渲染速度较慢

java - 主键上的 JPA @AutoGenelated 对嵌套实体使用父序列/自动增量

java - 从 JSP 按钮执行 Spring MVC 中的方法

java - Spring MVC 可以在@RequestMapping 之后调用@ModelAttribute 吗?

java - spring boot:无法解析导入的org.springframework.jdbc.core.JdbcTemplate

spring-boot - Spring data jpa,外部化原生查询

basic-authentication - 使用 Spring Boot 进行基本身份验证