xgeoff wrote:
Rober2D2 wrote:
The problem in your original mapping file, is that you put a name and a class for the composite-id, which you shoul not (unless you want a separate class to keep the id).
I don't think that is true. I only posted one of the mapping files I tried. I tried many combinations, including the method that you suggest. I am certain that, per my post above, you MUST have both a name AND a class attribute. Is the model you posted something that you have actually gotten to work?
In that example I am using a key-many-to-one, because it is a relations table. Below, I put a working example, but before that, something I forgot about your code. If you use this:
Code:
<composite-id name="comp_id" class = "java.lang.String">
,you are telling hibernate to look for a field named comp_id, which is of the class java.lang.String, which has two fields inside (Hibernate can't find the field comp_id inside your class, so there is a parse error)
Apart from that, is a good idea to have a separate class for a composite primary key, but this is a design decision.
The working example.
Mapping file:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="example">
<class name="CompositeClass" table="COMPOSITE_CLASS">
<composite-id>
<key-property name="key1" type="string" column="KEY_1"/>
<key-property name="key2" type="string" column="KEY_2"/>
</composite-id>
<property name="data" column="DATA" type="string"/>
</class>
</hibernate-mapping>
The java class. Notice that it implements Serializable and overrides equals and hashCode. That is necessary for composite-id.
Code:
package example;
import java.io.Serializable;
public class CompositeClass implements Serializable {
private String key1;
private String key2;
private String data;
/**
* @return the key1
*/
public String getKey1() {
return key1;
}
/**
* @param key1 the key1 to set
*/
public void setKey1(String key1) {
this.key1 = key1;
}
/**
* @return the key2
*/
public String getKey2() {
return key2;
}
/**
* @param key2 the key2 to set
*/
public void setKey2(String key2) {
this.key2 = key2;
}
/**
* @return the data
*/
public String getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(String data) {
this.data = data;
}
@Override
public boolean equals(Object object) {
if (object == null) return false;
if (!(object instanceof CompositeClass)) return false;
CompositeClass compositeClass = (CompositeClass) object;
return this.toString().equals(compositeClass.toString());
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.key1 + "-" + this.key2;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
String cadena = this.toString();
return cadena.hashCode();
}
}
The java script:
Code:
package example;
import org.hibernate.Session;
import util.HibernateUtil;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Session sesion = HibernateUtil.getSessionFactory().openSession();
sesion.beginTransaction();
CompositeClass compositeClass = new CompositeClass();
compositeClass.setKey1("Key1");
compositeClass.setKey2("Key2");
compositeClass.setData("Data");
sesion.save(compositeClass);
sesion.getTransaction().commit();
sesion.close();
}
}
And the hibernate.cfg.xml file
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/example</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="example/CompositeClass.hbm.xml"/>
</session-factory>
</hibernate-configuration>