Spring JPA Repository ony已获取结果时,它将获取id而不是完整对象 [英] Spring JPA Repository ony fetches id instead of full object when it's already in result

查看:49
本文介绍了Spring JPA Repository ony已获取结果时,它将获取id而不是完整对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a SpringBoot rest application, I have two classes as follows:

User.java和Message.java.

User.java and Message.java.

消息具有-from-字段(用户),并且-to-属于(用户)类型.

Message has -from- field (User) and also -to- is of type (User).

所以我做到了:

在User.java中:

In 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中:

In 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条消息,并且其中的前两个消息发送给了同一用户,只有这两个消息中的第一个带有完整的用户对象,而第二个消息只有它的id,如下所示:

And apparently it works! -BUT- let's say I have 3 messages, and the first two of them went sent to the same user, only the first of those two comes with the full user object and the seconds only it's id, as follows:

[
{
    "id": 16,
    "from": {
        "id": 1,
        "firstName": "Ale",
        "lastName": null,
        "email": "axfeea@gmail.com",
        "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": "efefe@fefe.com",
        "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
}

]

如果第三条消息是由非重复用户提供的,则它带有完整的对象.

And if 3rd message comes with a non-repeated user it comes with the full object.

我需要始终有完整的物体.

I need the full object to come always.

和-btw-在数据库中,它们看起来都很好并且以相同的方式显示.

And -btw- in the database they all look good and same way.

有什么想法吗?

谢谢大家!

推荐答案

由于指定了注释 JsonIdentityInfo ,Jackson会像生成的JSON一样序列化对象.

Since you have specified the annotation JsonIdentityInfo, Jackson serializes the objects as in the resulting JSON.

注释的Javadoc指定:

The Javadoc of the annotation specifies:

在实践中,这是通过将第一个实例序列化为完整的对象和对象标识以及将对对象的其他引用作为参考值来实现的.

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.

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

So if you don't want that behaviour, remove the annotation.

这篇关于Spring JPA Repository ony已获取结果时,它将获取id而不是完整对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆