• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please help me .....Hibernate Expertise Required

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Sheriff
Posts: 67748
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
This looks like a job for .... legal tender! It says so right in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic