I get the following exception during an insert, I think it says I am trying to include two records with the same primary key, but i think it is not true, every primary is different with the others.
Code:
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
at com.eliop.diana.model.manager.WsDiasFestivosManager.insertar(WsDiasFestivosManager.java:76)
at com.eliop.diana.services.ServicioConsultaCalendarioFestivo.invoke(ServicioConsultaCalendarioFestivo.java:43)
at com.eliop.diana.services.ServiciosTPLWSRecuperacionServiceClient.main(ServiciosTPLWSRecuperacionServiceClient.java:81)
These are the primary keys data, the last one is where it launches the exception:
[code]
2008,1, ,31/12/2007
2008,2, ,30/04/2008
2008,2, ,14/08/2008
[code]
This is the code:
[code]
public void insertar(CalenFesti[] consultaCalendarioFestivoReturn, String anio) {
Session sessionHibernate = HibernateSessionFactory.getSession();
Transaction transaction = sessionHibernate.beginTransaction();
try
{
eliminarAnio(sessionHibernate, anio);
transaction.commit();
for (CalenFesti diaFestivo : consultaCalendarioFestivoReturn) {
insertarDiaFestivo(sessionHibernate, diaFestivo, anio);
}
}
catch (HibernateException e)
{
transaction.rollback();
throw e;
}
}
public void insertarDiaFestivo(Session sessionHibernate, CalenFesti diaFestivo, String anio) {
Transaction transaction = sessionHibernate.beginTransaction();
String codCA = diaFestivo.getCodCA();
String codIsla = diaFestivo.getCodIsla();
Calendar[] fechas = diaFestivo.getFechas();
for (Calendar fecha : fechas) {
WsDiasFestivos wsDiasFestivos = new WsDiasFestivos();
WsDiasFestivosId wsDiasFestivosId = new WsDiasFestivosId();
wsDiasFestivosId.setAnio(anio);
wsDiasFestivosId.setCodCa(Long.valueOf(codCA));
wsDiasFestivosId.setCodIslaComarca(codIsla);
wsDiasFestivosId.setFecha(FechaUtil.getDDbarraMMbarraAAAA(fecha));
System.out.println(wsDiasFestivosId.getAnio()+wsDiasFestivosId.getCodCa().toString()+wsDiasFestivosId.getCodIslaComarca()+wsDiasFestivosId.getFecha());
wsDiasFestivos.setId(wsDiasFestivosId);
wsDiasFestivos.setCodSeq(Long.valueOf(FechaUtil.getAAAAMMDD(fecha)));
wsDiasFestivos.setFechaGbd(new Date());
sessionHibernate.save(wsDiasFestivos);
transaction.commit();
}
}
public void eliminarAnio(Session sessionHibernate, String anio) {
Query q = sessionHibernate.createQuery(
"delete "+
"from WsDiasFestivos "+
"where (anio = ?)");
q.setString(0, anio);
int rows = q.executeUpdate();
}
[/code]
And this is the cfg.xml file:
[code]
<?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">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.eliop.diana.model.WsDiasFestivos" table="WS_DIAS_FESTIVOS" schema="CENTAX">
<composite-id name="id" class="com.eliop.diana.model.WsDiasFestivosId">
<key-property name="anio" type="java.lang.String">
<column name="ANIO" length="4" />
</key-property>
<key-property name="codCa" type="java.lang.Long">
<column name="COD_CA" precision="2" scale="0" />
</key-property>
<key-property name="codIslaComarca" type="java.lang.String">
<column name="COD_ISLA_COMARCA" length="2" />
</key-property>
<key-property name="fecha" type="java.lang.String">
<column name="FECHA" length="40" />
</key-property>
</composite-id>
<property name="codSeq" type="java.lang.Long">
<column name="COD_SEQ" precision="8" scale="0" not-null="true" />
</property>
<property name="fechaGbd" type="java.util.Date">
<column name="FECHA_GBD" length="7" not-null="true" />
</property>
</class>
</hibernate-mapping>
[/code]
Thanks a lot by your help.
Joseba.