• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help me .....Hibernate Expertise Required

 
Shahnawaz Siddiqui
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I try to do One table per subclass but I m getting this exception.I am not able to find root cause
Please help me
My class Mapping is fine

public class Book implements Serializable{
private int id;
private String name;
private String type;
private String price;
...

...}

import java.io.Serializable;

public class AnnEditionBook extends Book implements Serializable {
private int discount;

public int getDiscount() {
return discount;
}

public void setDiscount(int discount) {
this.discount = discount;
}

}

public class SpecialEditionBook extends Book implements Serializable {
/**
*
*/
private int cds;

public int getCds() {
return cds;
}

public void setCds(int cds) {
this.cds = cds;
}
}

Relation data Model
Table book1
id
name
type
price

table AnnEditionBook
id
discount

table SpecialEditionBook
id
cds


<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="com.shan.hibernate.inheritance.Book" table="book1" discriminator-value="book">
<id name="id" column="id" type="int"></id>
<discriminator column="offer_type" type="string"></discriminator>
<property name="name" column="name" type="string"></property>
<property name="type" column="type" type="string"></property>
<property name="price" column="price" type="string"></property>

<subclass name="AnnEditionBook" discriminator-value="ann" extends="com.shan.hibernate.inheritance.Book">
<property name="discount" column="discount" type="int"></property>
</subclass>
<subclass name="SpecialEditionBook" discriminator-value="splEnd" extends="com.shan.hibernate.inheritance.Book">
<property name="cds" column="cds" type="int"></property>
</subclass>
</class>
</hibernate-mapping>

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.shan.hibernate.granular.Address;
import com.shan.hibernate.granular.User;
import com.shan.hibernate.inheritance.AnnEditionBook;
import com.shan.hibernate.inheritance.SpecialEditionBook;


public class StudentClient {


public static Session getSessionForTrans(){
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
return session;
}

public void inherit(){
try{
Session session= StudentClient.getSessionForTrans();
Transaction tx=session.beginTransaction();
System.out.print("CCCCCCC");
com.shan.hibernate.inheritance.Book bk1=new com.shan.hibernate.inheritance.Book();
bk1.setId(2);
bk1.setName("shanBok");
bk1.setPrice("899");
bk1.setType("hacking");
System.out.print("CCCCCCC");
session.save(bk1);

AnnEditionBook anbk=new AnnEditionBook();
anbk.setDiscount(31);
session.save(anbk);
SpecialEditionBook sed=new SpecialEditionBook();
sed.setCds(12);
session.save(sed);
session.flush();
tx.commit();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}


}
public static void main(String[] args) {

StudentClient sc=new StudentClient();
//sc.update();
//sc.insert();
//sc.addComponent();
sc.inherit();
}

}

og4j:WARN Please initialize the log4j system properly.
java.lang.NullPointerException
at org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:87)
at org.hibernate.persister.entity.BasicEntityPersister.<init>(BasicEntityPersister.java:405)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:104)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:199)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1043)
at StudentClient.getSessionForTrans(StudentClient.java:18)
at StudentClient.inherit(StudentClient.java:91)
at StudentClient.main(StudentClient.java:123)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on this site.

For more information, please read this.

This post has been moved to a more appropriate forum.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic