Hibernate:多对一使用公式 [英] Hibernate: Many-to-one using Formula

查看:23
本文介绍了Hibernate:多对一使用公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人能帮我找到答案.

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.

我现有的三个主要表:A、B、C.

I have three main existing tables: A,B,C.

A 有一列参考 B(多对一关系).问题是它应该与 C 有关系而不是 B.所以我创建了一个 *-1 映射 BC.

A has a column with reference to B(many to one relation). The problem is that it should have a relation to C not to B. So I have created a *-1 mapping 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

我不想有 B 或 BC 的 java 实体,只有 A 和 C,而 A 应该有一个字段 A.c

I don't want to have java entities for B or BC, just A and C, and A should have a field A.c

到目前为止,我已经尝试使用 @Formula 注释无济于事.

So far I have tried using the @Formula annotation to no avail.

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

这会产生以下 SQL:

this produces the following SQL:

select this_.c_ID from A this_

它显然失败了,因为表 A 中没有列 c_ID(为什么完全忽略公式?).

It obviously fails because there is no column c_ID in table A (why is formula ignored completely ?).

删除@ManyToOne 注释会产生:

Removing the @ManyToOne annotation produces:

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

这将是完美的,除了 Hibernate 需要一个 BINARY 值(类 C 的序列化?)并在转换它接收到的 Integer 时抛出异常.

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.

这个 ID 应该足以延迟加载,但我如何告诉它这样做呢?任何@ManyToOne 的使用都会破坏公式.

This ID should be enough for lazy loading, but how do I tell it to do that? any use of @ManyToOne breaks the formula.

如何在不改变 A、B、C 表或创建 Java 类 B 或 BC 的情况下实现 A-C 链接?

How can I achieve the A-C link without altering the A,B,C tables or creating the java classes B or BC?

感谢您提供任何信息,丹

Thanks for any info, Dan

推荐答案

听起来很像这样 bug,不幸的是没有准备好使用注释进行修复,但似乎您可以使用这些类的 xml 映射文件.

Sounds very like this bug, unfortunately no fix ready using annotations, seems you might get it working with xml mapping file for those classes though.

这篇关于Hibernate:多对一使用公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆