2

I have three entities, say A, B and C. A contains multiple instances of B and C. B and C have String fields as their primary key which are meant to be populated at runtime. The codes are as follows:

Class A:

@Entity
public class A
{
 private int id;
 private B[] bSet = null;
 private C[] cSet = null;

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 public int getId()
 {return id;}

 @OneToMany(cascade = CascadeType.ALL, mappedBy = "a")
 @OrderColumn(name = "bIndex")
 public B[] getBset()
 {return bSet;}

 @OneToMany(cascade = CascadeType.ALL, mappedBy = "a")
 @OrderColumn(name = "cIndex")
 public C[] getCset()
 {return cSet;}
}

Class B:

@Entity
public class B
{
 private String bId;
 private A a;

 @Id
 public String getBId()
 {return bId;}

 @ManyToOne(cascade = CascadeType.ALL)
 public A getA(A a)
 {return a;}
}

Class C:

@Entity
public class C
{
 private String cId;
 private A a;

 @Id
 public String getCId()
 {return cId;}

 @ManyToOne(cascade = CascadeType.ALL)
 public A getA(A a)
 {return a;}
}

If I try to persist a B entity in my application, Hibernate does not go for an insert, as I saw after enabling Hibernate logging. It is trying for an update, and throwing this exception.

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

This problem hasn't been answered satisfactorily anywhere. I tried here, here and here. As they say, Hibernate expects a primary id of null while saving, and if it's anything else, it goes for updating the row, and throws this exception. But I need to insert rows with these Strings as primary keys. So what can I do for that? Please help me with this.

ADDITIONAL INFO

I think I'm fighting a losing battle here. It seems that this is a bug in Hibernate itself, and until Hibernate devs solve it, we can't do this. Just look here.

Community
  • 1
  • 1
supriyo_basak
  • 505
  • 1
  • 7
  • 24
  • Can you try to save B and C individually and see? String primary key is not the problem here. Looks like something do do with mapping or locking mode – Zeus Sep 24 '14 at 14:08
  • Unfortunately, I need to keep a reference of A for every B and C saved. In fact, this is the subject matter of another question which I posted just an hour back or so. So I can't save B or C individually. They all have to work together. – supriyo_basak Sep 24 '14 at 14:16
  • Can you try replacing B[] with list. Hibernate supports set, list, map, bag only. May be that could be an issue.. – Zeus Sep 24 '14 at 14:20
  • @Zeus, Simple arrays work fine. The problem definitely isn't there. When I replace the String primary key with a dummy auto-generated int one, all the rows of B are being persisted without any problem. – supriyo_basak Sep 24 '14 at 14:34
  • @abhishek, Post the code how you are trying to persist the entites and also the complete stacktrace of the exception. – Chaitanya Sep 24 '14 at 15:26
  • @Chaitanya, I did that last night in my other question. Did you go through it? – supriyo_basak Sep 25 '14 at 05:34
  • Given that your id specifies a generator, Hibernate could probably assume that when id==null => insert, when id!= null => update. – Niels Bech Nielsen Sep 25 '14 at 07:08
  • @NielsBechNielsen, the problem is for the primary keys of B and C, which have String primary keys and cannot be auto-generated. I haven't mentioned the problem for class A at all. If you look at the codes for either class B or C, you will find that none of them have generators specified. – supriyo_basak Sep 25 '14 at 08:37
  • Sorry, didn't read it fully, nor test it locally, just providing tips. Should't it access A when persisting B? Have you added debug info from org.hibernate.event.internal (show flushing events), and org.hibernate.internal.util (pretty print managed entities). That helps me occasionally – Niels Bech Nielsen Sep 25 '14 at 09:43
  • @abhishek For class B and C, you did not mention generator strategy. Use "assigned" and set the primary key for B and C before persisting. Please let me know if this works – javafan Sep 25 '14 at 16:34

0 Answers0