1

How the two tags in hbm file are different one-to-one and many-to-one with unique="true"? I just learnt that to get bidirectional one to one mapping I need to use one to one tag on both hbm files. Can't I put many to one with unique="true" on both sides(both hbm) to get bidirectional behaviour in hibernate ?

in this link ,I found

<!-- In One-to-one we cannot specify the foreign key column 
         that has to be filled up
        <one-to-one name="person" class="PersonOTO_B" cascade="all"
        constrained="true"> </one-to-one>
    -->

    <many-to-one name="person" column="P_ID" unique="true"
        not-null="true" lazy="false" />

Is the above reason a valid one ?

Regards Jayendra

djvg
  • 11,722
  • 5
  • 72
  • 103
jayendra bhatt
  • 1,337
  • 2
  • 19
  • 41

1 Answers1

0

The unique = true doesn't mean anything when if you are not using Hibernate to generate out your SQL schema when your application starts. Hibernate does not enforce uniqueness of values, that is a pure database function.

If you use Hibernate to generate your database schema then it places a Unique constraint on any property that you have set unique=true on, but if you are pointing Hibernate at a database that already exist, and only validating the schema of that database, Hibernate won't check that the unique constraint exists.

The other properties this applies to are (from the top of my head) insertable, updateable and nullable

If you want a guide on how to create different kinds of relationships in Hibernate I wrote a handy guide that you can find here. Simply translate the annotations examples that I have into your .cfg file or just use the annotations and you should be set.

Community
  • 1
  • 1
JamesENL
  • 6,400
  • 6
  • 39
  • 64