Hello, I have a problem using hibernate and spring in a web application.
Firts I will tell you some information about my configuration:
Hibernate-Version: 3.2.5.ga
Mapping documents:
Programes
<hibernate-mapping>
<class table="PROGRAMES" name="net.sabadell.programes.model.Programa">
<id name="id">
<generator class="native"/>
</id>
<version unsaved-value="negative" access="field" name="version"/>
<many-to-one lazy="false" name="area"/>
<many-to-one lazy="false" name="servei"/>
<property name="descripcio"/>
<property name="estat"/>
<set table="LINIES_ACTUACIO" optimistic-lock="false" access="field" lazy="false" cascade="all" name="liniesActuacio">
<key column="ID_PROGRAMA"/>
<composite-element class="net.sabadell.programes.model.LiniaActuacio">
<parent name="programa"/>
<many-to-one lazy="false" name="exercici"/>
<property name="nom" not-null="true" length="800"/>
<property name="dataAlta" not-null="true" access="field"/>
<property name="dataBaixa" access="field"/>
</composite-element>
</set>
<set table="OBJECTIUS" optimistic-lock="false" access="field" lazy="false" cascade="all" name="objectius">
<key column="ID_PROGRAMA"/>
<composite-element class="net.sabadell.programes.model.Objectiu">
<parent name="programa"/>
<many-to-one lazy="false" name="exercici"/>
<property name="nom" not-null="true" length="800"/>
<property name="dataAlta" not-null="true" access="field"/>
<property name="dataBaixa" access="field"/>
</composite-element>
</set>
<set table="RELACIO_INDICADORS" optimistic-lock="false" access="field" lazy="false" cascade="all" name="indicadors">
<key column="ID_PROGRAMA"/>
<composite-element class="net.sabadell.programes.model.RelacioProgramaIndicador">
<parent name="programa"/>
<many-to-one lazy="false" name="indicador"/>
</composite-element>
</set>
<set table="PROGRAMES_USUARIS" optimistic-lock="false" access="field" lazy="false" cascade="all" name="usuaris">
<key column="ID_PROGRAMA"/>
<many-to-many column="ID_USUARI" class="net.sabadell.programes.model.Usuari"/>
</set>
<property name="tipusPrograma"/>
<set table="ACOMPLIMENTS" optimistic-lock="false" access="field" lazy="false" cascade="all" name="acompliments">
<key column="ID_PROGRAMA"/>
<composite-element class="net.sabadell.programes.model.Acompliment">
<parent name="programa"/>
<many-to-one lazy="false" name="exercici"/>
<property name="nom"/>
<property name="dataAlta" not-null="true" access="field"/>
<property name="dataBaixa" access="field"/>
</composite-element>
</set>
<property name="nom" not-null="true" length="80" unique="true"/>
<property name="dataAlta" not-null="true" access="field"/>
<property name="dataBaixa" access="field"/>
</class>
</hibernate-mapping>
Indicador
<hibernate-mapping>
<class table="INDICADORS" name="net.sabadell.programes.model.Indicador">
<id name="id">
<generator class="native"/>
</id>
<version unsaved-value="negative" access="field" name="version"/>
<property name="unitat"/>
<property name="tipusExercici"/>
<set table="VALOR_EXERCICI" optimistic-lock="false" access="field" lazy="false" cascade="all" name="valorsExercici">
<key column="ID_INDICADOR"/>
<composite-element class="net.sabadell.programes.model.ValorExercici">
<parent name="indicador"/>
<many-to-one lazy="false" name="exercici"/>
<property name="valorPrevisio"/>
<property name="valorReal"/>
<property name="dataAlta" not-null="true" access="field"/>
<property name="dataBaixa" access="field"/>
</composite-element>
</set>
<property name="estat"/>
<property name="nom" not-null="true" length="80"/>
<property name="decimalNum"/>
<property name="dataAlta" not-null="true" access="field"/>
<property name="dataBaixa" access="field"/>
</class>
</hibernate-mapping>
Configuration:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for MagatzemsBenestar.
-->
<beans>
<!-- ========================= RESOURCE DEFINITIONS ========================= -->
<!-- Local C3P0 DataSource that works in any environment -->
<!-- See JPetStore for an example of using Apache Commons DBCP as alternative -->
<!-- (Both DataSource implementations have a "close" method to be called on shutdown) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="20"/>
<property name="loginTimeout" value="300"/>
<property name="maxIdleTime" value="3000"/>
<property name="maxStatements" value="50"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<value>classpath*:/net/sabadell/programes/model/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
</props>
</property>
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy"/>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
<!--
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
-->
<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->
<!--
- Transactional proxy for Pack Escoles's central data access object.
-
- Defines specific transaction attributes with "readOnly" markers,
- which is an optimization that is particularly valuable with Hibernate
- (to suppress unnecessary flush attempts for read-only operations).
-
- Note that in a real-life app with multiple transaction proxies,
- you will probably want to use parent and child bean definitions
- as described in the manual, to reduce duplication.
-->
<bean id="TxFactory" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="load*">PROPAGATION_REQUIRED</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
<prop key="execUpdate*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!--
- Pack Escoles's central data access object: Hibernate implementation.
-->
<bean id="programessabadelldao" parent="TxFactory">
<property name="target">
<bean class="net.sabadell.programes.model.extra.ProgramesSabadellDaoImpl">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
</property>
</bean>
<!-- Hibernate 3.0's JMX statistics service -->
<!-- Implements the StatisticsServiceMBean management interface -->
<bean name="programessabadell:type=HibernateStatistics" class="org.hibernate.jmx.StatisticsService">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
The generated SQL (show_sql=true):
Hibernate: select programa0_.id as id4_, programa0_.version as version4_, programa0_.area as area4_, programa0_.servei as servei4_, programa0_.descripcio as descripcio4_, programa0_.estat as estat4_, programa0_.tipus_programa as tipus7_4_, programa0_.nom as nom4_, programa0_.data_alta as data9_4_, programa0_.data_baixa as data10_4_ from programes programa0_ where programa0_.id=1
Hibernate: select area0_.id as id0_0_, area0_.version as version0_0_, area0_.descripcio as descripcio0_0_, area0_.nom as nom0_0_, area0_.data_alta as data5_0_0_, area0_.data_baixa as data6_0_0_ from arees area0_ where area0_.id=?
Hibernate: select servei0_.id as id10_0_, servei0_.version as version10_0_, servei0_.descripcio as descripcio10_0_, servei0_.nom as nom10_0_, servei0_.data_alta as data5_10_0_, servei0_.data_baixa as data6_10_0_ from serveis servei0_ where servei0_.id=?
Hibernate: select acomplimen0_.id_programa as id1_0_, acomplimen0_.exercici as exercici0_, acomplimen0_.nom as nom0_, acomplimen0_.data_alta as data4_0_, acomplimen0_.data_baixa as data5_0_ from acompliments acomplimen0_ where acomplimen0_.id_programa=?
Hibernate: select exercici0_.id as id1_0_, exercici0_.version as version1_0_, exercici0_.estat as estat1_0_, exercici0_.nom as nom1_0_, exercici0_.data_alta as data5_1_0_, exercici0_.data_baixa as data6_1_0_ from exercicis exercici0_ where exercici0_.id=?
Hibernate: select exercici0_.id as id1_0_, exercici0_.version as version1_0_, exercici0_.estat as estat1_0_, exercici0_.nom as nom1_0_, exercici0_.data_alta as data5_1_0_, exercici0_.data_baixa as data6_1_0_ from exercicis exercici0_ where exercici0_.id=?
Hibernate: select exercici0_.id as id1_0_, exercici0_.version as version1_0_, exercici0_.estat as estat1_0_, exercici0_.nom as nom1_0_, exercici0_.data_alta as data5_1_0_, exercici0_.data_baixa as data6_1_0_ from exercicis exercici0_ where exercici0_.id=?
Hibernate: select usuaris0_.id_programa as id1_1_, usuaris0_.id_usuari as id2_1_, usuari1_.id as id12_0_, usuari1_.version as version12_0_, usuari1_.admin as admin12_0_, usuari1_.consultor as consultor12_0_, usuari1_.nom as nom12_0_, usuari1_.data_alta as data6_12_0_, usuari1_.data_baixa as data7_12_0_ from programes_usuaris usuaris0_ left outer join usuaris usuari1_ on usuaris0_.id_usuari=usuari1_.id where usuaris0_.id_programa=?
Hibernate: select indicadors0_.id_programa as id1_0_, indicadors0_.indicador as indicador0_ from relacio_indicadors indicadors0_ where indicadors0_.id_programa=?
Hibernate: select indicador0_.id as id2_0_, indicador0_.version as version2_0_, indicador0_.unitat as unitat2_0_, indicador0_.tipus_exercici as tipus4_2_0_, indicador0_.estat as estat2_0_, indicador0_.nom as nom2_0_, indicador0_.decimal_num as decimal7_2_0_, indicador0_.data_alta as data8_2_0_, indicador0_.data_baixa as data9_2_0_ from indicadors indicador0_ where indicador0_.id=?
Hibernate: select indicador0_.id as id2_0_, indicador0_.version as version2_0_, indicador0_.unitat as unitat2_0_, indicador0_.tipus_exercici as tipus4_2_0_, indicador0_.estat as estat2_0_, indicador0_.nom as nom2_0_, indicador0_.decimal_num as decimal7_2_0_, indicador0_.data_alta as data8_2_0_, indicador0_.data_baixa as data9_2_0_ from indicadors indicador0_ where indicador0_.id=?
Hibernate: select indicador0_.id as id2_0_, indicador0_.version as version2_0_, indicador0_.unitat as unitat2_0_, indicador0_.tipus_exercici as tipus4_2_0_, indicador0_.estat as estat2_0_, indicador0_.nom as nom2_0_, indicador0_.decimal_num as decimal7_2_0_, indicador0_.data_alta as data8_2_0_, indicador0_.data_baixa as data9_2_0_ from indicadors indicador0_ where indicador0_.id=?
Hibernate: select indicador0_.id as id2_0_, indicador0_.version as version2_0_, indicador0_.unitat as unitat2_0_, indicador0_.tipus_exercici as tipus4_2_0_, indicador0_.estat as estat2_0_, indicador0_.nom as nom2_0_, indicador0_.decimal_num as decimal7_2_0_, indicador0_.data_alta as data8_2_0_, indicador0_.data_baixa as data9_2_0_ from indicadors indicador0_ where indicador0_.id=?
Hibernate: select indicador0_.id as id2_0_, indicador0_.version as version2_0_, indicador0_.unitat as unitat2_0_, indicador0_.tipus_exercici as tipus4_2_0_, indicador0_.estat as estat2_0_, indicador0_.nom as nom2_0_, indicador0_.decimal_num as decimal7_2_0_, indicador0_.data_alta as data8_2_0_, indicador0_.data_baixa as data9_2_0_ from indicadors indicador0_ where indicador0_.id=?
Hibernate: select indicador0_.id as id2_0_, indicador0_.version as version2_0_, indicador0_.unitat as unitat2_0_, indicador0_.tipus_exercici as tipus4_2_0_, indicador0_.estat as estat2_0_, indicador0_.nom as nom2_0_, indicador0_.decimal_num as decimal7_2_0_, indicador0_.data_alta as data8_2_0_, indicador0_.data_baixa as data9_2_0_ from indicadors indicador0_ where indicador0_.id=?
Hibernate: select indicador0_.id as id2_0_, indicador0_.version as version2_0_, indicador0_.unitat as unitat2_0_, indicador0_.tipus_exercici as tipus4_2_0_, indicador0_.estat as estat2_0_, indicador0_.nom as nom2_0_, indicador0_.decimal_num as decimal7_2_0_, indicador0_.data_alta as data8_2_0_, indicador0_.data_baixa as data9_2_0_ from indicadors indicador0_ where indicador0_.id=?
Hibernate: select indicador0_.id as id2_0_, indicador0_.version as version2_0_, indicador0_.unitat as unitat2_0_, indicador0_.tipus_exercici as tipus4_2_0_, indicador0_.estat as estat2_0_, indicador0_.nom as nom2_0_, indicador0_.decimal_num as decimal7_2_0_, indicador0_.data_alta as data8_2_0_, indicador0_.data_baixa as data9_2_0_ from indicadors indicador0_ where indicador0_.id=?
Hibernate: select indicador0_.id as id2_0_, indicador0_.version as version2_0_, indicador0_.unitat as unitat2_0_, indicador0_.tipus_exercici as tipus4_2_0_, indicador0_.estat as estat2_0_, indicador0_.nom as nom2_0_, indicador0_.decimal_num as decimal7_2_0_, indicador0_.data_alta as data8_2_0_, indicador0_.data_baixa as data9_2_0_ from indicadors indicador0_ where indicador0_.id=?
Hibernate: select valorsexer0_.id_indicador as id1_0_, valorsexer0_.exercici as exercici0_, valorsexer0_.valor_previsio as valor3_0_, valorsexer0_.valor_real as valor4_0_, valorsexer0_.data_alta as data5_0_, valorsexer0_.data_baixa as data6_0_ from valor_exercici valorsexer0_ where valorsexer0_.id_indicador=?
Hibernate: select valorsexer0_.id_indicador as id1_0_, valorsexer0_.exercici as exercici0_, valorsexer0_.valor_previsio as valor3_0_, valorsexer0_.valor_real as valor4_0_, valorsexer0_.data_alta as data5_0_, valorsexer0_.data_baixa as data6_0_ from valor_exercici valorsexer0_ where valorsexer0_.id_indicador=?
Hibernate: select valorsexer0_.id_indicador as id1_0_, valorsexer0_.exercici as exercici0_, valorsexer0_.valor_previsio as valor3_0_, valorsexer0_.valor_real as valor4_0_, valorsexer0_.data_alta as data5_0_, valorsexer0_.data_baixa as data6_0_ from valor_exercici valorsexer0_ where valorsexer0_.id_indicador=?
Hibernate: select valorsexer0_.id_indicador as id1_0_, valorsexer0_.exercici as exercici0_, valorsexer0_.valor_previsio as valor3_0_, valorsexer0_.valor_real as valor4_0_, valorsexer0_.data_alta as data5_0_, valorsexer0_.data_baixa as data6_0_ from valor_exercici valorsexer0_ where valorsexer0_.id_indicador=?
Hibernate: select valorsexer0_.id_indicador as id1_0_, valorsexer0_.exercici as exercici0_, valorsexer0_.valor_previsio as valor3_0_, valorsexer0_.valor_real as valor4_0_, valorsexer0_.data_alta as data5_0_, valorsexer0_.data_baixa as data6_0_ from valor_exercici valorsexer0_ where valorsexer0_.id_indicador=?
Hibernate: select valorsexer0_.id_indicador as id1_0_, valorsexer0_.exercici as exercici0_, valorsexer0_.valor_previsio as valor3_0_, valorsexer0_.valor_real as valor4_0_, valorsexer0_.data_alta as data5_0_, valorsexer0_.data_baixa as data6_0_ from valor_exercici valorsexer0_ where valorsexer0_.id_indicador=?
Hibernate: select valorsexer0_.id_indicador as id1_0_, valorsexer0_.exercici as exercici0_, valorsexer0_.valor_previsio as valor3_0_, valorsexer0_.valor_real as valor4_0_, valorsexer0_.data_alta as data5_0_, valorsexer0_.data_baixa as data6_0_ from valor_exercici valorsexer0_ where valorsexer0_.id_indicador=?
Hibernate: select valorsexer0_.id_indicador as id1_0_, valorsexer0_.exercici as exercici0_, valorsexer0_.valor_previsio as valor3_0_, valorsexer0_.valor_real as valor4_0_, valorsexer0_.data_alta as data5_0_, valorsexer0_.data_baixa as data6_0_ from valor_exercici valorsexer0_ where valorsexer0_.id_indicador=?
Hibernate: select valorsexer0_.id_indicador as id1_0_, valorsexer0_.exercici as exercici0_, valorsexer0_.valor_previsio as valor3_0_, valorsexer0_.valor_real as valor4_0_, valorsexer0_.data_alta as data5_0_, valorsexer0_.data_baixa as data6_0_ from valor_exercici valorsexer0_ where valorsexer0_.id_indicador=?
Hibernate: select objectius0_.id_programa as id1_0_, objectius0_.exercici as exercici0_, objectius0_.nom as nom0_, objectius0_.data_alta as data4_0_, objectius0_.data_baixa as data5_0_ from objectius objectius0_ where objectius0_.id_programa=?
Hibernate: select exercici0_.id as id1_0_, exercici0_.version as version1_0_, exercici0_.estat as estat1_0_, exercici0_.nom as nom1_0_, exercici0_.data_alta as data5_1_0_, exercici0_.data_baixa as data6_1_0_ from exercicis exercici0_ where exercici0_.id=?
Hibernate: select liniesactu0_.id_programa as id1_0_, liniesactu0_.exercici as exercici0_, liniesactu0_.nom as nom0_, liniesactu0_.data_alta as data4_0_, liniesactu0_.data_baixa as data5_0_ from linies_actuacio liniesactu0_ where liniesactu0_.id_programa=?
Hibernate: delete from relacio_indicadors where id_programa=? and indicador=?
Hibernate: delete from relacio_indicadors where id_programa=? and indicador=?
Hibernate: delete from relacio_indicadors where id_programa=? and indicador=?
Hibernate: delete from relacio_indicadors where id_programa=? and indicador=?
Hibernate: delete from relacio_indicadors where id_programa=? and indicador=?
Hibernate: delete from relacio_indicadors where id_programa=? and indicador=?
Hibernate: delete from relacio_indicadors where id_programa=? and indicador=?
Hibernate: delete from relacio_indicadors where id_programa=? and indicador=?
Hibernate: delete from relacio_indicadors where id_programa=? and indicador=?
Hibernate: insert into relacio_indicadors (id_programa, indicador) values (?, ?)
Hibernate: insert into relacio_indicadors (id_programa, indicador) values (?, ?)
Hibernate: insert into relacio_indicadors (id_programa, indicador) values (?, ?)
Hibernate: insert into relacio_indicadors (id_programa, indicador) values (?, ?)
Hibernate: insert into relacio_indicadors (id_programa, indicador) values (?, ?)
Hibernate: insert into relacio_indicadors (id_programa, indicador) values (?, ?)
Hibernate: insert into relacio_indicadors (id_programa, indicador) values (?, ?)
Hibernate: insert into relacio_indicadors (id_programa, indicador) values (?, ?)
Hibernate: insert into relacio_indicadors (id_programa, indicador) values (?, ?)
Hibernate: delete from valor_exercici where id_indicador=? and data_alta=?
Hibernate: insert into valor_exercici (id_indicador, exercici, valor_previsio, valor_real, data_alta, data_baixa) values (?, ?, ?, ?, ?, ?)
Hibernate: delete from valor_exercici where id_indicador=? and data_alta=?
Hibernate: insert into valor_exercici (id_indicador, exercici, valor_previsio, valor_real, data_alta, data_baixa) values (?, ?, ?, ?, ?, ?)
Hibernate: delete from valor_exercici where id_indicador=? and data_alta=?
Hibernate: delete from valor_exercici where id_indicador=? and data_alta=?
Hibernate: insert into valor_exercici (id_indicador, exercici, valor_previsio, valor_real, data_alta, data_baixa) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into valor_exercici (id_indicador, exercici, valor_previsio, valor_real, data_alta, data_baixa) values (?, ?, ?, ?, ?, ?)
Hibernate: delete from valor_exercici where id_indicador=? and data_alta=?
Hibernate: delete from valor_exercici where id_indicador=? and data_alta=?
Hibernate: delete from valor_exercici where id_indicador=? and data_alta=?
Hibernate: insert into valor_exercici (id_indicador, exercici, valor_previsio, valor_real, data_alta, data_baixa) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into valor_exercici (id_indicador, exercici, valor_previsio, valor_real, data_alta, data_baixa) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into valor_exercici (id_indicador, exercici, valor_previsio, valor_real, data_alta, data_baixa) values (?, ?, ?, ?, ?, ?)
Hibernate: delete from valor_exercici where id_indicador=? and data_alta=?
Hibernate: insert into valor_exercici (id_indicador, exercici, valor_previsio, valor_real, data_alta, data_baixa) values (?, ?, ?, ?, ?, ?)
Hibernate: delete from valor_exercici where id_indicador=? and data_alta=?
Hibernate: delete from valor_exercici where id_indicador=? and data_alta=?
Hibernate: insert into valor_exercici (id_indicador, exercici, valor_previsio, valor_real, data_alta, data_baixa) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into valor_exercici (id_indicador, exercici, valor_previsio, valor_real, data_alta, data_baixa) values (?, ?, ?, ?, ?, ?)
When I execute a hql query like this: "From Programa p where p.id = 1"
Hibernate uses the setters and the unparametrized constructor to obtain the Object "Programa".
If I'm using the web application, when hibernate is buiding the "Programa" object it does delete's and inserts's of my "relacio_indicadors" and "valor_exercici" relationship.
But if i'm using a JUnit test this troubles doesn't occurs.
What's the differents between JUnit Tests and Web Application. What's the problem with this configuration?
I thought than in some setter i modified the object, but if these happends it will occurs in a JUnit test too, or no?
Thanks.
My best regards.
Oriol Aguilar.
|