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