1

It's my first question. I hope I can get any hint here to solve my problem.

I use the hibernate for my application. Here is my Code.

public class A {
    @OneToMany(mappedBy = "a", cascade = {CascadeType.MERGE, CascadeType.REMOVE}, orphanRemoval = true, fetch = FetchType.LAZY)
    private List<B> bList;
}

public class B{
    @ManyToOne(fech = FetchType.EAGER, optional = false)
    @JoinColumn(name = "b_id", insertable = false, updatable = false)
    private A a;
}

And I expected that I can save the bList when I save the A automatically. It works.

But there is a big problem when bList is null. hibernate try to save bList with A, even though bList is null.

So it makes this Error.

ORA-01400: cannot insert NULL into ("MYTEMPTB"."TB_B"."B_ID")

how can I solve it?? Please give me some hint. Thanks.

Jin
  • 57
  • 1
  • 6
  • initialize your list with empty one `private List bList=new ArrayList<>();` – Mohamed Nabli Jun 16 '16 at 08:32
  • You don't want bList to be null, or? Then you could use a prepersist callback to set the bList to something else than null before A is saved. Another option would be the initialization of bList using the new operator, although I would prefer the first proposal. – Lonkey Jun 16 '16 at 08:37
  • @MohamedNabli yes. My List is initialize in empty state. But I add some data into bList When I need. By using **addBList**method. And this method work like this. if(this.bList == null){ this.bList = new ArrayList(); } b.setA(this); bList.add(b); Thank you for your attention. :-) – Jin Jun 17 '16 at 02:18
  • @Lonkey It have a two diffrent case. Sometime It need some List of B entity. But sometime it doesn't need any data about B. So, I tried to use the **prepersist**. but It's not working like my plan. Maybe I need to try another proposal. Thank for your advice. Have a nice day! ;-) – Jin Jun 17 '16 at 02:27

0 Answers0