0

i've created an application which uses hibernate inheritance and mapping. There is a parent class named Payment and Child class called Credit. Right i'm trying to insert multiple child values for one single parent id. Like as shown below

|``````````````|
|    Payment   |
|--------------|
| id  |  nane  |
|-----|--------|
|  1  |  mod 1 |
|-----|--------|
|  2  |  mod 2 |
|-----|--------|


|````````````````````````````````|
|              Credit            |     
|--------------------------------|
| id  |  payid | type  |  mode   |
|-----|--------|-------|---------|
|  1  |   1    |   1   |   m1    |
|-----|--------|-------|---------|
|  2  |   1    |   4   |   m6    |
|-----|--------|-------|---------|
|  3  |   1    |   5   |   m5    |
|-----|--------|-------|---------|
|  4  |   1    |   1   |   m7    |
|-----|--------|-------|---------|

My code is as given below (For mapping and other details you can check THIS)

Payment tabletcstatus=new Payment();
    tabletcstatus.setName("name 123");
    session.save(tabletcstatus);


    Credit c=new Credit();
    c.setMode("mode 1");
    c.setPayid(tabletcstatus);
    session.save(c);


    Credit c1=new Credit();
    c1.setMode("mode 2");
    c1.setPayid(tabletcstatus);
    session.save(c1); 

But when i execute i'm getting the following executed

Hibernate: insert into hhh.payment (name) values (?)
Hibernate: insert into hhh.payment (name) values (?)
Hibernate: insert into credit (type, mode, payid) values (?, ?, ?)
Hibernate: insert into hhh.payment (name) values (?)
Hibernate: insert into credit (type, mode, payid) values (?, ?, ?)

here payment is inserted thrice and the two credit has different payid

can anyone please tell me some solution for this.

Community
  • 1
  • 1
  • 1
    You will have to show us the hibernate mappings. Thanks – K.C. Nov 08 '13 at 07:53
  • its there in http://stackoverflow.com/questions/19834618/does-hibernate-using-both-inheritance-and-hibernate-mapping-will-cause-any-drawb –  Nov 08 '13 at 08:35
  • I think inheritance is causing problem.try without inheritance – rajesh kakawat Nov 08 '13 at 08:45
  • sir but we need inheritance.....i'm using both inheritance and mapping..... can u please tell me the problem??? –  Nov 08 '13 at 08:47

1 Answers1

1

Well your mapping is somewhat 'exotic' in that you have Credit extends Payment and Payment also has a collection of credits. Not something I have come across but I guess, if this is really what you want, then it should work.

Regarding the first issue, how many entries would you expect in payments? You create three 'payments' 1 x Payment and 2 x Credits and are using a Joined Table strategy so 3 insertions is exactly right.

Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • but i need payment (parent) to be inserted once and for that id i need multiple credits (child) –  Nov 08 '13 at 08:54
  • Then revise your mappings. Remove the inheritance so that Payment has a collection of credits and Credit IS NOT as payment. 3 insertions to payment is exactly correct for your current mappings. – Alan Hay Nov 08 '13 at 09:00
  • sir can we do that using inheritance .......and without mapping....... still i'm in a bit confusion whether inheritance or mapping which is better...... –  Nov 08 '13 at 09:05