-->
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: ClassCastException
PostPosted: Tue Jul 27, 2004 5:42 am 
Beginner
Beginner

Joined: Sun Apr 04, 2004 8:45 am
Posts: 20
I want to make a simple example-application in which I have nine classes (Adresse, Person, Student, Anmeldung ... ). I wrote all mapping-files and I can create database (SchemaExport). Now, I want to save my objects ant it doesn't work. If I create Adresse- and Person-Object, it works. But with Student-Object I have the java.lang.ClassCastException. And the problem ist with the follwong part of the mapping-file for Student:

Code:
<!-- Liste der Anmeldungen -->
<set name="anmeldungListe" cascade="none" lazy="true">
   <key column="ANMELDUNG_ID" />
   <one-to-many class="ch.fhz.hsw.hibernate.model.Anmeldung"/>
</set>

Here is my mapping file Person.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
   <hibernate-mapping>
      <class name="ch.fhz.hsw.hibernate.model.Person" table="t_personen">
         <id name="personId" type="long" unsaved-value="null" >
            <column name="ID" sql-type="integer" not-null="true"/>
            <generator class="hilo"/>
         </id>
         <!-- Nachname -->
         <property name="nachname">
            <column not-null="true" sql-type="varchar(32)" name="NACHNAME"
               />
         </property>
         <!-- Vorname -->
         <property name="vorname">
            <column not-null="true" sql-type="varchar(32)" name="VORNAME" />
         </property>
         <one-to-one name="adresse"
            class="ch.fhz.hsw.hibernate.model.Adresse" cascade="all" />
            
         <!-- Unterklasse Dozent -->
         <joined-subclass name="ch.fhz.hsw.hibernate.model.Dozent"
            table="t_dozierende">
            <key column="ID" />
            <!-- Bidirektionale Associationen many-to-many -->
            <!-- Faecher -->
            <set name="faecherListe" table="t_dozierende_faecher"
               cascade="none" lazy="true">
               <key column="DOZENT_ID" />
               <many-to-many class="ch.fhz.hsw.hibernate.model.Fach"
                  column="FACH_ID" />
            </set>
            <!-- Aufsichten -->
            <set name="aufsichtenListe" table="t_dozierende_aufsichten"
               cascade="none" lazy="true">
               <key column="DOZIERENDE_ID" />
               <many-to-many class="ch.fhz.hsw.hibernate.model.Aufsicht"
                  column="AUFSICHT_ID" />
            </set>
         </joined-subclass>
         
         <!-- Unterklasse Student -->
         <joined-subclass name="ch.fhz.hsw.hibernate.model.Student"
            table="t_studierende">
            <key column="ID" />
            <!-- Liste der Anmeldungen -->
            <set name="anmeldungListe" cascade="none" lazy="true">
               <key column="ANMELDUNG_ID" />
               <one-to-many class="ch.fhz.hsw.hibernate.model.Anmeldung"/>
            </set>
         </joined-subclass>
      </class>
</hibernate-mapping>

Here is the mapping file Anmeldung.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="ch.fhz.hsw.hibernate.model.Anmeldung" table="t_anmeldungen">
      <id name="anmeldungId" type="long" unsaved-value="null" >
         <column name="ID" sql-type="integer" not-null="true"/>
         <generator class="hilo"/>
      </id>
      <!-- Anmeldungsdatum -->
      <property name="anmeldungDatum">
         <column name="ANMELDUNG_DATUM" sql-type="TIMESTAMP" />
      </property>
      <!-- Bidirektionale Assoziation 'Student' -->
      <many-to-one name="student" class="ch.fhz.hsw.hibernate.model.Student" cascade="none" />
   </class>
</hibernate-mapping>

Here are the java-files Student.java and Anmeldung.java:
Code:
package ch.fhz.hsw.hibernate.model;

import java.util.ArrayList;
import java.util.Collection;

public class Student extends Person
{
    private long matrikelNummer;
    private String studiengang;
    private int semester;
    private Collection anmeldungListe;

    public Student()
    {
        anmeldungListe = new ArrayList();
    }

    /**
     * @param adresse Adresse
     * @param nachname String
     * @param vorname String
     * @param matrikelNummer long
     * @param studiengang String
     * @param semester int
     */
    public Student(Adresse adresse, String nachname, String vorname,
            long matrikelNummer, String studiengang, int semester)
    {
        super(adresse, nachname, vorname);
        this.matrikelNummer = matrikelNummer;
        this.studiengang = studiengang;
        this.semester = semester;
        anmeldungListe = new ArrayList();
    }

    /**
     * @return matrikelNummer long
     */
    public long getMatrikelNummer()
    {
        return matrikelNummer;
    }

    /**
     * @param matrikelNummer long
     */
    public void setMatrikelNummer(long matrikelNummer)
    {
        this.matrikelNummer = matrikelNummer;
    }

    /**
     * @return semester int
     */
    public int getSemester()
    {
        return semester;
    }

    /**
     * @param semester int
     */
    public void setSemester(int semester)
    {
        this.semester = semester;
    }

    /**
     * @return studiengang String
     */
    public String getStudiengang()
    {
        return studiengang;
    }

    /**
     * @param studiengang String
     */
    public void setStudiengang(String studiengang)
    {
        this.studiengang = studiengang;
    }

    /**
     * @return anmeldungListe Collection
     */
    public Collection getAnmeldungListe()
    {
        return anmeldungListe;
    }

    /**
     * @param anmeldungListe Collection
     */
    public void setAnmeldungListe(Collection anmeldungListe)
    {
        this.anmeldungListe = anmeldungListe;
    }

    /**
     * @param anmeldung Anmeldung
     */
    public void addAnmeldung(Anmeldung anmeldung)
    {
        anmeldungListe.add(anmeldung);
    }

    /**
     * @param anmeldung Anmeldung
     * @return boolean
     */
    public boolean removeAnmeldung(Anmeldung anmeldung)
    {
        return ((ArrayList) anmeldungListe).remove(anmeldung);
    }
}


Code:

package ch.fhz.hsw.hibernate.model;

import java.util.GregorianCalendar;

import ch.fhz.hsw.hibernate.util.Util;

public class Anmeldung
{
    private Long anmeldungId;
    private GregorianCalendar anmeldungDatum;
    private Student student;

    public Anmeldung()
    {
    }

    /**
     * @param anmeldungDatum
     * @param student
     */
    public Anmeldung(GregorianCalendar anmeldungDatum, Student student)
    {
        this.anmeldungDatum = anmeldungDatum;
        this.student = student;
    }

    /**
     * @return anmeldungDatum GregorianCalendar
     */
    public GregorianCalendar getAnmeldungDatum()
    {
        return anmeldungDatum;
    }

    /**
     * @param anmeldungDatum GregorianCalendar
     */
    public void setAnmeldungDatum(GregorianCalendar anmeldungDatum)
    {
        this.anmeldungDatum = anmeldungDatum;
    }

    /**
     * @return anmeldungId Long
     */
    public Long getAnmeldungId()
    {
        return anmeldungId;
    }

    /**
     * @param anmeldungId Long
     */
    private void setAnmeldungId(Long anmeldungId)
    {
        this.anmeldungId = anmeldungId;
    }

    /**
     * @return student Student
     */
    public Student getStudent()
    {
        return student;
    }

    /**
     * @param student Student
     */
    public void setStudent(Student student)
    {
        this.student = student;
    }
}


And here is the method main:
Code:
public static void main(String[] args)
{
   Session session = null;
   Transaction tx = null;
   PrintStream out = System.out;

   try
   {
      session = HibernateUtil.currentSession();

      tx = session.beginTransaction();

      Adresse a1 = new Adresse("Pilatusstrasse", 12, 6002, "Luzern",
          "041 320 00 44", "rolf@tic.ch");

      session.save(a1);

      Student s1 = new Student(a1, "Pfister", "Markus", 668, "Betriebswirtschaft", 2);
      session.save(s1);

      tx.commit();
   }
   catch (HibernateException e)
   {
          System.err.println(e.toString());
   }

}


Ther stack trace is as follow:

Code:
7.07.2004 11:39:18 net.sf.hibernate.cfg.Environment <clinit>
INFO: Hibernate 2.1.4
27.07.2004 11:39:18 net.sf.hibernate.cfg.Environment <clinit>
INFO: loaded properties from resource hibernate.properties: {hibernate.connection.username=student, hibernate.connection.password=, hibernate.cglib.use_reflection_optimizer=true, hibernate.connection.pool_size=10, hibernate.dialect=net.sf.hibernate.dialect.PostgreSQLDialect, hibernate.connection.url=jdbc:postgresql://127.0.0.1:5432/adressen_db, hibernate.connection.driver_class=org.postgresql.Driver}
27.07.2004 11:39:18 net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: Adresse.hbm.xml
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: ch.fhz.hsw.hibernate.model.Adresse -> t_adressen
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: Person.hbm.xml
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: ch.fhz.hsw.hibernate.model.Person -> t_personen
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindJoinedSubclass
INFO: Mapping joined-subclass: ch.fhz.hsw.hibernate.model.Dozent -> t_dozierende
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindCollection
INFO: Mapping collection: ch.fhz.hsw.hibernate.model.Dozent.faecherListe -> t_dozierende_faecher
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindCollection
INFO: Mapping collection: ch.fhz.hsw.hibernate.model.Dozent.aufsichtenListe -> t_dozierende_aufsichten
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindJoinedSubclass
INFO: Mapping joined-subclass: ch.fhz.hsw.hibernate.model.Student -> t_studierende
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: Fach.hbm.xml
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: ch.fhz.hsw.hibernate.model.Fach -> t_faecher
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindCollection
INFO: Mapping collection: ch.fhz.hsw.hibernate.model.Fach.dozierendeListe -> t_dozierende_faecher
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: Aufsicht.hbm.xml
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: ch.fhz.hsw.hibernate.model.Aufsicht -> t_aufsichten
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindCollection
INFO: Mapping collection: ch.fhz.hsw.hibernate.model.Aufsicht.aufsichtPersonenListe -> t_dozierende_aufsichten
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: Klassenraum.hbm.xml
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: ch.fhz.hsw.hibernate.model.Klassenraum -> t_klassenraeume
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: Anmeldung.hbm.xml
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: ch.fhz.hsw.hibernate.model.Anmeldung -> t_anmeldungen
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: Pruefung.hbm.xml
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: ch.fhz.hsw.hibernate.model.Pruefung -> t_pruefungen
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-many association mappings
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: ch.fhz.hsw.hibernate.model.Student.anmeldungListe -> t_anmeldungen
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: ch.fhz.hsw.hibernate.model.Pruefung.klassenraumListe -> t_klassenraeume
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: ch.fhz.hsw.hibernate.model.Pruefung.aufsichtPersonen -> t_aufsichten
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: ch.fhz.hsw.hibernate.model.Pruefung.anmeldungListe -> t_anmeldungen
27.07.2004 11:39:18 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: ch.fhz.hsw.hibernate.model.Pruefung.faecherListe -> t_faecher
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-one association property references
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
27.07.2004 11:39:18 net.sf.hibernate.dialect.Dialect <init>
INFO: Using dialect: net.sf.hibernate.dialect.PostgreSQLDialect
27.07.2004 11:39:18 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use outer join fetching: true
27.07.2004 11:39:18 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
27.07.2004 11:39:18 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 10
27.07.2004 11:39:18 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost:5432/adressen_db
27.07.2004 11:39:18 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=student, password=, driver-class=org.postgresql.Driver}
27.07.2004 11:39:18 net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
27.07.2004 11:39:18 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use scrollable result sets: true
27.07.2004 11:39:18 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use JDBC3 getGeneratedKeys(): false
27.07.2004 11:39:18 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: false
27.07.2004 11:39:18 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
27.07.2004 11:39:18 net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: cache provider: net.sf.ehcache.hibernate.Provider
27.07.2004 11:39:18 net.sf.hibernate.cfg.Configuration configureCaches
INFO: instantiating and configuring caches
27.07.2004 11:39:18 net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
27.07.2004 11:39:19 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: no JNDI name configured
java.lang.ClassCastException
   at net.sf.hibernate.type.SetType.wrap(SetType.java:24)
   at net.sf.hibernate.impl.WrapVisitor.processArrayOrNewCollection(WrapVisitor.java:78)
   at net.sf.hibernate.impl.WrapVisitor.processCollection(WrapVisitor.java:49)
   at net.sf.hibernate.impl.AbstractVisitor.processValue(AbstractVisitor.java:69)
   at net.sf.hibernate.impl.WrapVisitor.processValues(WrapVisitor.java:93)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:920)
   at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
   at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:779)
   at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
   at ch.fhz.hsw.hibernate.test.TestClass.main(TestClass.java:46)
Exception in thread "main"


Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 10:58 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
It's throwing a ClassCastException because of the following:

Java Code in constructor:
Code:
anmeldungListe = new ArrayList();


Mapping:
Code:
<set name="anmeldungListe" cascade="none" lazy="true">
    <key column="ANMELDUNG_ID" />
    <one-to-many class="ch.fhz.hsw.hibernate.model.Anmeldung"/>
</set>


You are mapping the collection of Anmeldungs as a Set, but setting the underlying Collection to a List.

If you want the Anmeldungs to be ordered, then change your mapping to a list. If not, then change your Java code to initialise the collection to a something which implements the Set interface.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 4:12 am 
Beginner
Beginner

Joined: Sun Apr 04, 2004 8:45 am
Posts: 20
sdknott wrote:
It's throwing a ClassCastException because of the following:

Java Code in constructor:
Code:
anmeldungListe = new ArrayList();


Mapping:
Code:
<set name="anmeldungListe" cascade="none" lazy="true">
    <key column="ANMELDUNG_ID" />
    <one-to-many class="ch.fhz.hsw.hibernate.model.Anmeldung"/>
</set>


You are mapping the collection of Anmeldungs as a Set, but setting the underlying Collection to a List.

If you want the Anmeldungs to be ordered, then change your mapping to a list. If not, then change your Java code to initialise the collection to a something which implements the Set interface.


I changed set to list
Code:
<list name="anmeldungListe" cascade="none" lazy="true">
   <key column="ANMELDUNG_ID" />
   <index column="LIST_INDEX" type="integer" />
   <one-to-many class="ch.fhz.hsw.hibernate.model.Anmeldung"/>
</list>

and it works fine. Thanks for your help.


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.