-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: about retrieve inheritance
PostPosted: Mon Mar 14, 2005 4:51 am 
Newbie

Joined: Mon Mar 14, 2005 4:35 am
Posts: 5
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

I use Hibernate to retrieve the inheritance. Such as class SubClass1 and SubClass2 extends class Parent. I have tried <subclass> with discriminator and <joined-subclass>. When I use such query:
select a from Parent as a.
all the result types are Parent, not any sub classes.

But hibernate 2 can convert the query results into SubClass1 or SubClass2 with the same mapping file and program.

Is it a bug of hibernate 3.0 rc1?

does anyone have solutions?

Thanks

Hibernate version:
3.0 RC1
Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 18, 2005 5:07 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
REcheck your code, you probably hve done something wrong

_________________
Emmanuel


Top
 Profile  
 
 Post subject: This is my code
PostPosted: Mon Mar 21, 2005 10:49 pm 
Newbie

Joined: Mon Mar 14, 2005 4:35 am
Posts: 5
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 25, 2005 1:00 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
By default classes are lazy. Have a look at the proxy limitations in the reference documentation.
Quick solve: set your class as lazy=false.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 28, 2005 2:56 am 
Newbie

Joined: Mon Mar 14, 2005 4:35 am
Posts: 5
set lazy=false require define proxy. How do I define it?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 28, 2005 3:33 am 
Newbie

Joined: Mon Mar 14, 2005 4:35 am
Posts: 5
Thank you, it works! :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.