Is it possible to have foreign key enforced without object-to-object mapping?

As said

I understand that if I convert <many-to-one... into a <property... this would work, but foreign key would not be enforced by the database.

So my advice is: use both

public class EntityA {

    private Integer idOfB;

    private EntityB entityB;

    // getter's and setter's

}

And

<class name="A" table="a_table">
    <id name="id"/>
    <property name="idOfB" column="fk_B" not-null="false" unique="true"/>
    <many-to-one name="entityB" update="false" insert="false" column="fk_B"/>
</class>

Notice when two properties share the same column, you have to put settings about it in just one property. Otherwise, Hibernate will complain some errors. It explains why i define update="false" and insert="false" in entityB property.

regards,


You could always create the Foreign Key DDL manually in your hibernate hbm.xml file:

<hibernate-mapping>
    ...
    <database-object>
        <create>[CREATE FK]</create>
        <drop>[DROP FK]</drop>
    </database-object> 
</hibernate-mapping>

You can also scope this if different dialects need to be supported.

Check out 5.7. Auxiliary database objects