- 浏览: 642854 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
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 3868方法 说明 Res ... -
hibernate3 大批量更新/删除数据 (update/delete)
2011-11-10 12:15 1340Hibernate3.0 采用新的基 ... -
hibernate lazy(延迟加载)
2011-10-12 02:20 885hibernate lazy策略可以使用在: * < ... -
一些关于Hibernate延迟加载的误区
2011-09-12 23:13 844首先是第一个误区:延迟加载只能作用于关联实体 看到这个是 ... -
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling
2011-08-12 19:17 1208Hi I am facing a problem wit ... -
Criteria Condition: Match Mode End
2011-07-02 11:05 946//////////////////////////// ... -
Criteria: Group By Criteria
2011-07-02 11:03 1253/////////////////////////////// ... -
Query Criteria Equal
2011-07-02 11:02 853/////////////////////////////// ... -
Criteria: Order
2011-07-02 10:59 734//////////////////////////// ... -
Criteria: Associations Sorting Criteria
2011-07-02 10:57 865//////////////////////////// ... -
hibernate的Query有没有setResultTransformer这个方法
2011-07-01 23:24 3040可以对原生SQL 查询使用ResultTransformer。 ... -
Hibernate中一级缓存、二级缓存及查询缓存的技术性总结
2011-06-21 17:31 1211一、 一级缓存 1. ... -
hibernate 保存 oracle 10g大数据字段
2011-06-14 04:01 1415Java代码 要点如下: ... -
Hibernate 错误原因总结
2011-06-14 00:10 13821、错误: 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 791ibatis:sql需要自己写 hibe ... -
Hibernate StackOverFlowError 异常出现的原因
2011-04-13 16:51 1239引起StackOverFlowError异常的一般是因为在程序 ...
相关推荐
博文链接:https://zhengyy.iteye.com/blog/165750
关于不动点算法的经典论文集,主要讲反问题在实际应用中所涉及的不动点算法。
反演经典书籍。
Inverse Problem Theory and Methods for Model Parameter Estimation - A. Tarantola(牛叉)
eter estimation and inverse problem philosophy and methodology, specifically regarding such key issues as uncertainty, ill-posedness, regularization, bias, and resolution. We emphasize theoretical ...
参数估计和反问题:《Parameter estimation and inverse problems》
Matrix Determinant, Inverse, Trace, and Rank.
inverse = false; th.click(function(){ table.find('td').filter(function(){ return $(this).index() === thIndex; }).sortElements(function(a, b){ return parseFloat($.text([a])) > parseFloat($.text...
Parameter Estimation and Inverse Problems一书CD程序,宝贵的资源,一起分享。
https://blog.csdn.net/a352614834/article/details/80784117
以Ruby-ish方式使用Core Data的实验 ... property :name , String , :required => true end class Article < MotionData xss=removed> 'Author' , :inverse => :articles property :title , String , :req
Inverse Synthetic Aperture Radar Simulation and Processing Tool If you have the M-code, please share it, many people will thank you!
Parameter Estimation and Inverse Problems, Second Edition对应的第7章代码
Inverse Kinematics Analysis and Emulation of the Legs Joint of the Humanoid Robot.pdf
Differential Equations Inverse and Direct Problems; 作者:M. S. Baouendi 文件格式:PDF