wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Mixing annotations and mapping while using inheritence strategies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Mixing annotations and mapping while using inheritence strategies" Watch "Mixing annotations and mapping while using inheritence strategies" New topic
Author

Mixing annotations and mapping while using inheritence strategies

Priya Venkatesan
Ranch Hand

Joined: Nov 02, 2002
Posts: 61
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

Joined: Nov 02, 2002
Posts: 61
Any thoughts or ideas? Please help.
Shailesh Kini
Ranch Hand

Joined: Oct 17, 2001
Posts: 153
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 ]

Shailesh Kini.
Priya Venkatesan
Ranch Hand

Joined: Nov 02, 2002
Posts: 61
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

Joined: Oct 17, 2001
Posts: 153
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mixing annotations and mapping while using inheritence strategies
 
Similar Threads
Dynamic SQL in hibernate
Join table with DetachedCriteria
Composite foreign key causing "object is not an instance of declaring class"?
help needed hibernate fetching strategy
Dynamic sql with hibernate