Finally, searching in the forums I got with the following mapping that seems to be correct but crashes with a Class Cast Exception when executing the inserts. I hope with this you can help me, because I'm mad with it.
Table usuario mapping
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.pruebaHibernate.logic.usuarios.to.TOUsuario" table="usuarios">
<id name="id" type="integer" column="id_usuario">
<generator class="sequence">
<param name="sequence">usuarios_id_usuario_seq</param>
</generator>
</id>
<property name="nombre" column="nombre" type="string"/>
<property name="apellidos" column="apellidos" type="string"/>
<property name="login" column="login" type="string"/>
<property name="password" column="password" type="string"/>
<set name="usuariosVehiculos" inverse="true" cascade="save-update">
<key column="id_usuario" not-null="true"/>
<one-to-many class="com.pruebaHibernate.logic.usuariosvehiculos.to.TOUsuariosVehiculos"/>
</set>
</class>
</hibernate-mapping>
Table vehiculo mapping
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.pruebaHibernate.logic.vehiculos.to.TOVehiculo" table="vehiculos">
<id name="id" type="integer" column="id_vehiculo">
<generator class="sequence">
<param name="sequence">vehiculos_id_vehiculo_seq</param>
</generator>
</id>
<property name="nombre" column="nombre" type="string"/>
<property name="matricula" column="matricula" type="string"/>
<set name="usuariosVehiculos" inverse="true" cascade="save-update">
<key column="id_vehiculo" not-null="true"/>
<one-to-many class="com.pruebaHibernate.logic.usuariosvehiculos.to.TOUsuariosVehiculos"/>
</set>
</class>
</hibernate-mapping>
Table usuariosvehiculos mapping
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.pruebaHibernate.logic.usuariosvehiculos.to.TOUsuariosVehiculos" lazy="true" table="usuarios_has_vehiculos">
<composite-id name="id" class="com.pruebaHibernate.logic.usuariosvehiculos.to.TOUsuariosVehiculos$Id">
<key-property name="idUsuario" type="integer" column="id_usuario"/>
<key-property name="idVehiculo" type="integer" column="id_vehiculo"/>
</composite-id>
<property name="fecha" type="date" column="fecha"/>
<many-to-one name="usuario" column="id_usuario" class="com.pruebaHibernate.logic.usuarios.to.TOUsuario" not-null="true" insert="false" update="false"/>
<many-to-one name="vehiculo" column="id_vehiculo" class="com.pruebaHibernate.logic.vehiculos.to.TOVehiculo" not-null="true" insert="false" update="false"/>
</class>
</hibernate-mapping>
Usuarios POJO implementaion
package com.pruebaHibernate.logic.usuarios.to;
import com.pruebaHibernate.logic.usuariosvehiculos.to.TOUsuariosVehiculos;
import java.util.Set;
/**
*
* @author Dani
*/
public class TOUsuario {
private Integer id;
private String nombre;
private String apellidos;
private String login;
private String password;
private TOUsuariosVehiculos usuariosVehiculos;
/** Creates a new instance of TOUsuario */
public TOUsuario() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellidos() {
return apellidos;
}
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public TOUsuariosVehiculos getUsuariosVehiculos() {
return usuariosVehiculos;
}
public void setUsuariosVehiculos(TOUsuariosVehiculos usuariosVehiculos) {
this.usuariosVehiculos = usuariosVehiculos;
}
}
Vehiculos POJO implementation
package com.pruebaHibernate.logic.vehiculos.to;
import com.pruebaHibernate.logic.usuariosvehiculos.to.TOUsuariosVehiculos;
public class TOVehiculo {
private Integer id;
private String nombre;
private String matricula;
private TOUsuariosVehiculos usuariosVehiculos;
public TOVehiculo() {}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getMatricula() {
return matricula;
}
public void setMatricula(String matricula) {
this.matricula = matricula;
}
public TOUsuariosVehiculos getUsuariosVehiculos() {
return usuariosVehiculos;
}
public void setUsuariosVehiculos(TOUsuariosVehiculos usuariosVehiculos) {
this.usuariosVehiculos = usuariosVehiculos;
}
}
UsuariosVehiculos POJO implementation
package com.pruebaHibernate.logic.usuariosvehiculos.to;
import com.pruebaHibernate.logic.usuarios.to.TOUsuario;
import com.pruebaHibernate.logic.vehiculos.to.TOVehiculo;
import java.util.Date;
/**
*
* @author Dani
*/
public class TOUsuariosVehiculos {
private TOUsuario usuario;
private TOVehiculo vehiculo;
private Date fecha;
private Id id;
public TOUsuariosVehiculos() {}
public TOUsuario getUsuario() {
return usuario;
}
public void setUsuario(TOUsuario usuario) {
this.usuario = usuario;
}
public TOVehiculo getVehiculo() {
return vehiculo;
}
public void setVehiculo(TOVehiculo vehiculo) {
this.vehiculo = vehiculo;
}
public Date getFecha() {
return fecha;
}
public void setFecha(Date fecha) {
this.fecha = fecha;
}
public Id getId() {
return id;
}
public void setId(Id id) {
this.id = id;
}
public static class Id {
private Long idUsuario;
private Long idVehiculo;
public Id() {}
public Long getIdUsuario() {
return idUsuario;
}
public void setIdUsuario(Long idUsuario) {
this.idUsuario = idUsuario;
}
public Long getIdVehiculo() {
return idVehiculo;
}
public void setIdVehiculo(Long idVehiculo) {
this.idVehiculo = idVehiculo;
}
}
}
And here is the code
TOUsuario toUsuario = new TOUsuario();
toUsuario.setNombre("Pedro");
toUsuario.setPassword("33434");
toUsuario.setLogin("Pedro");
toUsuario.setApellidos("Perez");
TOVehiculo toVehiculo = new TOVehiculo();
toVehiculo.setMatricula("XXXA");
toVehiculo.setNombre("Vehiculo Trabajo");
TOUsuariosVehiculos toUsuariosVehiculos = new TOUsuariosVehiculos();
toUsuariosVehiculos.setFecha(new Date());
toUsuariosVehiculos.setUsuario(toUsuario);
toUsuariosVehiculos.setVehiculo(toVehiculo);
toUsuario.setUsuariosVehiculos(toUsuariosVehiculos);
toVehiculo.setUsuariosVehiculos(toUsuariosVehiculos);
session.save(toUsuario);
tx.commit();
session.save(toVehiculo);
tx.commit();
session.save(toUsuariosVehiculos);
tx.commit();
I get this trace when performing the first session.save()
INFO [http-8084-Processor25] (Environment.java:460) - Hibernate 3.0.1
INFO [http-8084-Processor25] (Environment.java:478) - loaded properties from resource hibernate.properties: {hibernate.cglib.use_reflection_optimizer=true}
INFO [http-8084-Processor25] (Environment.java:506) - using CGLIB reflection optimizer
INFO [http-8084-Processor25] (Environment.java:536) - using JDK 1.4 java.sql.Timestamp handling
INFO [http-8084-Processor25] (Configuration.java:1160) - configuring from resource: /hibernate.cfg.xml
INFO [http-8084-Processor25] (Configuration.java:1131) - Configuration resource: /hibernate.cfg.xml
INFO [http-8084-Processor25] (Configuration.java:441) - Mapping resource: usuario.hbm.xml
INFO [http-8084-Processor25] (HbmBinder.java:258) - Mapping class: com.pruebaHibernate.logic.usuarios.to.TOUsuario -> usuarios
INFO [http-8084-Processor25] (Configuration.java:441) - Mapping resource: vehiculo.hbm.xml
INFO [http-8084-Processor25] (HbmBinder.java:258) - Mapping class: com.pruebaHibernate.logic.vehiculos.to.TOVehiculo -> vehiculos
INFO [http-8084-Processor25] (Configuration.java:441) - Mapping resource: usuariosvehiculos.hbm.xml
INFO [http-8084-Processor25] (HbmBinder.java:258) - Mapping class: com.pruebaHibernate.logic.usuariosvehiculos.to.TOUsuariosVehiculos -> usuarios_has_vehiculos
WARN [http-8084-Processor25] (HbmBinder.java:421) - Could not perform validation checks for component as the class com.pruebaHibernate.logic.usuariosvehiculos.to.TOUsuariosVehiculos$Id was not found
INFO [http-8084-Processor25] (Configuration.java:1272) - Configured SessionFactory: null
INFO [http-8084-Processor25] (Configuration.java:852) - processing extends queue
INFO [http-8084-Processor25] (Configuration.java:856) - processing collection mappings
INFO [http-8084-Processor25] (HbmBinder.java:1962) - Mapping collection: com.pruebaHibernate.logic.usuarios.to.TOUsuario.usuariosVehiculos -> usuarios_has_vehiculos
INFO [http-8084-Processor25] (HbmBinder.java:1962) - Mapping collection: com.pruebaHibernate.logic.vehiculos.to.TOVehiculo.usuariosVehiculos -> usuarios_has_vehiculos
INFO [http-8084-Processor25] (Configuration.java:865) - processing association property references
INFO [http-8084-Processor25] (Configuration.java:894) - processing foreign key constraints
INFO [http-8084-Processor25] (Dialect.java:91) - Using dialect: org.hibernate.dialect.PostgreSQLDialect
INFO [http-8084-Processor25] (SettingsFactory.java:91) - Default batch fetch size: 1
INFO [http-8084-Processor25] (SettingsFactory.java:95) - Generate SQL with comments: disabled
INFO [http-8084-Processor25] (SettingsFactory.java:99) - Order SQL updates by primary key: disabled
INFO [http-8084-Processor25] (SettingsFactory.java:285) - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
INFO [http-8084-Processor25] (ASTQueryTranslatorFactory.java:21) - Using ASTQueryTranslatorFactory
INFO [http-8084-Processor25] (SettingsFactory.java:107) - Query language substitutions: {}
INFO [http-8084-Processor25] (C3P0ConnectionProvider.java:50) - C3P0 using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost/PruebaHibernate
INFO [http-8084-Processor25] (C3P0ConnectionProvider.java:51) - Connection properties: {user=postgres, password=****}
INFO [http-8084-Processor25] (C3P0ConnectionProvider.java:54) - autocommit mode: false
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@1df5c7 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@9036e [ acquireIncrement -> 1, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, maxIdleTime -> 1800, maxPoolSize -> 20, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@17588d5 [ description -> null, driverClass -> null, factoryClassLocation -> null, jdbcUrl -> jdbc:postgresql://localhost/PruebaHibernate, properties -> {user=******, password=******} ] , preferredTestQuery -> null, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ] , factoryClassLocation -> null, numHelperThreads -> 3, poolOwnerIdentityToken -> 1df5c7 ]
INFO [http-8084-Processor25] (SettingsFactory.java:149) - JDBC batch size: 15
INFO [http-8084-Processor25] (SettingsFactory.java:152) - JDBC batch updates for versioned data: disabled
INFO [http-8084-Processor25] (SettingsFactory.java:157) - Scrollable result sets: enabled
INFO [http-8084-Processor25] (SettingsFactory.java:165) - JDBC3 getGeneratedKeys(): disabled
INFO [http-8084-Processor25] (TransactionFactoryFactory.java:31) - Using default transaction strategy (direct JDBC transactions)
INFO [http-8084-Processor25] (TransactionManagerLookupFactory.java:33) - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
INFO [http-8084-Processor25] (SettingsFactory.java:177) - Automatic flush during beforeCompletion(): disabled
INFO [http-8084-Processor25] (SettingsFactory.java:180) - Automatic session close at end of transaction: disabled
INFO [http-8084-Processor25] (SettingsFactory.java:186) - Second-level cache: enabled
INFO [http-8084-Processor25] (SettingsFactory.java:190) - Query cache: disabled
INFO [http-8084-Processor25] (SettingsFactory.java:272) - Cache provider: org.hibernate.cache.EhCacheProvider
INFO [http-8084-Processor25] (SettingsFactory.java:205) - Optimize cache for minimal puts: disabled
INFO [http-8084-Processor25] (SettingsFactory.java:214) - Structured second-level cache entries: enabled
INFO [http-8084-Processor25] (SettingsFactory.java:226) - Statistics: disabled
INFO [http-8084-Processor25] (SettingsFactory.java:230) - Deleted entity synthetic identifier rollback: disabled
INFO [http-8084-Processor25] (SettingsFactory.java:244) - Default entity-mode: pojo
INFO [http-8084-Processor25] (SessionFactoryImpl.java:147) - building session factory
WARN [http-8084-Processor25] (Configurator.java:126) - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: file:/C:/Documents and Settings/Dani/.netbeans/4.0/jakarta-tomcat-5.0.28_base/work/Catalina/localhost/Hibernate/loader/ehcache-failsafe.xml
INFO [http-8084-Processor25] (SessionFactoryObjectFactory.java:82) - Not binding factory to JNDI, no JNDI name configured
INFO [http-8084-Processor25] (SessionFactoryImpl.java:373) - Checking 0 named queries
java.lang.ClassCastException
java.lang.ClassCastException
at org.hibernate.type.SetType.wrap(SetType.java:39)
at org.hibernate.event.def.WrapVisitor.processArrayOrNewCollection(WrapVisitor.java:83)
at org.hibernate.event.def.WrapVisitor.processCollection(WrapVisitor.java:50)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:104)
at org.hibernate.event.def.WrapVisitor.processValue(WrapVisitor.java:97)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:58)
at org.hibernate.event.def.AbstractSaveEventListener.visitCollections(AbstractSaveEventListener.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:220)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:158)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:445)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:440)
at com.pruebaHibernate.logic.usuarios.bo.BOUsuario.setUsuario(BOUsuario.java:77)
at com.pruebaHibernate.web.actions.MainAction.insertData(MainAction.java:90)
at com.pruebaHibernate.web.actions.MainAction.execute(MainAction.java:37)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
Note the "Could not perform validation checks for component as the class com.pruebaHibernate.logic.usuariosvehiculos.to.TOUsuariosVehiculos$Id was not found" warning. I don't know if it's relationed with the cast exception.
Thanks in advance.
Dani.
|