简体   繁体   English

JPA或Hibernate - 在不同类型的列上连接表

[英]JPA or Hibernate - Joining tables on columns of different types

Is there a way to tell Hibernate to wrap a column in a to_char when using it to join to another table or conversely convert a NUMBER to a VARCHAR? 有没有办法告诉Hibernate在使用它连接到另一个表或反过来将NUMBER转换为VARCHAR时将其包装在to_char中? I have a situation where I have a table which contains a generic key column of type VARCHAR which stores the Id of another table which is a Number. 我有一种情况,我有一个表,其中包含VARCHAR类型的通用键列,它存储另一个表的ID,它是一个数字。 I am getting a SQL exception when Hibernate executes the SQL it generates which uses '=' to compare the two columns. 当Hibernate执行它生成的SQL时,我得到一个SQL异常,它使用'='来比较两列。

Thanks... 谢谢...

PS I know this is not ideal but I am stuck with the schema so I have to deal with it. PS我知道这不太理想,但我坚持使用架构,所以我必须处理它。

This should be possible using a formula in your many-to-one. 这应该可以使用多对一的公式。 From section 5.1.22. 来自第5.1.22 Column and formula elements (solution also mentioned in this previous answer ): 列和公式元素前一个答案中也提到了解决方案):

column and formula attributes can even be combined within the same property or association mapping to express, for example, exotic join conditions. columnformula属性甚至可以在同一属性或关联映射中组合,以表示例如异国连接条件。

 <many-to-one name="homeAddress" class="Address" insert="false" update="false"> <column name="person_id" not-null="true" length="10"/> <formula>'MAILING'</formula> </many-to-one> 

With annotations (if you are using Hibernate 3.5.0-Beta-2+, see HHH-4382 ): 使用注释(如果您使用的是Hibernate 3.5.0-Beta-2 +,请参阅HHH-4382 ):

@ManyToOne
@Formula(value="( select v_pipe_offerprice.offerprice_fk from v_pipe_offerprice where v_pipe_offerprice.id = id )")
public OfferPrice getOfferPrice() { return offerPrice; } 

Or maybe check the @JoinColumnsOrFormula : 或者也许查看@JoinColumnsOrFormula

@ManyToOne
@JoinColumnsOrFormulas(
{ @JoinColumnOrFormula(formula=@JoinFormula(value="SUBSTR(product_idnf, 1, 3)", referencedColumnName="product_idnf")) })
@Fetch(FetchMode.JOIN)
private Product productFamily;

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

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