Hibernate version:
3.2.0
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<typedef name="Source" class="com.oreilly.hh.Source">
<param name="enumClass">com.oreilly.hh.Source</param>
<param name="identifierMethod">toInt</param>
<param name="valueOfMethod">fromInt</param>
</typedef>
<class name="com.oreilly.hh.Track" table="TRACK">
<meta attribute="class-description">
Represents a single playable track in the music database.
@author Jim Elliot (with help from Hibernate)
</meta>
<id name="id" type="int" column="TRACK_ID">
<meta attribute="scope-set">protected</meta>
<generator class="native" />
</id>
<property name="title" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="TITLE" not-null="true" index="TRACK_TITLE"></column>
</property>
<property name="filePath" type="string" not-null="true" />
<property name="playTime" type="time">
<meta attribute="field-description">Playing time</meta>
</property>
<set name="artists" table="TRACK_ARTISTS" lazy="true">
<key column="TRACK_ID" />
<many-to-many class="com.oreilly.hh.Artist"
column="ARTIST_ID" />
</set>
<set name="comments" table="TRACK_COMMENTS">
<key column="TRACK_ID" />
<element column="COMMENT" type="string" />
</set>
<property name="added" type="date">
<meta attribute="field-description">
When the track was created
</meta>
</property>
<property name="volume" type="short">
<meta attribute="field-dexcription">
How loud to play the track
</meta>
</property>
<property name="source" type="Source">
<meta attribute="field-description">
Media on which track was obtained
</meta>
<meta attribute="use-in-tostring">true</meta>
</property>
</class>
<query name="com.oreilly.hh.tracksNoLongerThan">
<![CDATA[
from com.oreilly.hh.Track as track
where track.playTime <= :length
]]>
</query>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
// Create some data and persist it
tx = session.beginTransaction();
Track track = new Track("Russian Trance", "vol2/album610/track02.mp3", Time.valueOf("00:03:30"), new HashSet<Artist>(), new HashSet<String>(), new Date(), (short)0, Source.CD);
addTrackArtist(track, getArtist("PPK", true, session));
session.save(track);
// We're done; make our changes permanent
tx.commit();
} catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
Name and version of the database you are using:HSQLDB 1.8.0.7
My enum typeCode:
package com.oreilly.hh;
import java.io.Serializable;
public enum Source implements Serializable {
CASSETTE(0),
VINYL(1),
VHS(2),
CD(3),
BROADCAST(4),
DOWNLOAD(5),
STREAM(6);
private final int sourceType;
Source(int sourceType) {
this.sourceType = sourceType;
}
public int toInt() {
return sourceType;
}
public static Source fromInt(int sourceType) {
switch (sourceType) {
case 0:
return CASSETTE;
case 1:
return VINYL;
case 2:
return VHS;
case 3:
return CD;
case 4:
return BROADCAST;
case 5:
return DOWNLOAD;
case 6:
return STREAM;
default:
return null;
}
}
}
Hallo,
ich lerne Hibernate mit dem Buch "Hibernate: A developer's Notebook" von Jim Elliott. Das Buch basiert (obwohl ich es sehr gut finde) leider auf Grund des Alters auf Hibernate 2 und Java 1.4, so dass ich einige Quelltextbeispiele anpassen muss. Eine Stelle, an der es aber grundsätzlich hakt, sind Aufzählungstypen.
Ich habe mir eine Aufzählungstyp geschrieben, um einer Liedersammlung die Herkunft des Tracks zuzufügen. Leider erscheint in der Datenbank nicht die int-Repräsentation, sondern (vermutlich) die Rückgabe von Source.toString(). Was mache ich falsch?