-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Saving persisted data into the database
PostPosted: Mon Oct 31, 2005 2:00 pm 
Newbie

Joined: Fri Oct 14, 2005 11:42 am
Posts: 9
Location: Madrid, SP
Hibernate version: 3

Mapping documents:
<?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>

<class name="es.tid.sgd.model.bean.usuario.Usuario"
table="usuarios">

<cache usage="read-write"/>

<id name="codUsuario" column="codigo_usuario" type="int">
<!--<generator class="sequence">
<param name="sequence">SQ_CODIGO_USUARIO</param>
</generator>-->
<generator class="increment"/>
</id>

<property name="nombre" column="nombre" type="string"
not-null="true"/>
<property name="apellidos" column="apellidos" type="string"
not-null="true"/>
<property name="login" column="login" type="string"
not-null="true"/>
<property name="telefono" column="telefono" type="string"/>
<property name="telefonoMovil" column="telefono_movil"
type="string"/>
<property name="correoElectronico"
column="correo_electronico" type="string" not-null="true"/>
<property name="comentarios" column="comentario" type="string"/>
<property name="cadenaUsuario"
formula="(select u.apellidos||', '||u.nombre||'
('||u.comentario||')'
from usuarios u
where u.codigo_usuario=codigo_usuario)"/>
</class>

</hibernate-mapping>

<?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>
<class
name="es.tid.sgd.model.bean.solicitudes.solrangos.SolicitudRango"
table="solicitudes_rango">

<!-- Atributos heredados de la clase padre -->
<id name="codSolicitud" column="codigo_solicitud_rango"
type="int">
<generator class="increment"/>
<!-- <param
name="sequence">SQ_CODIGO_SOLICITUD_RANGO</param>-->
<!--</generator>-->
</id>

<property name="fechaSolicitud"
column="fecha_solicitud_rango" type="date"/>

<many-to-one name="estadoSolicitud" lazy="true"
column="codigo_estado_solicitud"
class="es.tid.sgd.model.bean.solicitudes.estadosolicitud.EstadoSolicitud"/>

<many-to-one name="usuarioSolicitante" lazy="true"
column="codigo_usuario_solicitante"
class="es.tid.sgd.model.bean.usuario.Usuario" not-null="true"/>

<many-to-one name="usuarioGestor1" lazy="true"
column="codigo_usuario_gestor_1"
class="es.tid.sgd.model.bean.usuario.Usuario"/>

<many-to-one name="usuarioGestor2" lazy="true"
column="codigo_usuario_gestor_2"
class="es.tid.sgd.model.bean.usuario.Usuario"/>

<many-to-one name="usuarioGestor3" lazy="true"
column="codigo_usuario_gestor_3"
class="es.tid.sgd.model.bean.usuario.Usuario"/>

<!-- Atributos propios de la clase SolicitudRango -->
<!-- <many-to-one name="rango" lazy="true"
class="es.tid.sgd.model.bean.rango.Rango" column="rango"/> -->
<property name="rango" column="rango" type="string"/>

<many-to-one name="servicioRangoSuperior" lazy="true"
class="es.tid.sgd.model.bean.rango.serviciorango.ServicioRango"
column="codigo_servicio_rango_superior" not-null="true"/>

<many-to-one name="servicioRangoSol" lazy="true"
class="es.tid.sgd.model.bean.rango.serviciorango.ServicioRango"
column="codigo_servicio_rango_sol" not-null="true"/>

<property name="desUsoRango" column="uso" type="string"/>

<property name="Tamanio" column="tamanio" type="integer"/>

</class>

</hibernate-mapping>

Name and version of the database you are using: Oracle 10g

Hello you all, I've reading the documentation and the FAQ and I don't get the info what I want to know. Let me explain my doubt:

I have the mappings showed above. They obviusly correspond to two classes called SolicitudRango and Usuario (RangeRequest and User). Here it's the code (not all, but the most important, you can guess the rest seeing the hibernate mappings):

SolicitudRango class {
Integer codRangeRequest;
Usuario managerUser;
Usuario gestorUser1;
Usuario gestorUser2;
Usuario gestorUser3;
ServicioRango resquestedRangeService;
ServicioRango parentRangeService;
...
[getters and setters]
}

Usuario class {

String name;
String role;
String login;
String passwd;
String tlfn;
Integer codUser;
...
[getters and setters]
}

ServicioRango class {

Integer codService;
String bla bla bla;
List nets;
...
} // I do not give you this mapping cause I think that it's not important for the question.

In my database model I have this:
Code:
----------------- 0..*                       1--------
|Solicitud rango|<---------Requests---------->|Usuario|
-------------------                            --------

The tables are:
Code:
SolicitudesRangos:                            Usuarios
-----------------------                       --------------
Name                                        --->Code
Code                                        |    Name
Requester   -------------1--------------|---|  ....
gestorUser1-------------1----------------|
gestorUser2-------------1?---------------|
gestorUser3-------------1?---------------|
requestedRangeService
parentRangeService
....

1 means that must be one requester and one gestorUser1 and 1? means that it could be or not one gestorUser2 or 3, ok?

Well, what I want to do is inserting a new element into the table called SolicitudRango.

Requester, and gestorUserN are got from a form in a .jsp page. Only two attributes are filled: userCode and userName, so I'll have 4 objects like this:
Requester:
codUser = something
userName = something
tlfn = null
passwd = null
login = null
etc
And the same for gestorUser1,2,3. gestorUser2 and 3 could be null, so their objects could have no attributes filled, right?

Well, when I try to insert into the database I have two problems:
First: Hibernate tells me that some Usuario attributes musn't be null (like passwd,tlfn ,login, and so on). I don't care about these attributes and I don't want that hibernate tells me to filled them, How can I make that hibernate doesn't ask for filling those attributes? access="field"?

Second one: To test that I can save data into the database, I created full Usuario objects, with all their attributes filled (no one was null in the test) and when hibernate tries to save the data (getHibernateTemplate.save(object)) it tries to insert another user into the table Usuarios, and I don't want that hibernate does that thing. How can I avoid that hibernate tries to insert into the Usuarios table? cascade="all"?

Thanks a lot,
Miguel M.

PD: Sorry for my bad ascii art :S


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 4:36 pm 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:57 pm
Posts: 48
Location: Los Angeles, CA
Quote:
Hibernate tells me that some Usuario attributes musn't be null (like passwd,tlfn ,login, and so on). I don't care about these attributes and I don't want that hibernate tells me to filled them, How can I make that hibernate doesn't ask for filling those attributes?

Add not-null="false" to your mapping of gestorUser:
Code:
<many-to-one
    name="usuarioGestor2"
    lazy="true"
    not-null="false"
    column="codigo_usuario_gestor_2"
    class="es.tid.sgd.model.bean.usuario.Usuario"/>

Quote:
To test that I can save data into the database, I created full Usuario objects, with all their attributes filled (no one was null in the test) and when hibernate tries to save the data (getHibernateTemplate.save(object)) it tries to insert another user into the table Usuarios, and I don't want that hibernate does that thing. How can I avoid that hibernate tries to insert into the Usuarios table?

That's the correct behavior. What you should do is read back a Usuario that already exists in database and run your tests with it.

jd


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.