- 浏览: 656345 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
HkEndless:
不好意思,请问这确定是回调机制吗。你的例子中只是将接口的实现类 ...
Spring CallBack回调机制介绍 -
hanmiao:
写的真乱啊,完全不知所云...
Java如何调用可执行文件和批处理命令 -
junia_1:
junia_1 写道 shock: ...
为什么要使用EJB -
junia_1:
shock:
为什么要使用EJB -
coollifer:
不错
SQL Server数据导入到Oracle中的方法
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
(?, ?, ?, ?, ?, ?)
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
(?, ?, ?, ?, ?, ?)
发表评论
-
hibernate Restrictions用法 MatchMode.ANYWHERE
2012-07-14 15:50 3945方法 说明 Res ... -
hibernate3 大批量更新/删除数据 (update/delete)
2011-11-10 12:15 1366Hibernate3.0 采用新的基 ... -
hibernate lazy(延迟加载)
2011-10-12 02:20 915hibernate lazy策略可以使用在: * < ... -
一些关于Hibernate延迟加载的误区
2011-09-12 23:13 887首先是第一个误区:延迟加载只能作用于关联实体 看到这个是 ... -
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling
2011-08-12 19:17 1244Hi I am facing a problem wit ... -
Criteria Condition: Match Mode End
2011-07-02 11:05 978//////////////////////////// ... -
Criteria: Group By Criteria
2011-07-02 11:03 1329/////////////////////////////// ... -
Query Criteria Equal
2011-07-02 11:02 890/////////////////////////////// ... -
Criteria: Order
2011-07-02 10:59 767//////////////////////////// ... -
Criteria: Associations Sorting Criteria
2011-07-02 10:57 906//////////////////////////// ... -
hibernate的Query有没有setResultTransformer这个方法
2011-07-01 23:24 3082可以对原生SQL 查询使用ResultTransformer。 ... -
Hibernate中一级缓存、二级缓存及查询缓存的技术性总结
2011-06-21 17:31 1232一、 一级缓存 1. ... -
hibernate 保存 oracle 10g大数据字段
2011-06-14 04:01 1446Java代码 要点如下: ... -
Hibernate 错误原因总结
2011-06-14 00:10 14261、错误: object references an unsa ... -
MappingException提示Association references unmapped class错误的解决方法
2011-06-12 17:09 10出现如下错误信息时 Caused by: org.hibe ... -
Hibernate一对多关联映射的配置及其级联删除问题
2011-06-12 17:07 9首先举一个简单的一对多双向关联的配置: 一的一端:Quest ... -
Hibernate多对一关联映射原理、映射方法和数据的保存
2011-06-12 17:06 13Hibernate的多对一关联映射实现的基本原理就是: 在多 ... -
Hibernate一对多单向关联和双向关联映射方法及其优缺点
2011-06-12 17:05 11一对多关联映射和多对一关联映射实现的基本原理都是一样的,既是在 ... -
ibatis和hibernate区别
2011-04-13 17:34 826ibatis:sql需要自己写 hibe ... -
Hibernate StackOverFlowError 异常出现的原因
2011-04-13 16:51 1274引起StackOverFlowError异常的一般是因为在程序 ...
相关推荐
在IT行业中,尤其是在Java开发或者使用ORM框架(如Hibernate)时,“inverse=true”是一个非常重要的概念,它涉及到对象关系映射中的数据管理策略。本文将深入解析“inverse=true”的含义,以及它在实际应用中的作用...
`inverse=false`表示主控方维护,`inverse=true`则表示被控方维护。选择合适的设置可以优化性能,但同时也需要考虑到数据一致性的维护。在实际应用中,根据业务逻辑和并发需求合理设置`inverse`属性至关重要。
在一对多关系中,如果在多方(ManyToOne)的一端设置了`inverse="true"`,那么Hibernate将不再在多方对象保存或更新时处理关联。相反,它会交给一对一或一对多的那端去处理。这样做可以优化性能,避免不必要的数据库...
1. **关联的插入和更新**:在一对多关系中,如果设置`inverse="true"`,那么在保存或更新父实体时,Hibernate会自动处理与子实体的关联,添加新的子实体或更新已存在的关联。相反,如果在子实体上操作,Hibernate将...
### 知识点一:离散反问题的基本概念 **离散反问题**是指一类在数学建模过程中遇到的问题,这些问题通常与从观测数据中恢复未知源或过程有关。这类问题之所以被称为“反问题”,是因为它们是相对于正向(直接)问题...
通过在`Employee`类的`Department`引用上设置`inverse="true"`,可以告诉NHibernate由员工对象来维护这个关联,而不是部门。这样可以避免不必要的数据库更新,提高性能。 **Cascade属性** Cascade属性涉及到对象间...
如果设置`inverse=true`,则表示关系的维护责任转移到了非拥有者的一方。这样做的好处是,可以在不修改拥有者集合的情况下更新关系,通常用于优化性能或者满足特定业务逻辑的需求。 ### 集合映射:cascade属性 `...
**标题:** "Inverse Problem Theory and Methods for Model Parameter Estimation"(反演问题理论与模型参数估计方法) 此书名表明了该书的核心内容是关于反演问题的理论及其在模型参数估计中的应用方法。反演问题...
例如,在学生(Student)和老师(Teacher)的多对多关系中,如果`Student`的`Teachers`集合设置为`inverse="true"`,那么只有当修改`Teacher`对象时,Hibernate才会更新关系表`TeacherStudent`,而对`Student`的修改...
eter estimation and inverse problem philosophy and methodology, specifically regarding such key issues as uncertainty, ill-posedness, regularization, bias, and resolution. We emphasize theoretical ...
例如,如果`Course`的`students`集合设置为`inverse="true"`,那么在添加或删除`Student`时,Hibernate不会更新`Course`对应的关联信息,从而避免了冗余的数据更新。 2. **提高性能**:因为`Inverse`端不参与关联的...
- 在`one-to-many`关系中,推荐设置`inverse="true"`,由“many”方来进行关联关系的维护。 - 在`many-to-many`关系中,只设置其中一方`inverse="false"`,或双方都不设置,以避免不必要的更新操作。 通过以上内容...
当 `inverse="true"` 时,Hibernate 将负责管理关联,确保在主表(`Classes`)更新后,子表(`Student`)的外键值随之更新。这避免了在更新主表后手动更新子表的外键值,提高了数据一致性。 **示例分析** 在给定的...
在科学和工程领域中,参数估计与反问题(Parameter Estimation and Inverse Problems)是解决实际问题的重要工具之一。这类问题通常涉及未知参数或状态的确定,这些参数或状态无法直接测量,但可以通过观察到的数据...