简体   繁体   English

休眠:新记录或旧记录

[英]Hibernate: new or old record

I have a OneToMany connection between tables Result and ResultAux . 我在表ResultResultAux之间有一个OneToMany连接。 I can retrieve a set of ResultAux objects from Result . 我可以从Result检索一组ResultAux对象。 After that I'm adding some ResultAux objects to set and using merge on each set entry to flush changes into database. 之后,我要添加一些ResultAux对象进行设置,并在每个set条目上使用merge将更改刷新到数据库中。 Like this: 像这样:

Set<ResultAux> resultAuxes = result.getResultAuxes();
if (resultAuxes != null) {
    for (ResultAux resultAux : resultAuxes) {
        resultAux = getDaoFactory().getResultAuxDAO().merge(resultAux);
    }
}

For some additional actions i need to know is set entry a new record and will be inserted into table or it's an old one and (modified or not) will be updated. 对于某些其他操作,我需要知道的是设置条目新记录,并将其插入表中,或者它是旧记录,并且(是否修改)将被更新。 I noticed that all entries of ResultAux set already have an ID, so i can't check it for null as i did for other tables. 我注意到ResultAux集的所有条目都已经有一个ID,因此我无法像其他表那样检查它是否为null Is there any way to determine such thing (prefferably not involving extra libs)? 有什么方法可以确定这种情况(通常不涉及额外的库)吗?

EDIT : 编辑

<hibernate-mapping>
    <class name="ResultAux" table="RESULT_AUX">
        <id name="id" column="ID">
            <generator class="native" />
        </id>

        <property name="additinalInfoType" column="AITYPE" type="dao.hibernate.utl.AdditinalInfoEnumType" />
        <property name="sorter" column="SORTER" />
        <property name="value1" column="VAL1" />
        <property name="value2" column="VAL2" />

        <many-to-one name="result" column="RESULT_ID" class="Result" />
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="Result" table="RESULT">
        <id name="id" column="ID">
            <generator class="native" />
        </id>

        <property name="questionNumber" column="Q_NUM" />
        <property name="answerNumber" column="A_NUM" />
        <property name="questionGroup" column="Q_GRP" />
        <property name="answerValue" column="A_VAL" />
        <set name="resultAuxes" inverse="true" cascade="all-delete-orphan"
            lazy="false">
            <key column="RESULT_ID" />
            <one-to-many class="ResultAux" />
        </set>
    </class>
</hibernate-mapping>

I'm not sure my answer will resolve your problem, but to come back on @EugenioCuevas comment, I would have done something like this to persist your child entities: 我不确定我的答案是否会解决您的问题,但是要返回@EugenioCuevas评论,我会做这样的事情来保留您的子实体:

Set<ResultAux> resultAuxes = result.getResultAuxes();
if (resultAuxes != null) {
    for (ResultAux resultAux : resultAuxes) {
        resultAux.setResult(result);
    }
}
getDaoFactory().getResultDAO().merge(result);

Hibernate should then manage the relations on its own. 然后,Hibernate应该自行管理这些关系。

Using native as the the primary key generation strategy causes Hibernate to select identity , sequence or hilo as the PK generation strategy depending upon the capabilities of the underlying DB. 使用native作为主键生成策略会使Hibernate根据基础DB的功能选择identitysequencehilo作为PK生成策略。

I think for your DB , hibernate selects "sequence" strategy such that you may experience this problem (Records that are not inserted to the DB can have the assigned ID) by the following codes: 我认为,对于您的数据库,休眠通过以下代码选择“顺序”策略,以便您可能会遇到此问题(未插入数据库的记录可以具有分配的ID):

Set<ResultAux> resultAuxes = result.getResultAuxes();
ResultAux  newResultAux = new ResultAux();

/**
 * As session.save()  requires to return the ID for the saved instance , if  "sequence" strategy is 
 * used , Hibernate will hit the DB  to select the next ID (eg. select some_seq.NEXTVAL) to be used 
 * for the saved instance. So newResultAux  will have an ID after save()  but actually it is not saved to 
 * the DB yet as  saving to the DB  occurs during session.flush()
 */
session.save(newResultAux);
resultAuxes.add(newResultAux);

One of the solutions is to add a @Version property to the ResultAux . 解决的办法之一是将增加@Version属性的ResultAux Then you could check the value of this @Version property to determine whether it is a new record or not as the @Version property for a new record must be NULL 然后,您可以检查此@Version属性的值以确定它是否是新记录,因为新记录的@Version属性必须为NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM