1

I have this class

public class FolderOwner<T> { private T owner; }

where logically, T could be a User

public class User {
   private String id;
   private String lastName, firstName, middleName;
}

or a Department

public class Department {
   private long id;
   private String name;
}

Now, I have this class which has an attribute of an instance of FolderOwner

public class Folder {
   private FolderOwner owner;
   //other attributes
}

My question is how to map them in hibernate, considering those generic types in FolderOwner? I already read some answers on this forum, but they've done them using annotations. I'm quite new with Hibernate mapping and I prefer XML mapping than annotation momentarily.

My mapping for User

<class name="com.fileManagement.dataDesign.User" table="user">
    <id name="id" type="string" column="id"/>
    <property name="lastName" column="lastName" type="string" not-null="true"/>
    <property name="firstName" column="firstName" type="string"/>
    <property name="middleName" column="middleName" type="string"/>
</class>

My mapping for Department

<class name="com.fileManagement.dataDesign.Department" table="department">
    <id name="id" type="int" column="id">
        <generator class="native"/>
    </id>
    <property name="name" column="name" type="string"/>
</class>

Please help. Thanks.

1 Answer 1

0

If you want basic data fields, which are present in all tables use the @MappedSuperclass in you base class and extend the entity from there.

Update

There are different types of inheritance you can use with hibernate. See the excellent documentation at http://docs.jboss.org/hibernate/orm/3.5/reference/en/html/inheritance.html.

Apart from the inheritance of hibernate, there is also jpa on top with the @mappedsuperclass annotation. The advantage is that you super class does nit have to be an entity at all, and you can reuse everything you already annotated.

My use case were three identical tables (dimension1, dimension2, dimension3) that where referenced by another one. But you can think of many other good use cases like creation, deletion and update timestamps, onwer, createdBy and updatedBy fields...

All you have to do is to annotate the super class fields as you would normally do in you entity and extend the class.

Sign up to request clarification or add additional context in comments.

3 Comments

Please enlightened me. I haven't used inheritance herein.
Updated the answer. Code needed?
Thank you, but what I am tying to say is I have not used inheritance in my code. User and Department have no common base class (except Object off course). What I am trying to accomplish is I want Hibernate to map my FolderOwner<T> class, where T could be a type of User or Department. In my design a Folder class has a FolderOwner attribute. It means that instances of Folder class could have a FolderOwner<User> or FolderOwner<Department>. This thread stackoverflow.com/questions/7000428/… answered mostly, but they implement it using annotation. I want the XML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.