I'm relatively new to Hibernate. I can write and use some basic mappings, but I've got a question. The answer will help me relate better to Hibernate and hopefully learn how some others have addressed the same issue.
Let's say I wish to create a record of my music collection. The most natural breakdown for me was this:
Code:
public abstract class Recording{
Set discs;
Rating rating;
Set recordingArtists;
/* getters and setters */
}
public class Album extends Recording{
String albumName;
String releasedOnLabel;
int yearReleased;
/* getters and setters */
}
public class LiveShow extends Recording{
Location recordedAt;
Date recordedOn;
/* getters and setters */
}
Since I don't think in Hibernate yet, my original thought was to place the ID column in Recording and write the mapping file with <class> and <joined-subclass>.
Hibernate complains with this scheme because it cannot instantiate a Recording. That seems natural enough to me; why should Hibernate expect to persist a non-instantiable class?
In this toy problem, I just removed the abstract and it worked fine, but there's a real project I'm working with now where this will not be an option.
Is there any way to enforce non-instantiability of a class which will hold data that Hibernate will persist?This mapping file will not exactly correspond to the above, but the issue is the same.
Code:
<hibernate-mapping>
<class name="com.jimvanfleet.jtunes.Recording" table="recording">
<id name="recordingID" type="string" unsaved-value="null">
<column name="recordingID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<many-to-one name="rating"/>
<set name="disc" cascade="all">
<key column="recording"/>
<one-to-many class="com.jimvanfleet.jtunes.Disc"/>
</set>
<joined-subclass name="com.jimvanfleet.jtunes.Album" table="album">
<key column="recordingID"/>
<property name="albumName"/>
<property name="releaseYear"/>
<property name="bmgNum"/>
<property name="amazID"/>
</joined-subclass>
<joined-subclass name="com.jimvanfleet.jtunes.LiveShow" table="liveshow">
<key column="recordingID"/>
<property name="showDate"/>
<property name="showLocation"/>
<property name="recSource"/>
<property name="showCity"/>
</joined-subclass>
</class>
</hibernate-mapping>