Announcement Announcement Module
Collapse
No announcement yet.
Inserting persisted data into de DB Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Inserting persisted data into de DB

    This is a thread that I posted some days ago in the hibernate forum, but I didn't get a satisfatory answered, I hope that you can answer me.
    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.estadosol icitud.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.S ervicioRango"
    column="codigo_servicio_rango_superior" not-null="true"/>

    <many-to-one name="servicioRangoSol" lazy="true"
    class="es.tid.sgd.model.bean.rango.serviciorango.S ervicioRango"
    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:

    ----------------- 0..* 1--------
    |Solicitud rango|<---------Requests---------->|Usuario|
    ------------------- --------

    The tables are:

    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?

    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?
    Thanks a lot,
    Miguel M.

    PD: Sorry for my bad ascii art :S

  • #2
    First of all, sorry for the typo in the title, it must be "... into THE DB" instead of "...into de DB", and the thing that i want to comment you now is this:

    I get this exception when I try to insert into the DB:

    Exception thrown executing action 'es.tid.sgd.web.flow.rangerequest.RangeRequestActi [email protected]' in state 'viewInitRangeRequest' of flow 'rangeRequestFlow'; nested exception is org.springframework.dao.DataIntegrityViolationExce ption: Hibernate operation: Could not execute JDBC batch update; SQL [insert into solicitudes_rango (fecha_solicitud_rango, codigo_estado_solicitud, codigo_usuario_solicitante, codigo_usuario_gestor_1, codigo_usuario_gestor_2, codigo_usuario_gestor_3, rango, codigo_servicio_rango_superior, codigo_servicio_rango_sol, descripcion_uso, tamanio, codigo_solicitud_rango) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]; ORA-02291: restricción de integridad (ADMINSGD.FK_SOLRANGOS_USUARIOGES3) violada - clave principal no encontrada
    ; nested exception is java.sql.BatchUpdateException: ORA-02291: restricción de integridad (ADMINSGD.FK_SOLRANGOS_USUARIOGES3) violada - clave principal no encontrada

    getHibernateTemplate().save(object) tries to do this:
    [insert into solicitudes_rango (fecha_solicitud_rango, codigo_estado_solicitud, codigo_usuario_solicitante, codigo_usuario_gestor_1, codigo_usuario_gestor_2, codigo_usuario_gestor_3, rango, codigo_servicio_rango_superior, codigo_servicio_rango_sol, descripcion_uso, tamanio, codigo_solicitud_rango) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)];

    And what I want is that when codigo_usuario_gestor_2 or codigo_usuario_gestor_3 are null, HibernateTemplate does this thing:
    [insert into solicitudes_rango (fecha_solicitud_rango, codigo_estado_solicitud, codigo_usuario_solicitante, codigo_usuario_gestor_1, rango, codigo_servicio_rango_superior, codigo_servicio_rango_sol, descripcion_uso, tamanio, codigo_solicitud_rango) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)];

    Notice that in the insert mentioned above, codigo_usuario_gestor_2 and _3 do not appear cause they're null. How can I get to HibernateTemplate makes it the correct way?
    In both hibernate mappings I have something like this:

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

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

    Am I missing something in both mappings?

    Thanks in advance,
    Miguel M.

    Comment

    Working...
    X