1

我有以下数据库表:

CREATE TABLE DS_NVALUES(
  id Number NOT NULL,
  value1 Number(3,0),
  value2 Number(3,0),
  CONSTRAINT ds_nvalues_id PRIMARY KEY (id)
)

CREATE TABLE DS_AREA(
  id Number NOT NULL,
  id_nvalues Varchar2(15),
  n_c Number(4,0),  
  CONSTRAINT ds_area_id PRIMARY KEY (id)
)

一个 DSNValue 可以有一个 DSAreas 列表。这些是映射文件:

<hibernate-mapping package="es.entities">
    <class name="DSValues" table="ds_nvalues" lazy="false">
        <id name="id" column="id">
            <generator class="org.hibernate.id.MultipleHiLoPerTableGenerator">
                <param name="table">igea_codigos</param>
                <param name="primary_key_column">nombre</param>
                <param name="value_column">codigo</param>
                <param name="primary_key_value">ds_nvalues</param>
                <param name="max_lo">0</param>
            </generator>
        </id>       
        <property name="value1" column="value1"/>
        <property name="value2" column="value2"/>

        <property name="codedId" formula="encode_id('ds_nvalues', id)"/>

        <bag name="areas" table="ds_area" cascade="save-update, delete" fetch="select" lazy="false" inverse="true" >
            <key property-ref="codedId">
                <column name="id_nvalues"/>
            </key>
            <one-to-many class="es.entities.DSArea" not-found="exception" />
        </bag>

    </class>
</hibernate-mapping>



<hibernate-mapping package="es.entities">
    <class name="DSArea" table="ds_area" lazy="false">
        <id name="id" column="id">
            <generator class="org.hibernate.id.MultipleHiLoPerTableGenerator">
                <param name="table">igea_codigos</param>
                <param name="primary_key_column">nombre</param>
                <param name="value_column">codigo</param>
                <param name="primary_key_value">ds_area</param>
                <param name="max_lo">0</param>
            </generator>
        </id>
        <property name="n_c" column="n_c"/>

        <many-to-one name="dsValues" class="es.entities.DSValues"  unique="true" fetch="select">
            <column name="id_nvalues" write="encode_id('ds_nvalues', ?)"            read="decode_id(id_nvalues)"/>
        </many-to-one> 
    </class>
</hibernate-mapping>

当我创建一个新的 DSNValue 时,我有以下代码:

DSArea area1 = new DSArea();
area1.setN_c(300);
List<DSArea> areas = new ArrayList<DSArea>();
areas.add(area1);

DSValues dsValues = new DSValues();
dsValues.setValue1(3);
dsValues.setValue2(4);
dsValues.setAreas(areas);
area1.setDsValues(dsValues);

Session session = getCurrentSession();
Transaction t = session.beginTransaction();     
try{
    session.save(dsValues);
    t.commit();
}catch(Exception e){
    t.rollback();           
}

t.commit 抛出 NullPointerException,但在休眠日志中,我可以看到 INSERT 语句及其值,它们没问题。对于选择数据库元素,此映射配置工作正常。会发生什么?

java.lang.NullPointerException
            at 
org.hibernate.type.descriptor.java.AbstractTypeDescriptor.extractHashCode(AbstractTypeDescriptor.java:88)
        at org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:210)
        at org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:214)
        at org.hibernate.engine.spi.CollectionKey.generateHashCode(CollectionKey.java:86)
        at org.hibernate.engine.spi.CollectionKey.<init>(CollectionKey.java:74)
        at org.hibernate.engine.spi.CollectionKey.<init>(CollectionKey.java:50)
        at org.hibernate.event.internal.AbstractFlushingEventListener.postFlush(AbstractFlushingEventListener.java:369)
        at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:53)
        at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1233)
        at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
        at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
        at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
        at es.indra.rbc.da.impl.oracle.dao.OracleElementDAO.insert(OracleElementDAO.java:51)
4

0 回答 0