0

I have a user entity:

@Entity(name = "users")
@DynamicUpdate
public class User implements Serializable {

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

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    @JsonIgnore
    @JsonIgnoreProperties("user")
    private Set<Car> cars;
}

and a car entity:

@Entity(name = "cars")
@DynamicUpdate
public class Car implements Serializable {

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

    @Column(name = "plate_number")
    @JsonView(View.Summary.class)
    private String plateNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="user_id")
    @JsonIgnore
    @JsonManagedReference("user-coordinate")
    @Access(value = AccessType.FIELD)
    private User user;
}

when I get cars list I get this:

{
            "id": 5,
            "plateNumber": "aaaaada",
            "user": {
                "id": 110,
                "name": null
            }
        },

But I want to get this(without user entity!):

{
            "id": 5,
            "plateNumber": "aaaaada",
        },

I have 7 years' experience in PHP (I said it to know I am familiar with how to search and get what I want) but I am a beginner in java spring I searched a lot for the answer all I get is that it's related to Jackson but I don't know how to solve it please help me with details I will appreciate it.

Thank you

Roham Shojaei
  • 440
  • 4
  • 18
  • LOL. You are still trying to get this working despite me giving you the answer over a day ago! https://stackoverflow.com/q/57039548/1356423 – Alan Hay Jul 16 '19 at 14:41
  • 3
    Possible duplicate of [Avoid Jackson serialization on non fetched lazy objects](https://stackoverflow.com/questions/21708339/avoid-jackson-serialization-on-non-fetched-lazy-objects) – Alan Hay Jul 16 '19 at 14:41

2 Answers2

1

you are using @JsonManagedReference("user-coordinate") which overrides @JsonIgnore

JsonManagedReference directs Jackson to serialize the user when serializing car, so if you want user to not be serialized you need to removed it so @JsonIgnore takes place

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
  • Suddenly it worked! after this, I was doing the exact same but then it didn't work I first added JsonIgnore and tests it that didn't work then added JsonManagedReference now it works!! – Roham Shojaei Jul 16 '19 at 11:01
  • it depends on the order of the annotation , if JsonIgnore comes last it will work – Amer Qarabsa Jul 16 '19 at 11:02
  • Thank you so much, Now what should I do to user_id with integer type apears I created a new variable with column name user_id but it gave me an error of duplicate column name used how should I get this field as integer – Roham Shojaei Jul 16 '19 at 11:07
  • create user_id in Car table? your join column with user is 'user_id' this is why you are getting duplicate – Amer Qarabsa Jul 16 '19 at 11:12
  • Sorry the problem still exists I when it was current I just accidentally have been removed the getter and setter of user property so when I uncommented them the user field appears again – Roham Shojaei Jul 16 '19 at 11:23
  • did you remove @JsonManagedReference("user-coordinate")? – Amer Qarabsa Jul 16 '19 at 11:27
  • Yes this is now my column structure: @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="user_id") @JsonIgnore @Access(value = AccessType.FIELD) private User user; – Roham Shojaei Jul 16 '19 at 11:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196524/discussion-between-amer-qarabsa-and-hassan-shojaei). – Amer Qarabsa Jul 16 '19 at 11:31
0

For anyone had this problem two, I had two dependencies for JsonIgnore I was using:

org.codehaus.jackson.annotate

that was the problem I changed it to:

com.fasterxml.jackson.annotation

And the problem solved

Roham Shojaei
  • 440
  • 4
  • 18