-->
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.  [ 9 posts ] 
Author Message
 Post subject: Hibernate Annotation class inheriting from .hbm file
PostPosted: Mon Sep 21, 2009 3:42 pm 
Newbie

Joined: Mon Sep 21, 2009 3:28 pm
Posts: 3
Hi

I am having legacy code that uses Hibernate Mapping .hbm files written in XML. I want to extend one of this .hbm mapping file and add 4 new properties that will be mapped to new columns (We can do it using subclass tag and include the new properties).

But I would like to write a class that has this 4 new properties mapped to new columns using the annotations and at the same time inherit from a .hbm file, that has the properties mapping defined for the rest of the existing columns.

Is it possible to inherit the mappings from a hibernate file for an hibernate annotation class?

Thanks in Advance.


Top
 Profile  
 
 Post subject: Re: Hibernate Annotation class inheriting from .hbm file
PostPosted: Mon Sep 21, 2009 5:08 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi taks,

yes you can mix mapping files and annotations. Just extend the parent class mapped by the mapping file, annotate the fields you are adding within the child, and the class as @Entity. Then, add the annotated child class to your Hibernate configuration file.

You might run into some trouble depending on your inheritance strategy. But as long as no problems occur you don't have to worry about it.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Hibernate Annotation class inheriting from .hbm file
PostPosted: Thu Sep 24, 2009 11:44 am 
Newbie

Joined: Mon Sep 21, 2009 3:28 pm
Posts: 3
Hi Forestel

Thanks for your reply.

I followed the same steps that you have mentioned above for mixing annotations and .hbm mapping file. That is to:

1. Create a annotated class with new columns that extends the parent class defined in .hbm mapping file.

2. Add this new class to the list of the annotated classes.

The child class mapping that is done via annotations is getting picked up but not the one in the .hbm mapping file which is the base. It says that the value is null for existing columns.

I am defining my new class as below

@Entity
@table(name="parenttable")
@SequenceGenerator(name="requests_seq", sequenceName="REQUEST_SEQGEN")
public class childDto extends ParentDto

parent table has all the columns. childDto has the mapping for only the newly adding columns and .hbm has the mapping for ParentDto

Am I missing any step above.

Thanks in Advance


Top
 Profile  
 
 Post subject: Re: Hibernate Annotation class inheriting from .hbm file
PostPosted: Sun Sep 27, 2009 6:34 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi taks,

are you sure you told Hibernate to use both? Meaning, did you mention the mapping as well as the annotated child class in your Hibernate configuration?

If not so, could you post the mappings of the parent class and the code of the annotated class (fields and accessor methods should be sufficient).

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Hibernate Annotation class inheriting from .hbm file
PostPosted: Sun Sep 27, 2009 10:09 pm 
Newbie

Joined: Sun Sep 27, 2009 9:52 pm
Posts: 2
Hi,
It is interesting that I have exactly the same question. Let's say I have a legacy model called Person which uses .hbm mapping. Now I want a model called Applicant which extends Person using annotation. I am using org.hibernate.cfg.AnnotationConfiguration to load hibernate.cfg.xml

hibernate.cfg.xml is something similar to this:
Code:
<mapping resource="hibernate-mapping/Person.hbm.xml"/>
<mapping class="com.mycompany.model.Applicant"/>


Applicant.java is
Code:
@Entity
@Table(name="APPLICANT",)
public class Applicant extends Person{
}


If I set
Code:
<property name="hibernate.hbm2ddl.auto">create</property>
then Person is created fine, but I have problem with Applicant: "org.hibernate.AnnotationException: No identifier specified for entity: com.mycompany.model.Applicant". (I should not use @Id since Person already got a primary key).

What am I missing? Do I need somthing similar to @MappedSuperclass?
Thanks in advance


Top
 Profile  
 
 Post subject: Re: Hibernate Annotation class inheriting from .hbm file
PostPosted: Mon Sep 28, 2009 1:52 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi oppjinx,

I guess you will need the @PrimaryKeyJoinColumn annotation to specify the column where Hibernate should map the inherited identifier in the Applicant table.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Hibernate Annotation class inheriting from .hbm file
PostPosted: Tue Sep 29, 2009 5:43 pm 
Newbie

Joined: Mon Sep 21, 2009 3:28 pm
Posts: 3
Quote:
Are you sure you told Hibernate to use both? Meaning, did you mention the mapping as well as the annotated child class in your Hibernate configuration?

If not so, could you post the mappings of the parent class and the code of the annotated class (fields and accessor methods should be sufficient).


Hi Forestel

Yes I have configuration in place for both mapping file and annotation class.

Please note that I am using single table for both parent .hbm file and child annotated class. I am sure I am missing some tag here but I am not sure as which tag to use to inherit the properties from .hbm file.

My Hibernate mapping file is as below:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.xyz.person" >

  <class name="personDto" table="PERSON" abstract="true">

      <id name="reqSequence" column="REQ_SEQUENCE">
        <generator class="sequence">
        <param name="sequence">REQUEST_SEQGEN</param>
        </generator>
      </id>

     <discriminator column="REQUEST_TYPE"></discriminator>

      <property name="createdBy"     type="string"       column="CREATED_BY" not-null="true" />
      <property name="comment"             type="string"       column="COMMENT"/>
      <property name="type"         type="string"       column="REQUEST_TYPE" insert="false" update="false"  not-null="true"

/>
       <property name="closedDate"     type="timestamp"    column="CLOSED_DT"/>
  </class>
 
</hibernate-mapping>


My Annotated class is as follows:

Code:
@Entity
@Table(name = "PERSON")
public class UserDto extends PersonDto
{
  @Column(name = "ADDRESS")
  private String address;
  @Column(name = "DEPARTMENT")
  private String department;

  public UserDto()
  {
    super();
  }

  public String getAddress ()
  {
    return address;
  }
  public void setAddress(String address)
  {
    this.address = address;
  }
  public String getDepartment ()
  {
    return department;
  }
  public void setDepartment(String department)
  {
    this.department = department;
  }

}


Waiting for the reply.

Thanks in Advance


Top
 Profile  
 
 Post subject: Re: Hibernate Annotation class inheriting from .hbm file
PostPosted: Wed Sep 30, 2009 7:07 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi taks,

there is actually no need to explicitly map the discriminator column explicitly unless you require its values on the application side. If the latter is the case you should switch to content-based discrimination (see also Discriminator element) where you usually provide an SQL CASE WHEN ... END construct (refer to your DB manual if you are not acquainted with that).

I'm not really sure if you need to explicitly use the @Table annotation in the child class. Probably, you give it a try and run schema creation without it.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Hibernate Annotation class inheriting from .hbm file
PostPosted: Wed Sep 30, 2009 5:32 pm 
Newbie

Joined: Sun Sep 27, 2009 9:52 pm
Posts: 2
@Froestel: Thanks, I will try.


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