Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What @Json annotation to use for forgein key

I need an annotation that will only return certain Object properties or only forgein key
User:

@Id
    @GeneratedValue(strategy = AUTO)
    private Long id;
    ...
    @JsonIgnore
    @OneToMany(mappedBy="user")
    private Set<Log> logs=new HashSet<>();

Logs:

@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long logsId;
    ...
    @JsonProperty<--------
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    private User user;
    ...

I am using @JsonProperty but it returns a whole object with vulnerable data

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use @jsonignore annotation for the properties that you don’t want to be retrieved. If you have too much data to be annotated, I would suggest you to move those attributes to a single class and have a property on that while it’s being annotated with @jsonignore. Mainly this annotation is used to remove cyclic data returned with foreign keys.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading