简体   繁体   English

休眠:使用公式多对一

[英]Hibernate: Many-to-one using Formula

I hope someone can help me find an answer. 我希望有人能帮我找到答案。

I'm working with a legacy database and I can't change any of the preexisting tables, because other apps depend on them. 我正在使用遗留数据库,我无法更改任何预先存在的表,因为其他应用程序依赖于它们。

I have three main existing tables: A,B,C . 我有三个主要的现有表: A,B,C

A has a column with reference to B(many to one relation). A有一个参考B的列(多对一关系)。 The problem is that it should have a relation to C not to B. So I have created a *-1 mapping BC. 问题是它应该与C有关系而不是B.所以我创建了一个* -1映射BC。

Tables: A,B,C,BC (all have ID field)
A-B many to one
B-C many to one through BC
Needed:A-C without altering A,B or C

I don't want to have java entities for B or BC, just A and C, and A should have a field Ac 我不希望有B或BC的java实体,只有A和C,A应该有一个字段Ac

So far I have tried using the @Formula annotation to no avail. 到目前为止,我已经尝试使用@Formula注释无济于事。

class A{
  @ManyToOne
  @Formula(value="select BC.c from BC where BC.b = b")
  private C c;
}

this produces the following SQL: 这会产生以下SQL:

select this_.c_ID from A this_

It obviously fails because there is no column c_ID in table A (why is formula ignored completely ?). 它显然失败了,因为表A中没有列c_ID(为什么公式被完全忽略?)。

Removing the @ManyToOne annotation produces: 删除@ManyToOne注释会产生:

select (select BC.c from BC where BC.b = this_.b) as formula_0 from A this_

This would be perfect except Hibernate expects a BINARY value (the serialization of the class C ?) and throws an exception when casting the Integer it receives. 这是完美的,除了Hibernate期望BINARY值(类C的序列化?)并在转换它接收的Integer时抛出异常。

This ID should be enough for lazy loading, but how do I tell it to do that? 这个ID对于延迟加载应该足够了,但是我怎么告诉它这样做呢? any use of @ManyToOne breaks the formula. @ManyToOne的任何使用都会破坏公式。

How can I achieve the AC link without altering the A,B,C tables or creating the java classes B or BC? 如何在不改变A,B,C表或创建Java类B或BC的情况下实现AC链接?

Thanks for any info, Dan 感谢任何信息,丹

听起来很像这个bug ,遗憾的是没有使用注释修复,似乎你可能会使用xml映射文件来处理这些类。

这应该适用于Hibernate 3.5.0-Beta-2 +( HHH-4382已经修复)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM