Paks
Paks

Reputation: 1470

Mapping over several Tables

i have 3 tables:

    TABLE BOOKING (
  BOOK_ID  
  PERSONAL_ID                             
  ORGINIZER                      
  TITLE
    PRIMARY KEY ( BOOK_ID )
    FOREIGN KEY ( PERSONAL_ID)
);

TABLE ROOM
(
  ID
  BOOK_ID
  FROM                  
  UNTIL                        
  QUANTITY                   
  PRIMARY KEY ( ID) 
  FOREIGN KEY ( BOOK_ID)
);

TABLE PERSONAL
    (
      ID
      PERSONAL_ID
      NAME
      SURNAME
      EMAIL         
      PRIMARY KEY ( ID) 
    );

I map over the table ROOM like:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<class name="Depp.Domain.Booking, Depp.Core" >
    <id name="ID" unsaved-value="0" column="ID">
        <generator class="native">
            <!--<param name="sequence">GLOBALSEQUENCE</param>-->
        </generator>
    </id>
    <property name="Title" ></property>
    <property name="TitleEnabled" type="yes_no">
        <column name="THEMA_ENABLED"/>
    </property>
    <property name="ORGANIZER" column="PERSONAL_ID" not-null="true" ></property>
    <bag name="ROOM" inverse="true" lazy="true" cascade="all-delete-orphan" >
        <key column="BOOK_ID"/>
        <one-to-many class="Depp.Domain.ROOM, Depp.Core"/>
    </bag>

</class>

My Question is how can i map over the Table PERSONAL? My Problem is that PERSONALdoesn't have a FOREIGN KEY so i don't know how to map over PERSONAL. I need to get the NAME, SURNAME and EMAIL from that table.

Hope someone can help me

Thanks!

Upvotes: 0

Views: 204

Answers (2)

Joel Hudon
Joel Hudon

Reputation: 3215

The association between Booking and Personal can be map as a unidirectional many-to-one association.

Use property-ref attribute to specified the field other than the personal primary key to join with, in your case it`s the personalId attribute that correspond to the database column PERSONAL_ID instead of the ID of the Personal class.

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.

See this stackoverflow question on Hibernate - why use many-to-one to represent a one-to-one?

Mapping

<class name="Booking" >
    <id name="ID" unsaved-value="0" column="ID">
        <generator class="native">
            <!--<param name="sequence">GLOBALSEQUENCE</param>-->
        </generator>
    </id>
    ...
    <many-to-one name="personal" column="PERSONAL_ID"   property-ref="personalId" not-null="true"/>
    ...
</class>

<class name="Personal">
    <id name="id" column="ID">
        <generator class="native"/>
    </id>
    
    <property name="personalId" column="personal_id" />
    <property name="name" />
    <property name="surname" />
    <property name="email" />
    
</class>

Java

public class Booking{

    private Long id;

    private Personal personal;
    ...
}



public class Personal 
{
    Long id;
        
    private Long personalId;   
    private String name;        
    private String surname;
    private String email;
}

Upvotes: 2

mrd081
mrd081

Reputation: 279

how about using HQL to get list of that table objects ? TO get mapped objects, there must be FKs defined isn't it, otherwise how hibernate will be able to map columns ?

Upvotes: 0

Related Questions