• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Inheritance Mapping Question !

 
Ramm Abhiram G
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This question is regardig 'Table per class hierarchy' mapping.

I have a class A.
I have a class B extends A.
I have only one table (say T1) in the database for both A and B.


class A {
private String a1;
private String a2;
private String a3;

// Setters and Getters here
}


class B extends A {
private String b1;
// Setters and Getters here
}


Table T1 has columns as follows:

A1_COL VARCHAR2(10) PK
A2_COL VARCHAR2(10)
A3_COL VARCHAR2(10)
B1_COL VARCHAR2(10)

Suppose T1 has a row like this:

100 200300null


<hibernate-mapping>
<class name="foo.A" discriminator-value="0" table="T1">

<id name="id" column="A1_COL">
<generator class="sequence">
<param name="sequence">SEQ_A1</param>
</generator>
</id>

<discriminator column="DISCRIM_A1" />
............
............

<subclass name="foo.B" discriminator-value="17">
<property name="b1" column="B1_COL" type="string"/>
</subclass>
</class>
</hibernate-mapping>



Using Hibernate program I need to do the following task:

1) Retrieve the row based on primary key (100).
2) Set "B1_COL" value to 500 instead of null.
3) Update the database with new values.

I mean the above program should result in the following row in Database.

100 200300500


Appreciate your quick help.
This might be simple question, but i am new to Hibernate, so kindly help me.

Thanks n Have a nice day,
Abhiram
 
Ramm Abhiram G
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

If you can please provide me some sample code for the same.

Thanks,
Abhiram
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be aware that JavaRanch is NotACodeMill and you are encouraged to DoYourOwnHomework.

What have you managed so far? Where are you stuck?
[ October 16, 2008: Message edited by: Paul Sturrock ]
 
Ramm Abhiram G
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Basically I am writing a JUnit testcase.
I got struked while writing update method.
Because I am not sure how retrieve class 'B' reference and update the "B1_COL" value.
I wrote my code as follows:

public void testUpdate() throws Exception {
try {
txnService.begin("test");
B b;
A a = (A)service.findById(new String("100"), A.class); // service will take care of session object

String a1Id = a.getA1();
System.out.println("a1Id------>"+a1Id);

// Now I am in doubt how to get class "B" and update "B1_COL" value. However i wrote like this which is not working.

b = new B();
b.setB1Id(new String ("500"));
service.reattach(b);

b =(B)service.findById(a1Id,B.class);
assertNotNull(b);
System.out.println("bId------>"+b.getB1());

txnService.commit();
} catch(Exception e) {
e.printStackTrace();
}
}

Thanks,
Abhiram
 
deepak adlakha
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a quick question, if you have one table only..why you need two classes, why can't you work with single class only ?
 
Ramm Abhiram G
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. We can work with single class also.
But some of the information is subset of the actual information.
Suppose you have a Person class. There will be an address for each person.
So we can define one Address class and wrap it in Person class.
It's normal practise in programming.

Hope i am clear.

Thanks,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic