Code:
package test;
import javax.ejb.*;
@Entity
@Table(name="CAT")
@Inheritance(strategy = InheritanceType.JOINED)
public class Cat {
private String id;
private String name;
private char sex;
private float weight;
private int age;
public Cat() {
}
@Id(generate=GeneratorType.IDENTITY)
@Column(name="CAT_ID")
public String getId() {
return id;
}
private void setId(String id) {
this.id = id;
}
@Column(name="name", nullable = false, length=16)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="gender")
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
@Column(name="weight")
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
@Column(name="age")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Code:
package test;
import javax.ejb.*;
@Entity
@Table(name="DCAT")
@Inheritance(strategy = InheritanceType.JOINED)
@InheritanceJoinColumn(name="DCatId")
public class DomesticCat extends Cat {
private String c;
@Column(name="c")
public String getC() {
return c;
}
protected void setC(String c) {
this.c=c;
}
}
Code:
package test;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class TestUtil {
private static Log log = LogFactory.getLog(TestUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure("/test/hibernate.cfg.xml")
.buildSessionFactory();
Session session = sessionFactory.openSession();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
public static Session currentSession() throws HibernateException {
Session s = session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
run the codes below
Code:
Session session = TestUtil.currentSession();
Transaction tx = session.beginTransaction();
System.out.println("Start!");
DomesticCat dcat = new DomesticCat();
dcat.setName("dCat");
dcat.setAge(10);
dcat.setSex('M');
dcat.setWeight((float) 7.6);
dcat.setC("haha");
session.save(dcat);
tx.commit();
[b]TestUtil.closeSession();
session = TestUtil.currentSession();[/b]
Query query = session.createQuery("select d from DomesticCat as d");
for (Iterator it = query.iterate(); it.hasNext();) {
Cat cat = (Cat) it.next();
System.out.println("*************************************************");
System.out.println("Cat: " + cat.getName() + cat.getSex());
if (cat instanceof DomesticCat) {
System.out.println("DomesticCat: " + ((DomesticCat) cat).getC());
}
}
TestUtil.closeSession();
the output just contains
"Cat: dCatM"
but no "DomesticCat: haha"
if I remove the bold codes, the "DomesticCat: haha" will be printed.