Hibernate 使用 DetachedCriteria 和接口
使用 Hibernate 3.6.8.Final 和 Spring 3.0.5.RELEASE ,我尝试为具有多个实现的类添加一些通用 DAO 功能,这些实现被更高层覆盖以实现特定的类,但它不适用于 DetachedCriteria。
示例:
在基类中:
public interface ICat {
public void setMeowSound(String meow);
public String getMeowSound();
}
然后每个继承的项目都会定义 hibernate 注释。
例如,
@Entity
@Table(name="SQUAWKY_CATS")
public class SquawkyMeowingCat implements ICat, Serializable {
@Id
@Column(name="SQUAWK_NAME")
private String meow;
public String getMeowSound() {
return meow;
}
public void setMeowString(String meow) {
this.meow = meow;
}
}
这意味着我可以使用:
Criteria criteria = Session.createCriteria(ICat.class);
Spring/Hibernate 知道它从特定项目的具体继承中提取 ICat 的注释。
但是,如果我尝试这样做:
DetachedCriteria subQuery = DetachedCriteria.forClass(ICat.class,"inner"); // etcetera
那么我会在运行时为 ICat 得到一个未知实体。
现在这是有道理的,因为第一个实例是从会话中创建它,因此它具有所需的所有配置,而 DetachedCriteria 是一个静态方法,但是在尝试执行
criteria.list()
它拾取会话的时间时会出错,并且应该知道 ICat 实际上是一个 SquawkyMeowingCat,它具有所有注释。
所以我的问题分为两部分:
1)这是已知的行为并且会永远这样吗?
2)任何人都可以想出一种简单的方法来解决它,而不使用接口和具体的 ClassHolder 来交回它需要创建的类的实例吗?
Using Hibernate 3.6.8.Final and Spring 3.0.5.RELEASE , I'm trying to add some Common DAO functionality for classes that have multiple implementations overridden higher up to implement the specific classes however it doesn't work for DetachedCriteria.
Example:
In base class:
public interface ICat {
public void setMeowSound(String meow);
public String getMeowSound();
}
Then each inherited project would define the hibernate annotations.
e.g.
@Entity
@Table(name="SQUAWKY_CATS")
public class SquawkyMeowingCat implements ICat, Serializable {
@Id
@Column(name="SQUAWK_NAME")
private String meow;
public String getMeowSound() {
return meow;
}
public void setMeowString(String meow) {
this.meow = meow;
}
}
This means I can use:
Criteria criteria = Session.createCriteria(ICat.class);
And Spring/Hibernate knows that it pulls the annotations for ICat from the concrete inheritance in the particular project.
However if I try to do:
DetachedCriteria subQuery = DetachedCriteria.forClass(ICat.class,"inner"); // etcetera
then I get an Unknown entity at runtime for ICat.
Now this makes sense as in the first instance is creating it off the Session so it has all the configuration that it needs whereas the DetachedCriteria is a static method however it errors when trying to do the
criteria.list()
by which time it has picked up the Session and should know that ICat is actually a SquawkyMeowingCat which has all the annotations.
So my questions are two part:
1) Is this known behaviour and will be like this forever more?
2) Can anyone think of a simple way around it without using an Interface and concrete ClassHolder which hands back the instance of the class it needs to create?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定 DetachedCriteria 的情况,但避免显式依赖具体类的一种方法可能是使用接口查询 Hibernate 的元数据:
关于说明性代码片段的稳健性的常见警告。
I'm not sure about the case of the DetachedCriteria, but one way to avoid explicit dependence on the concrete class might be to query Hibernate's metadata using the interface:
With the usual caveats about the robustness of illustrative code spippets.