谁能解释一下hibernate 中的Cascades

    
newpaul 03-08-08

谁能解释一下hibernate 中的Cascades

还有关于proxy的问题
<class name="eg.Order" proxy="eg.Order">
The runtime type of the proxies will be a subclass of Order. Note that the proxied class must implement a default constructor with at least package visibility.

在它的文档上是这么说的

为什么说 代理的类型时order的子类呢 而不是order本身呢?

yehs220
2003-08-08 15:11

cascade:级联,用于传播各种操作到关联对象,可选值
all:传播所有
none:不传播任何
delete:传播delete
save-update:传播insert,update,
最新版的hibernate可能还有更多可选值。

proxy:
你可以load一个对象,看看它的实际类型是什么,再看看它的
继承树及实现接口。

newpaul
2003-08-08 15:20

我看了他的文档 但是不太理解 能帮我解释一下吗?

<class name="eg.Order" proxy="eg.Order">
The runtime type of the proxies will be a subclass of Order.
它说这里的proxy是 order 的子类 迷惑


<class name="eg.Cat" proxy="eg.Cat">
......
<subclass name="eg.DomesticCat" proxy="eg.DomesticCat">
.....
</subclass>
</class>
Firstly, instances of Cat will never be castable to DomesticCat, even if the underlying instance is an instance of DomesticCat

这里说 cat 类不能够造成它的子类DomesticCat 迷惑
Cat cat = (Cat) session.load(Cat.class, id); // instantiate a proxy (does not hit the db)
if ( cat.isDomesticCat() ) { // hit the db to initialize the proxy
DomesticCat dc = (DomesticCat) cat; // Error!
....
}

Secondly, it is possible to break proxy ==.

Cat cat = (Cat) session.load(Cat.class, id); // instantiate a Cat proxy
DomesticCat dc =
(DomesticCat) session.load(DomesticCat.class, id); // required new DomesticCat proxy!
System.out.println(cat==dc);
这一段也没明白 这里的break proxy == 是什么意思

However, the situation is not quite as bad as it looks. Even though we now have two references to different proxy objects, the underlying instance will still be the same object:

cat.setWeight(11.0); // hit the db to initialize the proxy
System.out.println( dc.getWeight() ); // 11.0

这里为何 cat 和dc 怎么又是同一个对象呢?? 迷惑



能解释一下吗? 谢谢

newpaul
2003-08-08 15:23

相关的 mapping
class name="eg.Order" proxy="eg.Order"


class name="eg.Cat" proxy="eg.Cat"
......
subclass name="eg.DomesticCat" proxy="eg.DomesticCat"
.....
/subclass
/class

yehs220
2003-08-08 15:42

如果你为某个class设置了proxy="...",那么hibernate会在运行时用CGLib生成一个
proxy="..."所指定的这个类的一个子类作为你的那个entity class的proxy。


比如说你为Cat指定proxy="Cat",hibernate会继承Cat,生成一个代理类,就叫CatProxy(我随便取的)吧

同样对DomesticCat,hibernate会继承DomesticCat,生成一个代理类,就叫DomesticCatProxy吧


Cat cat = (Cat) session.load(Cat.class, id); // instantiate a proxy (does not hit the db)
//cat的实际类型就是CatProxy,你可以调用cat.getClass().getName()和cat.getClass().getSuperClass().getName()就知道了

if ( cat.isDomesticCat() ) { // hit the db to initialize the proxy
DomesticCat dc = (DomesticCat) cat; // Error!

//从CatProxy转型到DomesticCat,显然不对
....
}

Secondly, it is possible to break proxy ==.

Cat cat = (Cat) session.load(Cat.class, id); // instantiate a Cat proxy
DomesticCat dc =
(DomesticCat) session.load(DomesticCat.class, id); // required new DomesticCat proxy!
System.out.println(cat==dc);
这一段也没明白 这里的break proxy == 是什么意思

//就是会打出false,cat和de的类型分别是CatProxy和DomesticCatProxy,怎么会相等呢?

However, the situation is not quite as bad as it looks. Even though we now have two references to different proxy objects, the underlying instance will still be the same object:

cat.setWeight(11.0); // hit the db to initialize the proxy
System.out.println( dc.getWeight() ); // 11.0

//这两个proxy的底层是同一个对象

2Go 1 2 下一页