-->
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.  [ 3 posts ] 
Author Message
 Post subject: Modeling lightweight classes using annotation
PostPosted: Wed Jun 18, 2008 5:01 am 
Newbie

Joined: Thu Jun 12, 2008 1:00 am
Posts: 2
We are trying to model the lightweight class design pattern as mentioned in
http://www.hibernate.org/41.html using annotations.

This is how it is done in xml mapping as explained in the given URL.

Code:
<class name="DocumentInfo" table="DOCUMENTS">
   <id name="key" type="long" column="ID">
       <generator class="native"/>
   </id>
   <property name="name"/>
   <property name="created"/>
   <property name="updated"/>
   <many-to-one name="folder"/>
</class>

<class name="Document" table="DOCUMENTS" polymorphism="explicit">
   <id name="key" type="long" column="ID">
       <generator class="native"/>
   </id>
   <property name="name"/>
   <property name="created"/>
   <property name="updated"/>
   <many-to-one name="folder"/>
   <property name="text"/>
</class>

So what we did in annotation was as follows

Code:
@Entity
@Table(name = "objects")
@MappedSuperclass
public class SuperClass {
    @Id
    private Integer id;
    @Column(name = "super_property")
    private String superProperty;
..........  // omitting getters and setters for the properties
}

@Entity
@Table(name = "objects")
@org.hibernate.annotations.Entity(polymorphism = PolymorphismType.EXPLICIT)
public class SubclassOne extends SuperClass {
   @Column(name = "subclass_one_property")
    private String subclassOneProperty;
..........  // omitting getters and setters for the properties
}


In our case everything is working perfectly well. However, we have annotated
the superclass with both @MappedSupperClass and @Entity annotations.
Is it the correct way to model the lightweight class pattern in annotation?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 22, 2008 1:17 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I recommend to turn on Hibernate debug trace (especially SQL trace) and verify yourself whether the above mapping is doing what you want.

Why do you use @MappedSuperclass? did it not work without? It seems unnecessary to me. Have you tried @Inheritance(strategy=InheritanceType.SINGLE_TABLE) on SuperClass?

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 12:06 am 
Newbie

Joined: Thu Jun 12, 2008 1:00 am
Posts: 2
hardy.ferentschik wrote:
Hi,

Why do you use @MappedSuperclass? did it not work without? It seems unnecessary to me. Have you tried @Inheritance(strategy=InheritanceType.SINGLE_TABLE) on SuperClass?

--Hardy


The @Inheritance(strategy=InheritanceType.SINGLE_TABLE) annotation enforces a discriminator column in the corresponding database table.

In our case we are only saving the subclass in the database. The superclass "SuperClass" is only a lightweight version of "SubclassOne". We only use the superclass during data retrieval. Hence, there is nothing to discriminate and we did not use the @Inheritance(strategy=InheritanceType.SINGLE_TABLE) annotation.


By default Hibernate assumes that there is a single table inheritance relationship between a superclass and its subclass when both are annotated with the @Entity annotation. As a result of this default behavior Hibernate searches a column named "DTYPE"(default name of the discriminator column) if we want to perform any database operation that are related to our concerned classes.

When we added the @MappedSuperclass annotation in the superclass Hibernate stopped searching for the "DTYPE" column. We don't know why Hibernate is behaving in this way.


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