-->
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.  [ 4 posts ] 
Author Message
 Post subject: Help on Inheritance
PostPosted: Mon Jun 14, 2004 6:53 am 
Newbie

Joined: Mon Apr 26, 2004 11:01 am
Posts: 12
Location: France
Hello,

I've read the documentation about inheritance, but it's still dark for me. For instance, I'd like to write the Hibernate mapping files of the 3 following classes :

Code:
public abstract class  CodeNom {
   
   protected String code;
   protected String libelle;

        public String getCode(){ return code;}
        public String getLibelle(){return libelle;}
        public void setCode(code){ this.code = code;}
        public void setLibelle(code){this.libelle = libelle;}

        public CodeNom(){
   }
}

public TypeCommande extends CodeNom{
   public TypeCommande() {
      super();
   }
}

public TypeFacture extends CodeNom{
   public TypeFacture() {
      super();
   }
}


I want to store each concrete class (TypeCommande and TypeFacture) in a single table of the database (TYPECDE and TYPEFAC). The attribute code is my primary key.
As the CodeNom class is abstract, I don't need to store it in the database.

Can someone tell me what is the mapping file of this example ?

Thanks a lot

_________________
Xavier MOGHRABI
http://www.enstimac.fr/~moghrabi


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 14, 2004 12:34 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Just don't bother with the abstract class in your mapping and map the "common" properties in the 2 <class> mappings.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 4:22 am 
Newbie

Joined: Mon Apr 26, 2004 11:01 am
Posts: 12
Location: France
Hi

But I wonder how can I write the mapping of an object referencing and abstract class like this one :

[url]
public class myObject {

private int id;
private String name;
private CodeNom codeNom;

public myObject() {
}

public int getId() {
return id:
}
public void setId(int id) {
this.id =id;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public CodeNom getCodeNom () {
return codeNom;
}
public void setCodeNom (CodeNom codeNom ) {
this.codeNom = codeNom;
}


}
[/url]

The attribute codeNom could be each an object TypeCommande, each an object TypeFacture.

How can I write the mapping if I didn't specify to hibernate the class CodeNom and just the two last concrete classes ?

_________________
Xavier MOGHRABI
http://www.enstimac.fr/~moghrabi


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 5:23 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
you should use the "table per subclass" method described in the documentation.

In your case:

Code:
<class name="CodeNom" table="CodeNom">
    <id name="code" type="string" column="code"/>
    <property name="libelle" column="libelle"/>

    <joined-subclass name="TypeCommande" table="TypeCommande">
        <key column="code"/>
    </joined-subclass>

    <joined-subclass name="TypeFacture" table="TypeFacture">
        <key column="code"/>
    </joined-subclass>
</class>


It's not very space efficient, but it allows you to have a FK reference to the CodeNom superclass.


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