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.  [ 5 posts ] 
Author Message
 Post subject: two mapped classes as compisite keys
PostPosted: Thu Jun 30, 2005 7:58 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
I must be missing something here and dont know what. What I want to do is pretty straight forward but it does'nt want to work. I have a Customer object and a Order Object and now I want to create a Customer_Order object that contains a Customer and a Order and another property. The customer and order is the key. So I tried to create a composite key with customer and order but that doesn't want to work. What am I doing wrong?

Mapping Files


Code:
<hibernate-mapping
>
    <class
        name="co.za.easypay.billPayment.Customer"
        table="customer"
    >
        <id
            name="customer_id"
            type="int"
            unsaved-value="null"
        >
        </id>
        <property
            name="customer_name"
            type="java.lang.String"
            update="true"
            insert="true"/>
    </class>

</hibernate-mapping>
<hibernate-mapping
>
    <class
        name="co.za.easypay.billPayment.Order"
        table="firsthibernate.order"
    >
        <id
            name="order_id"
            type="int"
            unsaved-value="null"
        >
        </id>
        <property
            name="order_number"
            type="java.lang.String"
            update="true"
            insert="true"
        />
    </class>
</hibernate-mapping>
<hibernate-mapping
>
    <class
        name="co.za.easypay.billPayment.Customer_Order"
        table="customer_order"
    >
        <composite-id>
            <key-many-to-one
                 name="customer" class="co.za.easypay.billPayment.Customer" column="customer_id"/>
            <key-many-to-one
                 name="order" class="co.za.easypay.billPayment.Order" column="order_id />
        </composite-id>       
        <property name="limit"></property>

    </class>

</hibernate-mapping>



Logs:
Quote:
(cfg.Environment 464 ) Hibernate 3.0.3
(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 1194) configuring from file: hibernate.cfg.xml
(cfg.Configuration 441 ) Mapping resource: co/za/easypay/billPayment/Customer_Order.hbm.xml
(cfg.HbmBinder 258 ) Mapping class: co.za.easypay.billPayment.Customer_Order -> customer_order
(cfg.HbmBinder 421 ) Could not perform validation checks for component as the class co.za.easypay.billPayment.Customer_Order was not found
(cfg.Configuration 441 ) Mapping resource: co/za/easypay/billPayment/Order.hbm.xml
(cfg.HbmBinder 258 ) Mapping class: co.za.easypay.billPayment.Order -> firsthibernate.order
(cfg.HbmBinder 835 ) Mapping class join: co.za.easypay.billPayment.Order -> customer_order
(cfg.Configuration 441 ) Mapping resource: co/za/easypay/billPayment/Customer.hbm.xml
(cfg.HbmBinder 258 ) Mapping class: co.za.easypay.billPayment.Customer -> customer
(cfg.HbmBinder 1206) Mapping collection: co.za.easypay.billPayment.Customer.orders -> customer_order
(cfg.Configuration 1272) Configured SessionFactory: null
(cfg.Configuration 852 ) processing extends queue
(cfg.Configuration 856 ) processing collection mappings
(cfg.Configuration 865 ) processing association property references
(cfg.Configuration 894 ) processing foreign key constraints
(connection.DriverManagerConnectionProvider 41 ) Using Hibernate built-in connection pool (not for production use!)
(connection.DriverManagerConnectionProvider 42 ) Hibernate connection pool size: 20
(connection.DriverManagerConnectionProvider 45 ) autocommit mode: false
(connection.DriverManagerConnectionProvider 80 ) using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/firsthibernate
(connection.DriverManagerConnectionProvider 86 ) connection properties: {user=root}
(cfg.SettingsFactory 72 ) RDBMS: MySQL, version: 4.1.7-nt
(cfg.SettingsFactory 73 ) JDBC driver: MySQL-AB JDBC Driver,version:mysql-connector-java-3.0.14-production( $Date: 2004/04/24 15:49:43 $, $Revision: 1.27.2.39 $ )
(dialect.Dialect 92 ) Using dialect: org.hibernate.dialect.MySQLDialect
(transaction.TransactionFactoryFactory 31 ) Using default transaction strategy (direct JDBC transactions)
(transaction.TransactionManagerLookupFactory 33 ) No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
(cfg.SettingsFactory 126 ) Automatic flush during beforeCompletion(): disabled
(cfg.SettingsFactory 130 ) Automatic session close at end of transaction: disabled
(cfg.SettingsFactory 137 ) JDBC batch size: 15
(cfg.SettingsFactory 140 ) JDBC batch updates for versioned data: disabled
(cfg.SettingsFactory 145 ) Scrollable result sets: enabled
(cfg.SettingsFactory 153 ) JDBC3 getGeneratedKeys(): enabled
(cfg.SettingsFactory 161 ) Connection release mode: null
(cfg.SettingsFactory 185 ) Maximum outer join fetch depth: 2
(cfg.SettingsFactory 188 ) Default batch fetch size: 1
(cfg.SettingsFactory 192 ) Generate SQL with comments: disabled
(cfg.SettingsFactory 196 ) Order SQL updates by primary key: disabled
(cfg.SettingsFactory 321 ) Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
(ast.ASTQueryTranslatorFactory 21 ) Using ASTQueryTranslatorFactory
(cfg.SettingsFactory 204 ) Query language substitutions: {}
(cfg.SettingsFactory 210 ) Second-level cache: enabled
(cfg.SettingsFactory 214 ) Query cache: disabled
(cfg.SettingsFactory 308 ) Cache provider: org.hibernate.cache.EhCacheProvider
(cfg.SettingsFactory 229 ) Optimize cache for minimal puts: disabled
(cfg.SettingsFactory 238 ) Structured second-level cache entries: enabled
(cfg.SettingsFactory 258 ) Echoing all SQL to stdout
(cfg.SettingsFactory 262 ) Statistics: disabled
(cfg.SettingsFactory 266 ) Deleted entity synthetic identifier rollback: disabled
(cfg.SettingsFactory 280 ) Default entity-mode: pojo
(impl.SessionFactoryImpl 151 ) building session factory
(impl.SessionFactoryObjectFactory 82 ) Not binding factory to JNDI, no JNDI name configured
(impl.SessionFactoryImpl 377 ) Checking 0 named queries
Hibernate: select customer_o0_.customer_id as customer1_, customer_o0_.order_id as order2_, customer_o0_.limit as limit0_ from customer_order customer_o0_
java.lang.ClassCastException
at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:726)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:284)
at org.hibernate.loader.Loader.doQuery(Loader.java:395)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:210)
at org.hibernate.loader.Loader.doList(Loader.java:1562)
at org.hibernate.loader.Loader.list(Loader.java:1545)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:840)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at co.za.easypay.billPayment.GetTest.main(GetTest.java:42)
Exception in thread "main"


code:
Code:
         tx = sess.beginTransaction();
         List ls =  sess.createQuery("from Customer_Order").list();
         Iterator it = ls.iterator();
         for(;it.hasNext();){
            Customer_Order cs= (Customer_Order)it.next();
            System.out.println(cs.getLimit());
            
         }
         


         //   Always Commit
         tx.commit();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 30, 2005 8:42 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 5:07 am
Posts: 33
Just add the follow to the <property name="limit">
type="java.lang.String"


Top
 Profile  
 
 Post subject: Nope
PostPosted: Thu Jun 30, 2005 8:59 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Nope, still not working. I think the problem lies within the <composite-id>, I tried the way they suggested in the documentation, where you actually create a class for the composite-id but that didnt work ether.


Top
 Profile  
 
 Post subject: Solved
PostPosted: Thu Jun 30, 2005 9:31 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
I found the problem. I had to implement Serializable and override hashcode() and equals().


Top
 Profile  
 
 Post subject: Re: two mapped classes as compisite keys
PostPosted: Thu Jun 11, 2009 5:52 pm 
Newbie

Joined: Mon Dec 15, 2008 1:31 pm
Posts: 4
How do you override hashcode and equals and which class???


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.