简体   繁体   中英

Hibernate many to one with join by column and constant

I have the following tables:

A and B

A has a composite PK: id int, type string.

B has only one of A's PK: id int.

How can I define a many to one relationship from A to B using B's id with constant type='typeB'?

I've tried the following example but it fails on runtime, it does not fetch a(null instead).

 <class name="B" table="B">
    <many-to-one name="a" class="A" fetch="join" outer-join="false">
        <column name="id"/>
        <column name="type" default="typeB"/>
    </many-to-one>
</class>

This is what finally worked for me:

<class name="B" table="B">
    <many-to-one name="a" class="A" fetch="join" outer-join="false">
        <column name="id"/>
        <formula>'typeB'</formula>
    </many-to-one>
</class>

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