Reputation: 46
First of all, I'm very new to JPA and Hibernate Annotations.
I have many systems and solutions developed in my current framework which is based on hibernate 3.5 and hbm.xml on Data Access Layer. Now I've decided to move to hibernate 4.1 and Annotation and get rid of hbm.xml files.
But here comes my problem.
I've a base class for ALL of my entities named GenericEntity which contains "id" and some other fields(like "version" and ...). This GenericEntity IS NOT mapped to any physical table directly and each entity is responsible to map these fields into physical table/column.
Now in Annotation, I couldn't find any way to omit id and version fields/annotations in entities
here are what I have:
public abstract class GenericEntity implements Serializable {
public static final short UNSAVED_VALUE = -1;
private long id = UNSAVED_VALUE;
private int version;
... getters and setters
}
public class User extends GenericEntity {
private String username;
private String password;
... getters and setters
}
and the hbm.xml is
<class name="my.User" table="TBL_USER" optimistic-lock="version">
<id name="id" column="USR_ID" unsaved-value="-1">
<generator class="identity"/>
</id>
<version name="version" column="USR_VERSION"/>
<property name="username" column="USR_USERNAME" type="string" />
<property name="password" column="USR_PASSWORD" type="string" />
</class>
there is no mapping for GenericEntity and there is no inheritance mapping for User about GenericEntity.
How Can I do that with annotation?!!
a solution is to define getId and setId as abstract methods, but I can not add id and version to tens of thousand of my entities.
also, I can not use hibernate inheritance for GenericEntity. because, I can not change my current tables easily and at the other hand it cost lots of penalty for performance.
also, I want to minimize change on other parts of my framework. GenericEntity is widely used in DAL.
Thanks
Meysam Te.
Upvotes: 0
Views: 1048
Reputation: 691635
You just need to annotate the GenericEntity class with @MappedSuperclass
, and each entity with @AttributeOverride(s)
.
The javadoc of the annotations explains it.
Upvotes: 4