简体   繁体   中英

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 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

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

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;
}

this produces the following 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 ?).

Removing the @ManyToOne annotation produces:

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.

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

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

Thanks for any info, Dan

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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