Spring JPA Repository ony fetches id instead of full object when it's already in result

﹥>﹥吖頭↗ 提交于 2021-02-04 16:24:06

问题


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

User.java and Message.java.

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

So I've made it like this:

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;

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)

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.

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

Any ideas?

Thank you all in advance!


回答1:


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

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.



来源:https://stackoverflow.com/questions/45785037/spring-jpa-repository-ony-fetches-id-instead-of-full-object-when-its-already-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!