-->
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.  [ 14 posts ] 
Author Message
 Post subject: Inheritance problem
PostPosted: Fri Sep 10, 2004 7:35 am 
Beginner
Beginner

Joined: Sun Aug 01, 2004 5:16 am
Posts: 22
Location: BERLIN
I got following example:

e.g:

Code:
package com.animals;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
* @hibernate.class   table = "Animal"
*                polymorphism="implicit"
*/
public class Animal {

   private long   eom;
   private String   name;
   /**
    *
    */
   public Animal() {
      super();
   }
   /**
    * @hibernate.id   column = "animal_eom"
    *          length = "4"
    *          type = "long"
    *          generator-class = "increment"
    */
   public long getEom() {
      return this.eom;
   }
   /**
    * @hibernate.property   
    *          column = "animal_name"
    *          length = "45"
    *          not-null = "true"
    *          type = "string"
    */
   public String getName() {
      return this.name;
   }
   public void setEom(long eom) {
      this.eom = eom;
   }
   public void setName(String name) {
      this.name = name;
   }
   
   public boolean equals(Object object) {
      if (!(object instanceof Animal)) {
         return false;
      }
      Animal rhs = (Animal) object;
      return new EqualsBuilder().append(
            this.eom, rhs.eom).append(this.name, rhs.name).isEquals();
   }
   public int hashCode() {
      return new HashCodeBuilder(-526618669, 1122276377).append(
            super.hashCode()).append(this.eom).append(this.name)
            .toHashCode();
   }
}


then:

Code:
package com.animals;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
* @hibernate.class   table = "Animal"
*
*/
public class Dog extends Animal{

   private String   color;
   /**
    *
    */
   public Dog() {
      super();
      // TODO Auto-generated constructor stub
   }
   /**
    * @hibernate.property   
    *          column = "animal_color"
    *          length = "45"
    *          not-null = "true"
    *          type = "string"
    */
   public String getColor() {
      return this.color;
   }
   public void setColor(String color) {
      this.color = color;
   }

   public boolean equals(Object object) {
      if (!(object instanceof Dog)) {
         return false;
      }
      Dog rhs = (Dog) object;
      return new EqualsBuilder().append(
            this.color, rhs.color).isEquals();
   }
   public int hashCode() {
      return new HashCodeBuilder(-900848193, 1510557323).append(
            super.hashCode()).append(this.color).toHashCode();
   }
}


result following files:

Code:
<?xml version="1.0"?>

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

<hibernate-mapping>
    <class
        name="com.animals.Animal"
        table="Animal"
        polymorphism="implicit"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="eom"
            column="animal_eom"
            type="long"
            length="4"
        >
            <generator class="increment">
            </generator>
        </id>

        <property
            name="name"
            type="string"
            update="true"
            insert="true"
            access="property"
            column="animal_name"
            length="45"
            not-null="true"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Animal.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


and

Code:
<?xml version="1.0"?>

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

<hibernate-mapping>
    <class
        name="com.animals.Dog"
        table="Animal"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="eom"
            column="animal_eom"
            type="long"
            length="4"
        >
            <generator class="increment">
            </generator>
        </id>

        <property
            name="color"
            type="string"
            update="true"
            insert="true"
            access="property"
            column="animal_name"
            length="45"
            not-null="true"
        />

        <property
            name="name"
            type="string"
            update="true"
            insert="true"
            access="property"
            column="animal_name"
            length="45"
            not-null="true"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Dog.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


if i now try following on a Session, it throws me a Class Cast Exception:

Code:
//this line works
Animal a=((Animal)Session.get(Animal.class,new Long(1));
System.out.println(a);
//this not and throws exception
Dog d=((Dog)Session.get(Dog.class,new Long(1));
System.out.println(a);


I think this is because Hibernate founds in cache the first loaded Object (Animal) and return it to me, and so its not possible to cast them
to a Dog .

but doing this:

Code:
Dog d=((Dog)Session.get(Dog.class,new Long(1));
System.out.println(a);
Animal a=((Animal)Session.get(Animal.class,new Long(1));
System.out.println(a);


it prints twice the Dog Object (hashCode is equal)...

do you have any idea why this is so?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 9:17 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
First of all you got a typo.
Dog d=((Dog)Session.get(Dog.class,new Long(1));
System.out.println(a); --> should be d ;)
Animal a=((Animal)Session.get(Animal.class,new Long(1));
System.out.println(a);

Then if you read the api doc of session. You'll see that get will init a proxy/instance of object and asocciates it with the session so your first approach will have an instance of Animal inside the proxy/session and not an instance of Animal. Dog extends Animal so a Dog is also an instance of Animal and not way round.
If you exchange the get method to load then both approaches should work.

_________________
regards

Olaf

vote if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 9:20 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
Please excuse my german posting. "Wer lesen kann ist klar im Vorteil" ;)

_________________
regards

Olaf

vote if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 10:41 am 
Beginner
Beginner

Joined: Sun Aug 01, 2004 5:16 am
Posts: 22
Location: BERLIN
maybe you didnt understand my problem....

i want to have following result,
- a lightweight-object
- a fullweight-object
- to create a list full of lightweight objects eg.Animal, mapped on a table in database with a pair of columns.
from this list , selecting an object should make the session load the corresponding fullweight-object with max load of column mappings of the same table.

my solution was to have a baseclass mapped to a table only with few columns, then create another class that extends the baseclass, and map more columns of that table.

did i something wrong?

using joined-subclass dindt resolve that prob...
thx for answering


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 10:44 am 
Beginner
Beginner

Joined: Sun Aug 01, 2004 5:16 am
Posts: 22
Location: BERLIN
Kaneda wrote:
Please excuse my german posting. "Wer lesen kann ist klar im Vorteil" ;)


willst mich bloss aergern ,hm...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 10:47 am 
Beginner
Beginner

Joined: Sun Aug 01, 2004 5:16 am
Posts: 22
Location: BERLIN
i changed the session- method to load(), didn't work anyway...any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 10:56 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
No but you gave the conclusion already in your post. "I think this is because Hibernate founds in cache the first loaded Object (Animal) and return it to me, and so its not possible to cast them
to a Dog . "
So why not just call load for getting the Dog.
From my point of view you should do a Session.get() of Animal-class and afterwards a Session.load() of Dog-class. Like below


Animal a=((Animal)Session.get(Animal.class,new Long(1));
System.out.println(a);
Dog d=((Dog)Session.load(Dog.class,new Long(1));
System.out.println(d);

_________________
regards

Olaf

vote if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 10:58 am 
Senior
Senior

Joined: Fri May 14, 2004 9:37 am
Posts: 122
Location: Cologne, Germany
Hmm any exception occuring ? Or is it the same CCE ?

_________________
regards

Olaf

vote if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 12:48 pm 
Beginner
Beginner

Joined: Sun Aug 01, 2004 5:16 am
Posts: 22
Location: BERLIN
nope, if i knew what i'm doing wrong...
do you think that that example i wrote should work?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 12, 2004 6:26 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Please pay attention to the last lines of the wiki page you've followed
http://www.hibernate.org/41.html

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 12, 2004 11:37 am 
Beginner
Beginner

Joined: Sun Aug 01, 2004 5:16 am
Posts: 22
Location: BERLIN
if that works , thx to you man!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 13, 2004 2:45 am 
Beginner
Beginner

Joined: Sun Aug 01, 2004 5:16 am
Posts: 22
Location: BERLIN
Kaneda wrote:
No but you gave the conclusion already in your post. "I think this is because Hibernate founds in cache the first loaded Object (Animal) and return it to me, and so its not possible to cast them
to a Dog . "
So why not just call load for getting the Dog.
From my point of view you should do a Session.get() of Animal-class and afterwards a Session.load() of Dog-class. Like below


Animal a=((Animal)Session.get(Animal.class,new Long(1));
System.out.println(a);
Dog d=((Dog)Session.load(Dog.class,new Long(1));
System.out.println(d);


wont work, only solution is to remove the first loaded object from session, thx anyway, but

Code:
Animal a=((Animal)Session.get(Animal.class,new Long(1));
System.out.println(a);
Sesson.evict(a);
Dog d=((Dog)Session.get(Dog.class,new Long(1));
System.out.println(d);

works,
so long...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 13, 2004 2:51 am 
Beginner
Beginner

Joined: Sun Aug 01, 2004 5:16 am
Posts: 22
Location: BERLIN
mapping this way resolves that problem also:

object a:
mapped to table "animal" (lower case)

object b:
mapped to table "ANIMAL" (upper case)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 13, 2004 1:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
drecksau wrote:
mapping this way resolves that problem also:

object a:
mapped to table "animal" (lower case)

object b:
mapped to table "ANIMAL" (upper case)

I do *not* recommand this hack. Hibernate forbid to have 1 row represented by multiple instances in Java at the same time for *very* good reasons

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 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.