Sorry for this post in french.
I Know it's french user forum .
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelpBonjour,
J'ai un problème sur des chargements tardif et sur des updates intempestifs.
Le modèle objet :
un Patient peut être associé à 1 sejour_courant, 1 sejour peut être associé à 0 ou * Contantes, et 1 Contante à un 1 User.
Quand je fecth un patient, son attribut sejour_courant (si il existe) doit être contsruit par une jointure, la List des constantes qui est contenu dans le sejour ne devrait par être fecther (lazy="true") alors que c'est le cas.
De plus quant j'insère une constante, hibernate génère un insert et 1 update sur la table MED_constante, alors que seule un insert et suffisant.
Je pense que j'ai pas tout saisi sur le mapping et ou les API.
Merci
Eric
Hibernate version: 3.1.rc2
Mapping documents:Patient :
<?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.medescan.proto.model.Patient"
table="MED_PATIENT"
>
<id
name="id"
column="PATIENT_ID"
type="long"
>
<generator class="increment">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Patient.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="IPP"
type="java.lang.String"
update="true"
insert="true"
column="IPP"
length="12"
unique="true"
/>
<property
name="nom"
type="java.lang.String"
update="true"
insert="true"
column="NOM"
length="80"
/>
<property
name="prenom"
type="java.lang.String"
update="true"
insert="true"
column="PRENOM"
length="80"
/>
<property
name="dateNais"
type="date"
update="true"
insert="true"
column="DTNAIS"
/>
<property
name="sexe"
type="java.lang.String"
update="true"
insert="true"
column="SEXE"
length="1"
/>
<property
name="service"
type="java.lang.String"
update="true"
insert="true"
column="SEVICE"
length="12"
/>
<property
name="chambre"
type="java.lang.String"
update="true"
insert="true"
column="CHAMBRE"
length="4"
/>
<many-to-one
name="sejourCourrant"
class="com.medescan.proto.model.Sejour"
column="SEJOUR_ID"
unique="true"
lazy="false"
fetch="join"
not-null="false"
/>
<!--
<one-to-one name="sejourCourrant" class="com.medescan.proto.model.Sejour" constrained="true"/>
To add non XDoclet property mappings, create a file named
hibernate-properties-Patient.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
#-------------------------
Sejour:
<?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.medescan.proto.model.Sejour"
table="MED_SEJOUR"
>
<id
name="id"
column="SEJOUR_ID"
type="long"
>
<generator class="increment">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Sejour.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="entree_dt"
type="date"
update="true"
insert="true"
column="ENTREE_DT"
/>
<property
name="sortie_dt"
type="date"
update="true"
insert="true"
column="SORTIE_DT"
/>
<property
name="numSejour"
type="java.lang.String"
update="true"
insert="true"
column="NUM_SEJOUR"
length="12"
unique="true"
/>
<!--
<one-to-many name="constantes" class="com.medescan.proto.model.Constante" column="SEJOUR_ID"/>
-->
<bag name="constantes" inverse="false" lazy="true" order-by="VALUE_DT asc" >
<key column="SEJOUR_ID" not-null="true"/>
<one-to-many class="com.medescan.proto.model.Constante" />
</bag>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Sejour.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
#------------------------------
Constante:
<?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.medescan.proto.model.Constante"
table="MED_CONSTANTE"
>
<id
name="id"
column="CONSTANTE_ID"
type="long"
>
<generator class="increment">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Constante.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="date"
type="timestamp"
update="true"
insert="true"
column="VALUE_DT"
/>
<property
name="libelle"
type="java.lang.String"
update="true"
insert="true"
column="LIBELLE"
length="100"
/>
<property
name="valeur"
type="java.lang.String"
update="true"
insert="true"
column="VALEUR"
length="50"
/>
<many-to-one name="user" column="USER_ID" class="com.medescan.proto.model.User" not-null="true" lazy="false" fetch="join"/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Constante.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
#---------------------------
User:
<?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.medescan.proto.model.User"
table="MED_USER"
>
<id
name="id"
column="USER_ID"
type="long"
>
<generator class="increment">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-User.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="dateNaiss"
type="date"
update="true"
insert="true"
column="DTNAIS"
/>
<property
name="nom"
type="java.lang.String"
update="true"
insert="true"
column="NOM"
length="80"
/>
<property
name="prenom"
type="java.lang.String"
update="true"
insert="true"
column="PRENOM"
length="100"
/>
<property
name="profile"
type="java.lang.String"
update="true"
insert="true"
column="PROFILE"
length="1"
/>
<property
name="code"
type="java.lang.String"
update="true"
insert="true"
column="CODE"
length="8"
unique="true"
/>
<property
name="civilite"
type="java.lang.String"
update="true"
insert="true"
column="CIVILITE"
length="2"
/>
<!--
<bag name="constantes" lazy="false" inverse="true" >
<key column="USER_ID" not-null="true"/>
<one-to-many class="com.medescan.proto.model.Constante" />
</bag>
To add non XDoclet property mappings, create a file named
hibernate-properties-User.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():#------- 1
code pour inserer une constante à un séjour:
//fecht persistante object
sejour = (ISejour) session.load(Sejour.class,new Long(sejour_id));
user = (IUser) session.load(User.class,new Long(user_id));
//------------- constante-sejour association
sejour.getConstantes().add(constante);
//--------------- constante_user association
constante.setUser(user);
// -------------- save constante
session.persist(constante);
#-------
PB: (voir le SQL) pourquoi un update alors que dans l'insert la valeur SEJOUR_ID est OK ??
#--------------------------------------------- 2
patient = (IPatient) session.createQuery("from com.medescan.proto.model.Patient as patient where patient.sejourCourrant.numSejour =? ").setParameter(0, num_sejour).uniqueResult();
#-----------------------------------
PB : (voir le SQL)
Pourquoi le chargement des constantes n'est pas tardif ??
Full stack trace of any exception that occurs:Name and version of the database you are using:The generated SQL (show_sql=true):#-------------------------- 1
insertion du sejour :
select max(CONSTANTE_ID) from MED_CONSTANTE
11:11:54,302 DEBUG SQL:344 - insert into MED_CONSTANTE (VALUE_DT, LIBELLE, VALEUR, USER_ID, SEJOUR_ID, CONSTANTE_ID) values (?, ?, ?, ?, ?, ?)
11:11:54,332 DEBUG SQL:344 - update MED_CONSTANTE set SEJOUR_ID=? where CONSTANTE_ID=?
#-----------------
#------------------- 2
11:05:45,825 DEBUG SQL:344 - select patient0_.PATIENT_ID as PATIENT1_1_, patient0_.IPP as IPP1_, patient0_.NOM as NOM1_, patient0_.PRENOM as PRENOM1_, patient0_.DTNAIS as DTNAIS1_, patient0_.SEXE as SEXE1_, patient0_.SEVICE as SEVICE1_, patient0_.CHAMBRE as CHAMBRE1_, patient0_.SEJOUR_ID as SEJOUR9_1_ from MED_PATIENT patient0_, MED_SEJOUR sejour1_ where patient0_.SEJOUR_ID=sejour1_.SEJOUR_ID and sejour1_.NUM_SEJOUR=?
11:05:45,845 DEBUG SQL:344 - select sejour0_.SEJOUR_ID as SEJOUR1_2_0_, sejour0_.ENTREE_DT as ENTREE2_2_0_, sejour0_.SORTIE_DT as SORTIE3_2_0_, sejour0_.NUM_SEJOUR as NUM4_2_0_ from MED_SEJOUR sejour0_ where sejour0_.SEJOUR_ID=?
11:05:45,875 DEBUG SQL:344 - select constantes0_.SEJOUR_ID as SEJOUR6_2_, constantes0_.CONSTANTE_ID as CONSTANTE1_2_, constantes0_.CONSTANTE_ID as CONSTANTE1_3_1_, constantes0_.VALUE_DT as VALUE2_3_1_, constantes0_.LIBELLE as LIBELLE3_1_, constantes0_.VALEUR as VALEUR3_1_, constantes0_.USER_ID as USER5_3_1_, user1_.USER_ID as USER1_0_0_, user1_.DTNAIS as DTNAIS0_0_, user1_.NOM as NOM0_0_, user1_.PRENOM as PRENOM0_0_, user1_.PROFILE as PROFILE0_0_, user1_.CODE as CODE0_0_, user1_.CIVILITE as CIVILITE0_0_ from MED_CONSTANTE constantes0_ inner join MED_USER user1_ on constantes0_.USER_ID=user1_.USER_ID where constantes0_.SEJOUR_ID=? order by constantes0_.VALUE_DT asc
#--------------------------
Debug level Hibernate log excerpt:Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html