-->
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: Simple mapping w/ composite-id raises duplicate import error
PostPosted: Mon Mar 01, 2004 5:41 pm 
Newbie

Joined: Fri Feb 20, 2004 10:59 am
Posts: 13
Location: Italy
Hi all,

I cannot map a very simple table with composite id. I hope someone can help me.

The table "User" has 3 columns:

Code:
PK_FIELD1    char(10)
PK_FIELD2    char(10)
NAME      char(10)


where (PK_FIELD1, PK_FIELD2) is the primary key.

I developed CompositeKey, a composite identifier, as described in chapter 7.4 of the hibernate reference. Here's the source code:

Code:
import java.io.Serializable;

public class CompositeKey implements Serializable{
   
   private String pk1;
   private String pk2;

   public String getPk1() {
      return pk1;
   }

   public String getPk2() {
      return pk2;
   }

   public void setPk1(String string) {
      pk1 = string;
   }

   public void setPk2(String string) {
      pk2 = string;
   }
   
   public boolean equals(CompositeKey pCk)
   {
      return (pCk.getPk1().equals(pk1) && pCk.getPk2().equals(pk2));
   }
   
   public int hashCode()
   {
      int hash=7;
      hash = 31 + pk1.hashCode();
      hash = 31*hash + pk2.hashCode();
      return hash;
   }
}


Then I wrote the User class to be mapped to the User table:

Code:
import java.io.Serializable;

public class User implements Serializable{

   private String name;
   private CompositeKey compId;
   
   
   public CompositeKey getCompId() {
      return compId;
   }

   public String getName() {
      return name;
   }

   public void setCompId(CompositeKey key) {
      compId = key;
   }

   public void setName(String string) {
      name = string;
   }

}


finally I mapped the class onto the table:

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>
    <class name="User" table="User">
            <composite-id name="compId" class="CompositeKey">
               <key-property name="pk1" column="PK_FIELD1" type="string" />
               <key-property name="pk2" column="PK_FIELD2" type="string" />
            </composite-id>
            <property name="nome" column="NAME" type="string"/>
    </class>
</hibernate-mapping>


Now when I startup tomcat+hibernate, I can't even make a query, cause the following instruction

Code:
Session session = HibernateUtil.currentSession();


raises the exception:

.....
... 27 more
Caused by: net.sf.hibernate.MappingException: Error reading resource: mapping/user.xml
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:318)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:976)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:928)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:856)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:842)
at HibernateUtil.<clinit>(HibernateUtil.java:10)
... 27 more
Caused by: net.sf.hibernate.MappingException: duplicate import: User
at net.sf.hibernate.cfg.Mappings.addImport(Mappings.java:85)
at net.sf.hibernate.cfg.Binder.bindClass(Binder.java:126)
at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:221)
at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1229)
at net.sf.hibernate.cfg.Configuration.add(Configuration.java:249)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:285)
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:315)
... 32 more

Has anyone got any clue?

Thanks.

Dario.

P.S: I'm using Hibernate 2.1.2 + MySql 4.0.17


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 4:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Do you have the User mapping defined multiple times in the configuration setup?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 4:39 am 
Newbie

Joined: Fri Feb 20, 2004 10:59 am
Posts: 13
Location: Italy
david wrote:
Do you have the User mapping defined multiple times in the configuration setup?


My hibernate.cfg.xml is the following

Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.datasource">java:comp/env/jdbc/testdb</property>
        <property name="show_sql">true</property>
        <property name="dialect">net.sf.hibernate.dialect.MSDEDialect</property>
        <property name="hibernate.cglib.use_reflection_optimizer">false</property>       
        <!-- Mapping files -->
        <mapping resource="mapping/user.xml"/>
    </session-factory>

</hibernate-configuration>


The only mapping file that I'm using, instead, is user.xml which I attached to my previous post.
I really don't understand what's wrong in my example, and it seems to me that the malfunction isn't due to the specific db I'm using (i.e. SQL Server) since the exception is raised when hibernate reads the mapping file.

Dario.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 4:48 am 
Newbie

Joined: Fri Feb 20, 2004 10:59 am
Posts: 13
Location: Italy
I may add that, though I've used a custom developed MSDEDialect (as cfg file shows) I get the same exception using the regular SQLServerDialect shipped with Hibernate 2.1.2.

Dario.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 11:19 am 
Newbie

Joined: Fri Feb 20, 2004 10:59 am
Posts: 13
Location: Italy
I finally solved this issue and I post the solution just in case someone was interested. The mistake was in the CompositeKey class, I implemented the equals() method using the wrong signature: the input parameter was a CompositeKey object instead of a generic Object. This was actually an overload of the equals() method and not an override. It is no surprise that hibernate could not work properly. My fault, sorry for bothering you.

dario


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.