• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mixing annotations and mapping while using inheritence strategies

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to annotations and would like to know if its possible to mix annotations with mappings while using inheritance strategies.

For (e.g) if I have a hbm mapping file say A.hbm.xml
Can I extend A and annotate it?

Specifically, can I do something like this:

@Entity
@Table(name="B")
@PrimaryKeyJoinColumn(name=B_id")
public class B extends A
{

@Column(name="newCol", nullable=true, length=50)
private String newCol;
...
}

class A does not have any annotations, just A.hbm

A.hbm:

<class name="com.my.object.A" >
...

<property name="name" column="name" ...>
</class>

Generally can I do something like this for any mapping strategy?

I read this line in the annotations docs and did not quiet understand:
"You cannot mix configuration strategies (hbm vs annotations) in a mapped entity hierarchy either"

Does this mean I cannot do what I am wanting to do?
 
Priya Venkatesan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any thoughts or ideas? Please help.
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Priya,

In my opinion from a maintenance perspective it is not feasible to have a mix of annotations and mappings file. I haven't seen developers build a liking to documentation yet and just imagine how difficult it would be for a newly hired developer to understand the mappings involved.

Leaving maintenance aside... you have the perfect example so I recommed that you use your fav IDE and start coding. It shouldn't take you more than a couple of hours to check it out for yourself and then share it with the rest of the ranchers. They will be grateful to you.
[ October 04, 2007: Message edited by: Shailesh Kini ]
 
Priya Venkatesan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shailesh,
Only after trying out I recognized hibernate's problem. Still uncertain about other alternatives, I raised this question in the forum.
Basically, when I do this I get the "java.lang.ExceptionInInitializerError" error and the detailed stackTrace specfies that "Id" needs to be specified for the subclass (which B).Since the inheritence strategy used is "table per subclass"(joined subclass), hibernate does not need to know about the PK for B. This gives me a feeling that hibernate is not recognizing B's parent class.

If I used mappings only, then the mapping for A would contain the joined-subclass element.
If I used annotations only, then A would have @Inheritence(strategy=InheritanceType.JOINED).
Since this information is missing by mixing annotations and mapping, hibernate is confused!


I guess this is what they are trying to convey in the docs:

"You cannot mix configuration strategies (hbm vs annotations) in a mapped entity hierarchy either."

I am not sure... :-)

Any thoughts is very much appreciated.
 
Shailesh Kini
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priya,

I am not a hibernate expert but... here are some of my thoughts.

Entity mapping can be done in 2 different ways...
1. Have a hbm mapping file.
2. Using annotations.

I am not sure which takes precedence of the other but imagine hibernate allowed both strategies to co-exist. In this case what if i have different mapping rules in hbm and annotations. Hibernate api developers would then have to figure out which rules to apply based on whether they have hbm or annotations.

Annotations can be applied at compile time, runtime etc... hbm files on the other hand have to be parsed at runtime. It can become increasingly complex to using both approaches together.

Maybe other ranchers can think of some more reasons.
reply
    Bookmark Topic Watch Topic
  • New Topic