-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Frage zu Hibernate und Eumerations in Java 5
PostPosted: Sun Nov 19, 2006 2:45 pm 
Newbie

Joined: Sun Oct 22, 2006 9:28 am
Posts: 13
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 type
Code:
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 9:14 am 
Newbie

Joined: Sun Nov 26, 2006 10:57 pm
Posts: 4
Ich habe hierzu nur folgendes gefunden:

http://www.hibernate.org/265.html und
http://www.hibernate.org/272.html

Viele Grüße

Oliver


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 1:53 pm 
Newbie

Joined: Sun Nov 26, 2006 10:57 pm
Posts: 4
Auf dieser Seite gibt es insgesamt zwei Beispiele für die Persistierung von Enums.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.