-->
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.  [ 10 posts ] 
Author Message
 Post subject: ont-to-many and Association references unmapped class error
PostPosted: Wed Jan 07, 2004 2:50 pm 
Newbie

Joined: Wed Jan 07, 2004 8:48 am
Posts: 11
Hi,

I'm using Middlegen to generate the mappings for hibernate, and then generate the classes with java net.sf.hibernate.tool.hbm2java.CodeGenerator, so no typo from my part.

As this is my first contact with Hibernate, I'm trying something not too difficult on a test db: I'm working on the table customer_status (with class CustomerStatu), which is only referenced from tables customers (class Customer).

I have a problem with this part of the mapping of customer_status (complete mapping below):

Code:
   <set
        name="customers"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="customer_status_id" />
        </key>
        <one-to-many
            class="custdb.hibernate.Customer"
        />
    </set>


By that I mean that if I remove it from the mapping file, it works perfectly.
I can retrieve a customer status and work on it:

CustomerStatu customer = (CustomerStatu) session.load(CustomerStatu.class, custId);

System.out.println("name of customerstatus =" +customer.getCustomerStatusName());

When I leave the set in the mapping I get:
Exception in thread "main" net.sf.hibernate.MappingException: Association references unmapped class: custdb.hibernate.Customer


This seems a frequent question but didn't find the fix corresponding to my situation (searched the forum, the FAQ, the reference docs, ...).

Thanks in advance for your help.

Raph



Customer Status mapping
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="custdb.hibernate.CustomerStatu"
    table="customer_status"
>

    <id
        name="customerStatusId"
        type="int"
        column="customer_status_id"
    >
        <generator class="assigned" />
    </id>

    <property
        name="customerStatusName"
        type="java.lang.String"
        column="customer_status_name"
        not-null="true"
        length="-1"
    />

    <!-- associations -->
    <!-- bi-directional one-to-many association to Customer -->
    <set
        name="customers"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="customer_status_id" />
        </key>
        <one-to-many
            class="custdb.hibernate.Customer"
        />
    </set>

</class>
</hibernate-mapping>


Customer mapping

Not sure it is needed, but here is is
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="custdb.hibernate.Customer"
    table="customers"
>   

    <id
        name="customerId"
        type="int"
        column="customer_id"
    >
        <generator class="assigned" />
    </id>

    <property
        name="name"
        type="java.lang.String"
        column="name"
        length="-1"
    />
    <property
        name="description"
        type="java.lang.String"
        column="description"
        length="-1"
    />
    <property
        name="vat"
        type="java.lang.String"
        column="vat"
        length="-1"
    />

    <!-- associations -->
    <!-- bi-directional many-to-one association to CustomerStatu -->
    <many-to-one
        name="customerStatu"
        class="custdb.hibernate.CustomerStatu"
        not-null="true"
    >
        <column name="customer_status_id" />
    </many-to-one>
    <set
        name="customers2accountManagers"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="customer_id" />
        </key>
        <one-to-many
            class="custdb.hibernate.Customers2accountManager"
        />
    </set>
    <!-- bi-directional one-to-many association to CustomerOrder -->
    <set
        name="customerOrders"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="customer_id" />
        </key>
        <one-to-many
            class="custdb.hibernate.CustomerOrder"
        />
    </set>
    <!-- bi-directional one-to-many association to Customers2contact -->
    <set
        name="customers2contacts"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="customer_id" />
        </key>
        <one-to-many
            class="custdb.hibernate.Customers2contact"
        />
    </set>
    <!-- bi-directional one-to-many association to Customers2address -->
    <set
        name="customers2addresss"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="customer_id" />
        </key>
        <one-to-many
            class="custdb.hibernate.Customers2address"
        />
    </set>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 9:11 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Are you sure your Customer mapping file gets loaded by Hibernate?

Please, show log Hibernate produces dueing mapping loading.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 5:43 am 
Newbie

Joined: Wed Jan 07, 2004 8:48 am
Posts: 11
I'm not sure the mappings are loaded correctly.
I don't have logs, and I'll look at how to get them working.
Here's the code I use:

Code:

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;


import net.sf.hibernate.MappingException;
import net.sf.hibernate.HibernateException;

import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;

import custdb.hibernate.CustomerStatu;
import custdb.hibernate.Customer;

class TestHb

{

    public static void main( String [] args)
        throws MappingException, HibernateException
    {
        Configuration cfg=new Configuration().addClass(CustomerStatu.class)addClass(Customer.class) ;

        SessionFactory sessions = cfg.buildSessionFactory();
        Session session = sessions.openSession();

        Integer custId = new Integer(4);
        CustomerStatu customer = (CustomerStatu) session.load(CustomerStatu.class, custId);

        System.out.println("name of customer = "+customer.getCustomerStatusName());

        session.close();


    }

}


Do I miss something for the logging to work?
I'm using the Sun JDK 1.4.2:
Code:
java -version
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)


I put the log4j.properties file in the same directory as my TestHb class, and log4j.jar is in the CLASSPATH.
It seems hibernate uses java logging with JDK 1.4+ (does it use over log4j or only if log4j not present?
I go look for the logs and hope I find them :-)

Raph


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 6:48 am 
Newbie

Joined: Wed Jan 07, 2004 8:48 am
Posts: 11
OK, I got the logging working.

For info, I couldn't get it working with log4j. It finally worked with the JDK logging by removing the log4j.jar from the CLASSPATH, and editing the $JAVA_HOME/jre/lib/logging.properties file.

HEre's what I get:

Code:
Jan 8, 2004 11:44:58 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: Hibernate 2.1.1
Jan 8, 2004 11:44:58 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.postgresql.Driver, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=net.sf.hibernate.cache.HashtableCacheProvider, hibernate.max_fetch_depth=1, hibernate.dialect=net.sf.hibernate.dialect.PostgreSQLDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.jdbc.batch_size=0, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=XXX, hibernate.connection.url=jdbc:postgresql://XXXXXXXXX/XXXXXXX, hibernate.connection.pool_size=1}
Jan 8, 2004 11:44:58 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: using java.io streams to persist binary types
Jan 8, 2004 11:44:58 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
Jan 8, 2004 11:44:58 AM net.sf.hibernate.cfg.Configuration addClass
INFO: Mapping resource: custdb/hibernate/CustomerStatu.hbm.xml
Jan 8, 2004 11:44:59 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: custdb.hibernate.CustomerStatu -> customer_status
Jan 8, 2004 11:45:00 AM net.sf.hibernate.cfg.Configuration addClass
INFO: Mapping resource: custdb/hibernate/Customer.hbm.xml
Jan 8, 2004 11:45:00 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: custdb.hibernate.Customer -> customers
Jan 8, 2004 11:45:00 AM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-many association mappings
Jan 8, 2004 11:45:00 AM net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: custdb.hibernate.CustomerStatu.customers -> customers
Exception in thread "main" net.sf.hibernate.MappingException: Association references unmapped class: custdb.hibernate.Customers2accountManager
        at net.sf.hibernate.cfg.Binder.bindCollectionSecondPass(Binder.java:1133)
        at net.sf.hibernate.cfg.Binder$CollectionSecondPass.secondPass(Binder.java:1327)
        at net.sf.hibernate.cfg.Binder$SecondPass.doSecondPass(Binder.java:1303)
        at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:589)
        at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:732)
        at TestHb.main(TestHb.java:26)




So it seems the Customer mapping is loaded.

Hoping someone can help :-)

Thanks.

Raph


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 7:04 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
You've mapped:
Code:
...
<one-to-many
            class="custdb.hibernate.Customers2accountManager"
/>


Where is the mapping for this class?

Justin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 9:41 am 
Newbie

Joined: Wed Jan 07, 2004 8:48 am
Posts: 11
drj wrote:
You've mapped:
Code:
...
<one-to-many
            class="custdb.hibernate.Customers2accountManager"
/>


Where is the mapping for this class?

Justin


Hmmm, my fault, I posted my message in a hurry before ging to a meeting and apparently hadn't read the message carefully.....

What I understand from this is that I have to
Code:
Configuration().addClass()

all related tables in my program, even those I don't actually use.
As all tables of the database are related, I'm wondering if I have to add all classes anyway. I think I'll discover it soon.

thanks to all for your advices and questions that helped me progress!

Raph


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 9:53 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
You don't have to map all objects to tables. Just custom objects that you reference in your existing mappings.
For example, you don't have to map a table if all you keep is foreign key as a property instead of the object itself in a relationship.

BTW you could put the configuration in an xml file:
Code:
<hibernate-configuration>
 

    <!-- a SessionFactory instance -->
    <session-factory>

      <!-- properties -->
      <property name="connection.datasource">java:comp/env/jdbc/DataSource</property>
      <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
      <property name="use_outer_join">true</property>
       
      <!-- Use ehcache as a caching tool -->
      <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</property>
       
      <!-- Mapping files -->      
      <mapping resource="Assessment.hbm.xml"/>      
      <mapping resource="Section.hbm.xml"/>      
      .....       
      
   </session-factory>
   
</hibernate-configuration>


and if you name if hibernate.cfg.xml just make sure it exists in the root of you classpath.

This is detailed in the reference manual section 3.6

Justin


Top
 Profile  
 
 Post subject: Facing same issue
PostPosted: Fri May 16, 2008 3:07 am 
Newbie

Joined: Mon Aug 06, 2007 10:00 am
Posts: 7
Hi,

Even I am facing the same issue with sql server 2005.
The strange part is, same code, same mapping and everything is working perfectly fine with Sybase database. It is happening now only with sqlserver from the time I have added one new class.
Were you able to solve your problem? let me know if any idea about my issue.

Thanks
Vishal


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 7:10 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
Did you put the reference of the new mapping file (for the new class) in the hibernate configuration file?

Code:
<mapping resource="filename.hbm.xml"/> 

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 19, 2008 2:53 am 
Newbie

Joined: Mon Aug 06, 2007 10:00 am
Posts: 7
Hi,
I was able to solve my prob. Actually based on database type my application is having different configuration file which I was not able to track. Later I found it and added the new class mapping in other mapping file also. its working fine now.
Thanks for the response.

Vishal


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.