`

inverse = “true” example and explanation

阅读更多
In Hibernate, only the “relationship owner” should maintain the relationship, and the “inverse” keyword is created to defines which side is the owner to maintain the relationship. However the “inverse” keyword itself is not verbose enough, I would suggest change the keyword to “relationship_owner“.

In short, inverse=”true” means this is the relationship owner, and inverse=”false” (default) means it’s not.
1. One to many Relationship
This is a one-to-many relationship table design, a STOCK table has many occurrences in STOCK_DAILY_RECORD table.
2. Hibernate Implementation

See the Hibernate implementation in XML mapping files.

File : Stock.java

public class Stock implements java.io.Serializable {
   ...
   private Set<StockDailyRecord> stockDailyRecords =
new HashSet<StockDailyRecord>(0);
   ...

File : StockDailyRecord.java

public class StockDailyRecord implements java.io.Serializable {
   ...
   private Stock stock;
   ...

File : Stock.hbm.xml

<hibernate-mapping>
    <class name="com.mkyong.common.Stock" table="stock" ...>
    ...
    <set name="stockDailyRecords" table="stock_daily_record" fetch="select">
        <key>
            <column name="STOCK_ID" not-null="true" />
        </key>
        <one-to-many class="com.mkyong.common.StockDailyRecord" />
    </set>
    ...

File : StockDailyRecord.hbm.xml

<hibernate-mapping>
  <class name="com.mkyong.common.StockDailyRecord" table="stock_daily_record" ...>
  ...
  <many-to-one name="stock" class="com.mkyong.common.Stock">
       <column name="STOCK_ID" not-null="true" />
  </many-to-one>
  ...

3. inverse = true / false

Inverse keyword is applied in one to many relationship. Here’s the question, if save or update operation perform in “Stock” object, should it update the “stockDailyRecords” relationship?

File : Stock.hbm.xml

    <class name="com.mkyong.common.Stock" table="stock" ...>
    ...
    <set name="stockDailyRecords" table="stock_daily_record" inverse="{true/false}" fetch="select">
        <key>
            <column name="STOCK_ID" not-null="true" />
        </key>
        <one-to-many class="com.mkyong.common.StockDailyRecord" />
    </set>
    ...

1. inverse=”true”

If inverse=”true” in the set variable, it means “stock_daily_record” is the relationship owner, so Stock will NOT UPDATE the relationship.

<class name="com.mkyong.common.Stock" table="stock" ...>
    ...
<set name="stockDailyRecords" table="stock_daily_record" inverse="true" >

2. inverse=”false”

If inverse=”false” (default) in the set variable, it means “stock” is the relationship owner, and Stock will UPDATE the relationship.

<class name="com.mkyong.common.Stock" table="stock" ...>
...
<set name="stockDailyRecords" table="stock_daily_record" inverse="false" >

See more examples below :
4. inverse=”false” Example

If keyword “inverse” is not define, the inverse = “false” will be used, which is

<!--Stock.hbm.xml-->
<class name="com.mkyong.common.Stock" table="stock" ...>
...
<set name="stockDailyRecords" table="stock_daily_record" inverse="false">

It means “stock” is the relationship owner, and it will maintains the relationship.

Insert example …

When a “Stock” object is saved, Hibernate will generated three SQL statements, two inserts and one update.

    session.beginTransaction();

    Stock stock = new Stock();
    stock.setStockCode("7052");
    stock.setStockName("PADINI");

    StockDailyRecord stockDailyRecords = new StockDailyRecord();
    stockDailyRecords.setPriceOpen(new Float("1.2"));
    stockDailyRecords.setPriceClose(new Float("1.1"));
    stockDailyRecords.setPriceChange(new Float("10.0"));
    stockDailyRecords.setVolume(3000000L);
    stockDailyRecords.setDate(new Date());

    stockDailyRecords.setStock(stock);       
    stock.getStockDailyRecords().add(stockDailyRecords);

    session.save(stock);
    session.save(stockDailyRecords);

    session.getTransaction().commit();

Output…

Hibernate:
    INSERT
    INTO
        mkyongdb.stock
        (STOCK_CODE, STOCK_NAME)
    VALUES
        (?, ?)
Hibernate:
    INSERT
    INTO
        mkyongdb.stock_daily_record
        (STOCK_ID, PRICE_OPEN, PRICE_CLOSE, PRICE_CHANGE, VOLUME, DATE)
    VALUES
        (?, ?, ?, ?, ?, ?)
Hibernate:
    UPDATE
        mkyongdb.stock_daily_record
    SET
        STOCK_ID=?
    WHERE
        RECORD_ID=?

Stock will update the “stock_daily_record.STOCK_ID” through Set variable (stockDailyRecords), because Stock is the relationship owner.
Note
The third statement is really NOT necessary.

Update example …

When a “Stock” object is updated, Hibernate will generated two SQL statements, one inserts and one update.

    session.beginTransaction();

    Stock stock = (Stock)session.get(Stock.class, 57);

    StockDailyRecord stockDailyRecords = new StockDailyRecord();
    stockDailyRecords.setPriceOpen(new Float("1.2"));
    stockDailyRecords.setPriceClose(new Float("1.1"));
    stockDailyRecords.setPriceChange(new Float("10.0"));
    stockDailyRecords.setVolume(3000000L);
    stockDailyRecords.setDate(new Date());

    stockDailyRecords.setStock(stock);       
    stock.getStockDailyRecords().add(stockDailyRecords);

    session.save(stockDailyRecords);
    session.update(stock);

    session.getTransaction().commit();

Output…

Hibernate:
    INSERT
    INTO
        mkyongdb.stock_daily_record
        (STOCK_ID, PRICE_OPEN, PRICE_CLOSE, PRICE_CHANGE, VOLUME, DATE)
    VALUES
        (?, ?, ?, ?, ?, ?)
Hibernate:
    UPDATE
        mkyongdb.stock_daily_record
    SET
        STOCK_ID=?
    WHERE
        RECORD_ID=?

Note
Again, the third statement is NOT necessary.
5. inverse=”true” Example

If keyword “inverse=true” is defined :

<!--Stock.hbm.xml-->
<class name="com.mkyong.common.Stock" table="stock" ...>
...
<set name="stockDailyRecords" table="stock_daily_record" inverse="true">

Now, it means “stockDailyRecords” is the relationship owner, and “stock” will not maintains the relationship.

Insert example …

When a “Stock” object is saved, Hibernate will generated two SQL insert statements.

    session.beginTransaction();

    Stock stock = new Stock();
    stock.setStockCode("7052");
    stock.setStockName("PADINI");

    StockDailyRecord stockDailyRecords = new StockDailyRecord();
    stockDailyRecords.setPriceOpen(new Float("1.2"));
    stockDailyRecords.setPriceClose(new Float("1.1"));
    stockDailyRecords.setPriceChange(new Float("10.0"));
    stockDailyRecords.setVolume(3000000L);
    stockDailyRecords.setDate(new Date());

    stockDailyRecords.setStock(stock);       
    stock.getStockDailyRecords().add(stockDailyRecords);

    session.save(stock);
    session.save(stockDailyRecords);

    session.getTransaction().commit();

Output …

Hibernate:
    INSERT
    INTO
        mkyongdb.stock
        (STOCK_CODE, STOCK_NAME)
    VALUES
        (?, ?)
Hibernate:
    INSERT
    INTO
        mkyongdb.stock_daily_record
        (STOCK_ID, PRICE_OPEN, PRICE_CLOSE, PRICE_CHANGE, VOLUME, DATE)
    VALUES
        (?, ?, ?, ?, ?, ?)

Update example …

When a “Stock” object is updated, Hibernate will generated one SQL statement.

    session.beginTransaction();

    Stock stock = (Stock)session.get(Stock.class, 57);

    StockDailyRecord stockDailyRecords = new StockDailyRecord();
    stockDailyRecords.setPriceOpen(new Float("1.2"));
    stockDailyRecords.setPriceClose(new Float("1.1"));
    stockDailyRecords.setPriceChange(new Float("10.0"));
    stockDailyRecords.setVolume(3000000L);
    stockDailyRecords.setDate(new Date());

    stockDailyRecords.setStock(stock);       
    stock.getStockDailyRecords().add(stockDailyRecords);

    session.save(stockDailyRecords);
    session.update(stock);

    session.getTransaction().commit();

Output…

Hibernate:
    INSERT
    INTO
        mkyongdb.stock_daily_record
        (STOCK_ID, PRICE_OPEN, PRICE_CLOSE, PRICE_CHANGE, VOLUME, DATE)
    VALUES
        (?, ?, ?, ?, ?, ?)
分享到:
评论

相关推荐

    inverse=true的总结

    在IT行业中,尤其是在Java开发或者使用ORM框架(如Hibernate)时,“inverse=true”是一个非常重要的概念,它涉及到对象关系映射中的数据管理策略。本文将深入解析“inverse=true”的含义,以及它在实际应用中的作用...

    hibernate inverse 个人总结.doc

    `inverse=false`表示主控方维护,`inverse=true`则表示被控方维护。选择合适的设置可以优化性能,但同时也需要考虑到数据一致性的维护。在实际应用中,根据业务逻辑和并发需求合理设置`inverse`属性至关重要。

    inverse 例子

    在一对多关系中,如果在多方(ManyToOne)的一端设置了`inverse="true"`,那么Hibernate将不再在多方对象保存或更新时处理关联。相反,它会交给一对一或一对多的那端去处理。这样做可以优化性能,避免不必要的数据库...

    JavaEE学习笔记之Hibernate表关系之一对多(inverse详解)

    1. **关联的插入和更新**:在一对多关系中,如果设置`inverse="true"`,那么在保存或更新父实体时,Hibernate会自动处理与子实体的关联,添加新的子实体或更新已存在的关联。相反,如果在子实体上操作,Hibernate将...

    Discrete Inverse Problems, Insight and Algorithms.pdf清晰版

    ### 知识点一:离散反问题的基本概念 **离散反问题**是指一类在数学建模过程中遇到的问题,这些问题通常与从观测数据中恢复未知源或过程有关。这类问题之所以被称为“反问题”,是因为它们是相对于正向(直接)问题...

    NHibernate Inverse & Cascade

    通过在`Employee`类的`Department`引用上设置`inverse="true"`,可以告诉NHibernate由员工对象来维护这个关联,而不是部门。这样可以避免不必要的数据库更新,提高性能。 **Cascade属性** Cascade属性涉及到对象间...

    hibernate集合映射inverse和cascade详解.txt

    如果设置`inverse=true`,则表示关系的维护责任转移到了非拥有者的一方。这样做的好处是,可以在不修改拥有者集合的情况下更新关系,通常用于优化性能或者满足特定业务逻辑的需求。 ### 集合映射:cascade属性 `...

    Inverse Problem Theory and Methods for Model Parameter Estimation

    **标题:** "Inverse Problem Theory and Methods for Model Parameter Estimation"(反演问题理论与模型参数估计方法) 此书名表明了该书的核心内容是关于反演问题的理论及其在模型参数估计中的应用方法。反演问题...

    关于cascade和inverse属性[文].pdf

    例如,在学生(Student)和老师(Teacher)的多对多关系中,如果`Student`的`Teachers`集合设置为`inverse="true"`,那么只有当修改`Teacher`对象时,Hibernate才会更新关系表`TeacherStudent`,而对`Student`的修改...

    PARAMETER ESTIMATION AND INVERSE PROBLEMS(2013)

    eter estimation and inverse problem philosophy and methodology, specifically regarding such key issues as uncertainty, ill-posedness, regularization, bias, and resolution. We emphasize theoretical ...

    彻底明白Hibernate中的Inverse

    例如,如果`Course`的`students`集合设置为`inverse="true"`,那么在添加或删除`Student`时,Hibernate不会更新`Course`对应的关联信息,从而避免了冗余的数据更新。 2. **提高性能**:因为`Inverse`端不参与关联的...

    hibernate inverse和cascade的详细讲解

    - 在`one-to-many`关系中,推荐设置`inverse="true"`,由“many”方来进行关联关系的维护。 - 在`many-to-many`关系中,只设置其中一方`inverse="false"`,或双方都不设置,以避免不必要的更新操作。 通过以上内容...

    Hibernate级联操作.docx

    当 `inverse="true"` 时,Hibernate 将负责管理关联,确保在主表(`Classes`)更新后,子表(`Student`)的外键值随之更新。这避免了在更新主表后手动更新子表的外键值,提高了数据一致性。 **示例分析** 在给定的...

    Parameter estimation and inverse problems

    在科学和工程领域中,参数估计与反问题(Parameter Estimation and Inverse Problems)是解决实际问题的重要工具之一。这类问题通常涉及未知参数或状态的确定,这些参数或状态无法直接测量,但可以通过观察到的数据...

Global site tag (gtag.js) - Google Analytics