简体   繁体   中英

Issue in Fetching the Many to One Associations in JPA

I have designed an REST webservice using Spring Boot+JPA. I have two Entity Classes - Event ,Address,EventMember. They are defined like below.

@Entity
@Table(name="event")
public class Event {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name="name")
    private String name;
    @Column(name="description")
    private String description;
    @JsonFormat(pattern="yyyy-MM-dd")
    private Timestamp event_date;
    private boolean is_deleted;

    @ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.ALL)
    @JoinColumn(name="address")
    @JsonBackReference
    @JsonInclude
    private Address address;

    @OneToMany(mappedBy="event")
    private Set<EventMember> eventMember;

Address :

@Entity
@Table(name="address")
public class Address {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String short_code;
private String address1;
private String address2;
private String landmark;
private String zipcode;
private boolean is_deleted;

@OneToMany(mappedBy="address",cascade = CascadeType.ALL)
private Set<Event> event;

When I am Fetching the Event By using the Event Id(Primary Key) in the JSON response I am getting the corresponding members in the EventMember entity, but the Address records are not getting fetched.
Can anyone Please let me know how can I get the Address Entities also while fetching the Event Member.

Code for Fetching Address by Id

public Address getAddressById(Long id) {
            return em.find(Address.class, id);
        }

是的,可以预期的是,因为您使用的是@JsonBackReference,这将忽略序列化的地址端

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM