-->
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: Add a UserType to many-to-one association MappingException
PostPosted: Thu Feb 23, 2006 9:10 am 
Newbie

Joined: Mon May 30, 2005 5:52 am
Posts: 15
Hibernate version:[3.0]


Hi,
I have a many-to-one releationship that I point to a UserType and it give me and exception.

I have a CUSTOMER that has one CUSTOMER_LEVEL. The CUSTOMER_LEVEL is an Enumneration that I handle via a UserType in the same was as is specified on page 209 of HibernateInAction.

I get a mapping exception (below). I presume that this is becuase I must have the class mapped somehow. If its to the Hibernate.cfg.xml file I dont know how to do this. Can't seem to find out how and the HibernateInAction Book does not seem to suggest that this is required. Or I am doing somthing wrong.

Do I add this class to the

Here is the mapping.

<many-to-one
name="customerLevel"
class="com.merlin.common.data.hibernate.usertypes.CustomerLevelUserType"
cascade="save-update"
outer-join="true"
update="true"
insert="true"
column="CUSTOMER_LEVEL_ID"
/>




[b]Full stack trace of any exception that occurs:

Caused by: org.hibernate.MappingException: An association from the table CUSTOMER refers to an unmapped class: com.merlin.common.data.hibernate.usertypes.CustomerLevelUserType
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:968)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:921)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:999)
at com.merlin.common.HibernateSessionFactory.getSessionFactory(Unknown Source)
at com.merlin.server.dao.BaseDAO.find(Unknown Source)
at com.merlin.server.dao.hibernate.CustomerDAO.find(Unknown Source)
... 31 more


Top
 Profile  
 
 Post subject: Add a UserType to many-to-one association MappingException
PostPosted: Thu Feb 23, 2006 9:50 am 
Newbie

Joined: Mon May 30, 2005 5:52 am
Posts: 15
Here is the code for the CustomerLevelUser Type.

Code:
public class CustomerLevelUserType extends DefaultUserType
{
    private static final int[] SQL_TYPES={Types.VARCHAR};
    public int[] sqlTypes(){ return SQL_TYPES;}
    public Class returnedClass(){ return MakeType.class;}
    public boolean equals(Object x, Object y) { return x==y;}

    public int hashCode(Object o) throws HibernateException {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public Object deepCopy(Object value) {return value;}
    public boolean isMutable() { return false;}

    public Serializable disassemble(Object o) throws HibernateException {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public Object assemble(Serializable serializable, Object o) throws HibernateException {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public Object replace(Object o, Object o1, Object o2) throws HibernateException {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public abstract Object nullSafeGet(ResultSet resultSet,
                              String [] names,
                              Object owner)
    throws HibernateException, SQLException;

    public void nullSafeSet(PreparedStatement statement,
                              Object value,
                              int index)
    throws HibernateException, SQLException
    {
        if (value==null)
        {
            statement.setNull(index, Types.VARCHAR);
        } else
        {
            statement.setString(index, value.toString());
        }
    }

    public Object nullSafeGet(ResultSet resultSet,
                              String [] names,
                              Object owner)
    throws HibernateException, SQLException
    {
        String name=resultSet.getString(names[0]);
        return resultSet.wasNull() ? null : CustomerLevel.getInstance(name);
    }
}



Here is the code for the CustomerLevel Enumeration Class.

Code:
package com.merlin.common.data.hibernate.xdoclet;

import java.util.Map;
import java.util.HashMap;

/**
* Created by IntelliJ IDEA.
* User: richardwalsh
* Date: 14-Feb-2006
* Time: 09:35:15
* To change this template use File | Settings | File Templates.
*  @hibernate.class
*    table="CUSTOMER_LEVEL"
*/
public class CustomerLevel extends BaseDataType
{
    public static CustomerLevel NORMAL=new CustomerLevel("NORMAL");
    public static CustomerLevel BRONZE=new CustomerLevel("BRONZE");
    public static CustomerLevel SILVER=new CustomerLevel("SILVER");
    public static CustomerLevel GOLD=new CustomerLevel("GOLD");
    public static CustomerLevel PLATINUM=new CustomerLevel("PLATINUM");
    private static Map INSTANCES=new HashMap();

    static
    {
        INSTANCES.put(NORMAL.toString(), NORMAL);
        INSTANCES.put(BRONZE.toString(), BRONZE);
        INSTANCES.put(SILVER.toString(), SILVER);
        INSTANCES.put(GOLD.toString(), GOLD);
        INSTANCES.put(PLATINUM.toString(), PLATINUM);
    }

    private Long id;
    private String level;

    public CustomerLevel(String level)
    {
        this.level=level;
    }

    /**
     * @hibernate.id
     *  generator-class="native"
     *  column="customer_level_id"
     */
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    /**
     *  @hibernate.property
     *    column="LEVEL"
     *    type="com.merlin.common.data.hibernate.usertypes.CustomerLevelUserType"
     */
    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    Object readResolve()
    {
        return getInstance(level);
    }

    public static CustomerLevel getInstance(String name)
    {
        return (CustomerLevel)INSTANCES.get(name);
    }
}



Here is the Generated Customer.hbm.xml file that is included in the Hibernate.cfg.xml file. This as a maby-to-one assocication with the CustomerLevel Entity as can be seen in the mapping below.

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="com.merlin.common.data.hibernate.xdoclet.Customer"
        table="CUSTOMER"
    >

        <id
            name="id"
            column="customer_id"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Customer.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <many-to-one
            name="customerLevel"
            class="com.merlin.common.data.hibernate.usertypes.CustomerLevelUserType"
            cascade="save-update"
            outer-join="true"
            update="true"
            insert="true"
            column="CUSTOMER_LEVEL_ID"
        />

        <property
            name="customerStartDate"
            type="java.util.Date"
            update="true"
            insert="true"
            column="CUSTOMER_START_DATE"
        />

        <many-to-one
            name="contactDetails"
            class="com.merlin.common.data.hibernate.xdoclet.ContactDetails"
            cascade="save-update"
            outer-join="true"
            update="true"
            insert="true"
            column="CONTACT_DETAILS_ID"
        />

        <bag
            name="vehicles"
            lazy="false"
            inverse="true"
            cascade="none"
        >

            <key
                column="VEHICLE_ID"
            >
            </key>

            <one-to-many
                  class="com.merlin.common.data.hibernate.xdoclet.Vehicle"
            />

      </bag>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Customer.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>

As you can see the HBM file says that the class is the CustomerLevelUserType, but the class that generates the HBM file is the CustomerLevel class. Again this is loosly based on "Using Enumerated Types" on page 209 in Hibernate In Action.

Here is the Full Stack Trace.
Code:
(cfg.Environment                     464 ) Hibernate 3.0.5
(cfg.Environment                     477 ) hibernate.properties not found
(cfg.Environment                     510 ) using CGLIB reflection optimizer
(cfg.Environment                     540 ) using JDK 1.4 java.sql.Timestamp handling
(cfg.Configuration                   1110) configuring from resource: /hibernate.cfg.xml
(cfg.Configuration                   1081) Configuration resource: /hibernate.cfg.xml
(cfg.Configuration                   444 ) Mapping resource: com/merlin/common/data/hibernate/xdoclet/ContactDetails.hbm.xml
(cfg.HbmBinder                       260 ) Mapping class: com.merlin.common.data.hibernate.xdoclet.ContactDetails -> CONTACT_DETAILS
(cfg.Configuration                   444 ) Mapping resource: com/merlin/common/data/hibernate/xdoclet/Customer.hbm.xml
(cfg.HbmBinder                       260 ) Mapping class: com.merlin.common.data.hibernate.xdoclet.Customer -> CUSTOMER
(cfg.Configuration                   444 ) Mapping resource: com/merlin/common/data/hibernate/xdoclet/Vehicle.hbm.xml
(cfg.HbmBinder                       260 ) Mapping class: com.merlin.common.data.hibernate.xdoclet.Vehicle -> VEHICLE
(cfg.HbmBinder                       784 ) Mapping joined-subclass: com.merlin.common.data.hibernate.xdoclet.Car -> CAR
(cfg.HbmBinder                       784 ) Mapping joined-subclass: com.merlin.common.data.hibernate.xdoclet.Truck -> TRUCK
(cfg.Configuration                   444 ) Mapping resource: com/merlin/common/data/hibernate/xdoclet/CustomerLevel.hbm.xml
(cfg.HbmBinder                       260 ) Mapping class: com.merlin.common.data.hibernate.xdoclet.CustomerLevel -> CUSTOMER_LEVEL
(cfg.Configuration                   444 ) Mapping resource: com/merlin/common/data/hibernate/xdoclet/Garage.hbm.xml
(cfg.HbmBinder                       260 ) Mapping class: com.merlin.common.data.hibernate.xdoclet.Garage -> GARAGE
(cfg.HbmBinder                       1218) Mapping collection: com.merlin.common.data.hibernate.xdoclet.Garage.suppliers -> GARAGE_SUPPLIER
(cfg.Configuration                   444 ) Mapping resource: com/merlin/common/data/hibernate/xdoclet/Supplier.hbm.xml
(cfg.HbmBinder                       260 ) Mapping class: com.merlin.common.data.hibernate.xdoclet.Supplier -> SUPPLIER
(cfg.HbmBinder                       1218) Mapping collection: com.merlin.common.data.hibernate.xdoclet.Supplier.garages -> GARAGE_SUPPLIER
(cfg.Configuration                   444 ) Mapping resource: com/merlin/common/data/hibernate/xdoclet/Model.hbm.xml
(cfg.HbmBinder                       260 ) Mapping class: com.merlin.common.data.hibernate.xdoclet.Model -> MODEL
(cfg.Configuration                   444 ) Mapping resource: com/merlin/common/data/hibernate/xdoclet/ModelType.hbm.xml
(cfg.HbmBinder                       260 ) Mapping class: com.merlin.common.data.hibernate.xdoclet.ModelType -> MODEL_TYPE
(cfg.Configuration                   444 ) Mapping resource: com/merlin/common/data/hibernate/xdoclet/MakeType.hbm.xml
(cfg.HbmBinder                       260 ) Mapping class: com.merlin.common.data.hibernate.xdoclet.MakeType -> MAKE_TYPE
(cfg.Configuration                   444 ) Mapping resource: com/merlin/common/data/hibernate/xdoclet/BodyType.hbm.xml
(cfg.HbmBinder                       260 ) Mapping class: com.merlin.common.data.hibernate.xdoclet.BodyType -> BODY_TYPE
(cfg.Configuration                   444 ) Mapping resource: com/merlin/common/data/hibernate/xdoclet/EngineType.hbm.xml
(cfg.HbmBinder                       260 ) Mapping class: com.merlin.common.data.hibernate.xdoclet.EngineType -> ENGINE_TYPE
(cfg.Configuration                   1222) Configured SessionFactory: null
(cfg.Configuration                   875 ) processing extends queue
(cfg.Configuration                   879 ) processing collection mappings
(cfg.HbmBinder                       2041) Mapping collection: com.merlin.common.data.hibernate.xdoclet.Customer.vehicles -> VEHICLE
(cfg.HbmBinder                       2041) Mapping collection: com.merlin.common.data.hibernate.xdoclet.Garage.vehicles -> VEHICLE
(cfg.Configuration                   888 ) processing association property references
(cfg.Configuration                   917 ) processing foreign key constraints
java.lang.reflect.InvocationTargetException
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at com.merlin.server.test.DAOProxy.invoke(Unknown Source)
   at com.merlin.server.test.DAOProxy.find(Unknown Source)
   at com.merlin.server.test.BaseDAOTestCase.testFind(Unknown Source)
   at com.merlin.server.test.dao.CustomerDAOTestCase.testFindAll(Unknown Source)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at junit.textui.TestRunner.doRun(TestRunner.java:116)
   at com.intellij.rt.execution.junit2.IdeaJUnitAgent.doRun(IdeaJUnitAgent.java:57)
   at junit.textui.TestRunner.start(TestRunner.java:172)
   at com.intellij.rt.execution.junit.TextTestRunner2.startRunnerWithArgs(TextTestRunner2.java:23)
   at com.intellij.rt.execution.junit2.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:97)
   at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
Caused by: org.hibernate.MappingException: An association from the table CUSTOMER refers to an unmapped class: com.merlin.common.data.hibernate.usertypes.CustomerLevelUserType
   at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:968)
   at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:921)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:999)
   at com.merlin.common.HibernateSessionFactory.getSessionFactory(Unknown Source)
   at com.merlin.server.dao.BaseDAO.find(Unknown Source)
   at com.merlin.server.dao.hibernate.CustomerDAO.find(Unknown Source)
   ... 31 more



Top
 Profile  
 
 Post subject: Is it possible that this cannot be done
PostPosted: Thu Feb 23, 2006 10:26 am 
Newbie

Joined: Mon May 30, 2005 5:52 am
Posts: 15
Is it possible that you cannot map from a many-to-one to a UserType class. I did this again put this time instead of having the associated being many-to-one I just set it up as a property (see below) and it works.

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="com.merlin.common.data.hibernate.xdoclet.Customer"
        table="CUSTOMER"
    >

        <id
            name="id"
            column="customer_id"
            type="java.lang.Long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Customer.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="customerLevel"
            type="com.merlin.common.data.hibernate.usertypes.CustomerLevelUserType"
            update="true"
            insert="true"
            column="CUSTOMER_LEVEL_ID"
        />

        <property
            name="customerStartDate"
            type="java.util.Date"
            update="true"
            insert="true"
            column="CUSTOMER_START_DATE"
        />

        <many-to-one
            name="contactDetails"
            class="com.merlin.common.data.hibernate.xdoclet.ContactDetails"
            cascade="save-update"
            outer-join="true"
            update="true"
            insert="true"
            column="CONTACT_DETAILS_ID"
        />

        <bag
            name="vehicles"
            lazy="false"
            inverse="true"
            cascade="none"
        >

            <key
                column="VEHICLE_ID"
            >
            </key>

            <one-to-many
                  class="com.merlin.common.data.hibernate.xdoclet.Vehicle"
            />

      </bag>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Customer.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


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.