1

So I have a database and a library full of JPA entities and I'm generically reading them on a Jersey Rest Service. When I try to read an entity, it recursively reads bidirectional relationships. I know that @JsonIgnore fixes this issue, but the database is too large to manually go through each entity and add @JsonIgnore to each field of owner entities.

Customer:

@Entity
public class Customer implements DomainObject{

    private static final long serialVersionUID = 2543387766776209353L;

    @Id
    private long id;
    private String firstName;
    private String lastName;

    @OneToOne(fetch = FetchType.LAZY, mappedBy="customer",cascade={CascadeType.ALL})
    private Address address;

    ...Getters and Setters...

}

Address:

@Entity
public class Address implements DomainObject {

    private static final long serialVersionUID = 2543387766776209353L;

    @Id
    private long id;
    private String city;
    private String street;

    @PrimaryKeyJoinColumn
    @OneToOne(fetch = FetchType.LAZY)
    private Customer customer;

    ...Getters and Setters...
}

Basically I want to ignore sending customer in Address's Json without @JsonIgnore if that is possible. If not, is there another JSON building technique I can use to generate JSONs that follow the JPA notations of owners and mappedBy fields?

1 Answer 1

0

Maybe you could consider creating different objects for your REST services responses as described in the Data Transfer Object pattern.

This would require you to write more code but would give you more control over your services' responses even allowing your to add aggregates if needed to avoid unnecessary round-trips from clients to toy service as described in the Wikipedia article.

This answer elaborates more on DTOs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.