简体   繁体   中英

JPA ManyToOne field not getting stored

I have a bean structure as shown below. The problem that I am facing is while trying to persist XBean, I am able to save all the data (ie xName, pBean, qBean, rBean, kBeans are all visible in storage) but there is no entry for Y_BEAN.

I am pretty much new with JPA annotations so not really sure if what I have done is correct. The idea is to have multiple entries of XBean (ie as List) with one instance of YBean

XBean also will hold an instance of YBean as its parent so when I retrieve XBean I should get all the data. Is there something wrong with @ManyToOne annotation?

@Entity
@Table (name = "X_BEAN")
public class XBean implements XInterface {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;

    private String xName;

    @OneToOne(cascade=CascadeType.ALL)
    private PBean pBean;

    @ManyToOne
    @JoinColumn(name="y_id")
    private YBean yBean;

    @OneToOne(cascade=CascadeType.ALL)
    private qBean qBean;

    @OneToOne(cascade=CascadeType.ALL)
    private RBean rBean;

    @OneToMany (mappedBy="xBean", cascade=CascadeType.ALL)
    private List<KBean> kBeans;

    // getter setters for each are below ...
}

and structure of YBean is like below

@Entity
@Table (name = "Y_BEAN")
public class YBean implements XInterface {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;

    @OneToOne(cascade=CascadeType.ALL)
    private ZBean zName;

    @OneToOne(cascade=CascadeType.ALL)
    private PBean pBean;

    @OneToOne(cascade=CascadeType.ALL)
    private RBean rBean;

    @OneToMany (mappedBy="yBean", cascade=CascadeType.ALL)
    private List<XBean> xBeans;

    // getter setter for each are below ...
}

I am using Google App Engine's storage

You need cascade=CascadeType.PERSIST on your ManyToOne , to tell Hibernate to persist the YBean when it persists the XBean.

You should also think about whether you want the cascade attribute on the inverse OneToMany . With CascadeType.ALL , if you were to delete an instance of YBean, Hibernate will delete all associated XBeans (of which there may be zero, one, or many), because CascadeType.ALL means "apply persistence operations, including deletion, to any other entities accessible via this property or collection". If you didn't have CascadeType.ALL and you deleted a YBean that was referred to by one or more XBeans, then those XBeans would now referenced a non-existent YBean ID, so you'd probably need to do some cleanup in that case.

Both options are irrelevant if your business logic never deletes a YBean until it is not referred to by any XBeans, but if your business logic doesn't prevent the case, then you should cascade or not based on whether you want to get rid of the XBeans or whether you want to clean them up (but not delete them) to no longer refer to the YBean that's being deleted.

@ManyToOne
@JoinColumn(name="y_id")
private YBean yBean;

what is the column y_id ?
what is the definition of it?

you can try removing @JoinColumn(name="y_id") and let JPA handle it.

and also add fetch = FetchType.EAGER like this.

@OneToMany (mappedBy="yBean", cascade=CascadeType.ALL, fetch = FetchType.EAGER)
private List<XBean> xBeans;

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