Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use property-ref attribute in hibernate

Tags:

hibernate

I have done the sample program. I used property-ref attribute. Even if I do not use also it is still working. Please let me know what is the actual usage of property-ref attribute in this program.

 <hibernate-mapping package="com.hibernate.onetone">
 <class name="Book" table="BOOK">
    <id name="id" column="BOOK_ID">
        <generator class="native" />
    </id>
    <property name="title" type="string" column="TITLE" />
    <property name="description" type="string" column="DESCRIPTION" />
    <property name="publishedDate" type="date" column="PUBLISHED" />

    <many-to-one name="author" class="com.hibernate.onetone.Author"
        column="author_id" unique="true" not-null="true"
        cascade="all" />             
   </class>
  </hibernate-mapping>

  <hibernate-mapping package="com.hibernate.onetone">
  <class name="Author" table="AUTHOR">
    <id name="id" column="AUTHOR_ID">
        <generator class="native"/>
    </id>
    <property name="name" column="NAME" />
    <property name="email" column="EMAIL" />  

   <!--  <property name="serialNumber" column="SerialNumber" /> -->
    <one-to-one name="book" class="Book" property-ref="author"
   constrained="true" cascade="all"/>    
   </class> 
  </hibernate-mapping>

   public class Book {
   private long id;
   private String title;
   private String description;
   private Date publishedDate;
   private Author author;       
   }
  public class Author {
  private long id;
  private String name;
  private String email;
  private Book book;     {
  }
like image 471
Pani Avatar asked Oct 25 '25 15:10

Pani


1 Answers

property-ref attribute is used to make an association bi-directional and specify which side of the association is the owner of the relationship. In the generated query it would result in an update on the foreign key only when the owner of the relationship is updated not the other side.

As in the docs

property-ref (optional): the name of a property of the associated class that is joined to this foreign key. If not specified, the primary key of the associated class is used.

Please also look at the below link for more information

http://www.allappforum.com/hibernate/hibernate_o_r_mapping_one_to_one_element.htm

http://www.allappforum.com/hibernate/hibernate_o_r_mapping_many_to_one_element.htm

It would be great if you could also provide you mapping and execution code so that i can make the explanation more clear:)

like image 139
Zulfi Avatar answered Oct 28 '25 07:10

Zulfi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!