简体   繁体   English

Hibernate何时创建从FK到非PK列的新条目?

[英]When does Hibernate create a new entry from a FK to a non-PK column?

suppose I have: 假设我有:

-- table_1 -------------------  
|id      | property_x | data |
------------------------------

-- table_2 ------------------------
|id      | property_x | moar_data |
-----------------------------------

And that table_1 has a FK from property_x to table_2.property_x table_1有FK从property_xtable_2.property_x
(why not table_2.id ? IDK, this project was already like this :( ) (为什么不是table_2.id ,这个项目已经像这样:()

The HBMs look like this: HBM如下所示:

<!-- Table 1 -->
<hibernate-mapping package="com.example">
    <class name="Table1" table="table_1">
        <id column="id" type="integer" name="id">
            <generator class="native"/>
        </id>
        <many-to-one class="Table2" fetch="join" name="table2" not-null="false">
            <column name="property_x" />
        </many-to-one>
    </class>
</hibernate-mapping>

and

<!-- Table 2 -->
<hibernate-mapping package="com.example">
    <class name="Table2" table="table_2">
        <id column="id" type="integer" name="id">
            <generator class="native"/>
        </id>
        <property column="property_x" name="propertyX" type="string"/>
    </class>
</hibernate-mapping>

The thing is that I want to save an object Table1 using session.save(objTable1) , without creating a new Table2 entry in the DB, and without loading it either. 事实是,我想使用session.save(objTable1)保存对象Table1 ,而不在数据库中创建新的Table2条目,也不必加载它。 I have done this in the past by creating a new object, and only setting the primary key values, leaving everything else blank, and not letting it update the Table2 table, but this is not the PK >_<. 过去,我通过创建一个新对象来完成此操作,仅设置主键值,将其他所有内容保留为空白,并且不让它更新Table2表,但这不是PK> _ <。

Now, suppose I have in table_2 an entry with id=5 , property_x="EX" . 现在,假设我在table_2一个with id=5property_x="EX"的条目。 When I do the following... 当我执行以下操作时...

// example method
public void saveSomething(Sessions sess) {
    Table1 table1 = new Table1();
    Table2 table2 = new Table2();

    table2.setPropertyX("EX");
    table1.setTable2(table2);

    sess.save(table1);
}

... it creates a new entry, I'm guessing it's because the PK (id) for Table2 is not set. ...它创建了一个新条目,我猜是因为未设置Table2的PK(id)。

My question is, how does hibernate decide to create a new entry in the DB from a FK object?, and is there a way to avoid that on the HBM files? 我的问题是,hibernate如何决定从FK对象在数据库中创建一个新条目?有没有办法避免在HBM文件上出现这种情况?

It's the cascade anotation, if you dont want table 2 to be created in the db, dont set it as a child, but if you want table 2 to be updated, you need the table 2 id. 这是级联注释,如果您不想在数据库中创建表2,请不要将其设置为子级,但是如果要更新表2,则需要表2 id。

Alternatively, you can set cascade="none" in the hbm of table 1 or cascade="update", but table 2 still wont be updated without the id. 另外,您可以在表1的hbm中设置cascade =“ none”或cascade =“ update”,但是如果没有id,表2仍然不会被更新。

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

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