Ver Mensaje Individual
  #1 (permalink)  
Antiguo 10/04/2015, 02:27
dasensio
 
Fecha de Ingreso: febrero-2013
Ubicación: Sevilla
Mensajes: 5
Antigüedad: 11 años, 3 meses
Puntos: 0
Error en insert con hibernate

Buenos días!

Estoy haciendo una aplicación web en java con spring, tengo una base de datos en Oracle que conecto con la aplicación con hibernate. No es la primera vez que hago algo por el estilo pero esta vez con algunas tablas me da un error al hacer los insert, a ver si me podéis echar una mano.

La tabla donde intento insertar se llama TPerfilDemanda la clave es empresa y perfil, tiene TLinea como FK y a parte tiene 2 campos alfanuméricos. Por su parte, la tabla TLinea tiene empresa y línea de PK, TEstacion y TTipoLinea como FK y otra serie de campos sin más interés.

Cuando intento hacer el insert en TPerfilDemanda me devuelve el siguiente error:

Estado HTTP 500 - Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationExcepti on: could not execute statement
java.sql.SQLIntegrityConstraintViolationException: ORA-01400: no se puede realizar una inserción NULL en ("DATADA"."T_PERFIL_DEMANDA"."IDLINEA").

He realizado insert idénticos en otras tablas del sistema y no me produce errores, además también he probado a hacer el insert informado el objeto TLinea a mano en vez de recuperándolo de la BBDD y me produce el mismo error, he mirado un montón de foros y webs y ya no se qué mirar más.

La función para insertar es:
Código:
public int save(TPerfilDemanda perfil) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.getTransaction().begin();
        session.saveOrUpdate(perfil);
        session.getTransaction().commit();
        session.close();
        return perfil.getId().getIdperfildemanda();
    }
que se llama desde:
Código:
TPerfilDemandaId pId = new TPerfilDemandaId();
            pId.setIdempresa(empresa);
            
            ILineaRepository repositorioLinea = new LineaRepositoryImpl();
            TLinea linea = repositorioLinea.getById(empresa, form.getLinea());
            p = new TPerfilDemanda(pId, linea, form.getNombre(), (short)0);
            
            repositorio.save(p);
La función que recupera la línea (La parte de //iniciar datos la añadí porque buscando información encontré que al recuperar los datos podía tener problemas en la persistencia de los datos y era necesario inicializar los datos de las FK):
Código:
public TLinea getById(int idempresa, int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(TLinea.class);
        criteria.add(Restrictions.eq("id.idlinea", id));
        criteria.add(Restrictions.eq("id.idempresa", idempresa));
        TLinea linea = (TLinea)criteria.uniqueResult();
        
        //iniciar datos
        Hibernate.initialize(linea.getTTipoLinea());
        Hibernate.initialize(linea.getId());
        Hibernate.initialize(linea.getTEstacion());
                
        session.close();
        return linea;
    }
Los XML de TLinea y TPerfilDemanda (En TPerfilDemanda, en la relación many-to-one con TLinea los parámetros update e insert los tengo a "false" porque me los creó automáticamente así al crear los POJOS, pero los he intentado cambiar y me da un error de mapeo)
Código:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 09-abr-2015 12:39:36 by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="com.shs.datada.entity.TLinea" table="T_LINEA" schema="DATADA" optimistic-lock="version">
        <composite-id name="id" class="com.shs.datada.entity.TLineaId">
            <key-property name="idempresa" type="int">
                <column name="IDEMPRESA" precision="8" scale="0" />
            </key-property>
            <key-property name="idlinea" type="int">
                <column name="IDLINEA" precision="8" scale="0" />
            </key-property>
        </composite-id>
        <many-to-one name="TEstacion" class="com.shs.datada.entity.TEstacion" update="false" insert="false" fetch="select">
            <column name="IDEMPRESA" precision="8" scale="0" not-null="true" />
            <column name="IDESTACIONORIGEN" precision="8" scale="0" not-null="true" />
        </many-to-one>
        <many-to-one name="TTipoLinea" class="com.shs.datada.entity.TTipoLinea" update="false" insert="false" fetch="select">
            <column name="IDEMPRESA" precision="8" scale="0" not-null="true" />
            <column name="IDTIPOLINEA" precision="8" not-null="true" />
        </many-to-one>
        <property name="nombrelinea" type="string">
            <column name="NOMBRELINEA" length="50" not-null="true" />
        </property>
        <property name="distancia" type="int">
            <column name="DISTANCIA" precision="8" scale="0" not-null="true" />
        </property>
        <property name="comentario" type="string">
            <column name="COMENTARIO" length="100" not-null="true" />
        </property>
        <property name="idestaciondestino" type="int">
            <column name="IDESTACIONDESTINO" precision="8" scale="0" not-null="true" />
        </property>
        <property name="codigolinea" type="string">
            <column name="CODIGOLINEA" length="5" not-null="true" />
        </property>
        <property name="codigointerno" type="string">
            <column name="CODIGOINTERNO" length="5" not-null="true" />
        </property>
        <property name="cercania" type="boolean">
            <column name="CERCANIA" precision="1" scale="0" not-null="true" />
        </property>
        <property name="lejania" type="boolean">
            <column name="LEJANIA" precision="1" scale="0" not-null="true" />
        </property>
        <set name="TPerfilDemandas" table="T_PERFIL_DEMANDA" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="IDEMPRESA" precision="8" scale="0" not-null="true" />
                <column name="IDLINEA" precision="8" not-null="true" />
            </key>
            <one-to-many class="com.shs.datada.entity.TPerfilDemanda" />
        </set>
        <set name="TPlanificacionLineas" table="T_PLANIFICACION_LINEA" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="IDEMPRESA" precision="8" scale="0" not-null="true" />
                <column name="IDLINEA" precision="8" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.shs.datada.entity.TPlanificacionLinea" />
        </set>
        <set name="TExcepcionConductors" table="T_EXCEPCION_CONDUCTOR" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="IDEMPRESA" precision="8" scale="0" not-null="true" />
                <column name="IDLINEA" precision="8" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.shs.datada.entity.TExcepcionConductor" />
        </set>
        <set name="TTrayectos" table="T_TRAYECTO" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="IDEMPRESA" precision="8" scale="0" not-null="true" />
                <column name="IDLINEA" precision="8" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.shs.datada.entity.TTrayecto" />
        </set>
        <set name="TExcepcionVehiculos" table="T_EXCEPCION_VEHICULO" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="IDEMPRESA" precision="8" scale="0" not-null="true" />
                <column name="IDLINEA" precision="8" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.shs.datada.entity.TExcepcionVehiculo" />
        </set>
    </class>
</hibernate-mapping>
Código:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 09-abr-2015 12:39:36 by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="com.shs.datada.entity.TPerfilDemanda" table="T_PERFIL_DEMANDA" schema="DATADA" optimistic-lock="version">
        <composite-id name="id" class="com.shs.datada.entity.TPerfilDemandaId">
            <key-property name="idempresa" type="int">
                <column name="IDEMPRESA" precision="8" scale="0" />
            </key-property>
            <key-property name="idperfildemanda" type="int">
                <column name="IDPERFILDEMANDA" precision="8" scale="0" />
            </key-property>
        </composite-id>
        <many-to-one name="TLinea" class="com.shs.datada.entity.TLinea" update="false" insert="false" fetch="select">
            <column name="IDEMPRESA" precision="8" scale="0" not-null="true" />
            <column name="IDLINEA" precision="8" not-null="true" />
        </many-to-one>
        <property name="descripcion" type="string">
            <column name="DESCRIPCION" length="50" not-null="true" />
        </property>
        <property name="factorcorrecciondemanda" type="short">
            <column name="FACTORCORRECCIONDEMANDA" precision="3" scale="0" not-null="true" />
        </property>
        <set name="TTramoHorarios" table="T_TRAMO_HORARIO" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="IDEMPRESA" precision="8" scale="0" not-null="true" />
                <column name="IDPERFILDEMANDA" precision="8" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.shs.datada.entity.TTramoHorario" />
        </set>
    </class>
</hibernate-mapping>

Muchísimas gracias!

Un saludo!