休眠:许多到一用公式 [英] Hibernate: Many-to-one using Formula

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

问题描述

我希望有人能帮助我找到答案。

I hope someone can help me find an answer.

我与旧的数据库工作,我不能改变任何preexisting表,因为其他应用程序依赖于他们。

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.

一个具有参照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

我不希望有Java的实体B或BC,只是A和C,和A应该有一个现场交流转换器

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:

select this_.c_ID from A this_

这显然是失败,因为没有列在C_ID表A(为什么公式完全忽略?)。

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所期待的一个二进制值(C类的序列化?),并抛出一个异常铸造它接收到的整数时,这将是完美的。

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-C连接不改变A,B,C表或创建Java类B或BC?

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

推荐答案

听起来非常像这样的错误,遗憾的是没有使用注释修复准备好了,看来你可能会得到它与这些类的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.

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

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