且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-29 09:41:16

这应该可以在多对一中使用公式.来自 5.1.22 部分.列和公式元素(此之前的答案中也提到了解决方案):>

columnformula 属性可以甚至可以组合在同一个属性或关联映射到表达,例如,异国情调的加入条件.

<column name="person_id" not-null="true" length="10"/><公式>'邮寄'</公式></多对一>

带注释(如果您使用的是 Hibernate 3.5.0-Beta-2+,请参阅 HHH-4382):

@ManyToOne@Formula(value="( 从 v_pipe_offerprice 中选择 v_pipe_offerprice.offerprice_fk 其中 v_pipe_offerprice.id = id )")public OfferPrice getOfferPrice() { return offerPrice;}

或者检查@JoinColumnsOrFormula:

@ManyToOne@JoinColumnsOrFormulas({ @JoinColumnOrFormula(formula=@JoinFormula(value="SUBSTR(product_idnf, 1, 3)", referencedColumnName="product_idnf")) })@Fetch(FetchMode.JOIN)私人产品产品系列;

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? 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. I am getting a SQL exception when Hibernate executes the SQL it generates which uses '=' to compare the two columns.

Thanks...

P.S. I know this is not ideal but I am stuck with the schema so I have to deal with it.

This should be possible using a formula in your many-to-one. From section 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.

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

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

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