映射到几个表

时间:2012-07-31 13:05:01

标签: hibernate nhibernate nhibernate-mapping hibernate-mapping

我有3张桌子:

    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) 
    );

我在桌子上映射如:

<?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>

我的问题是我如何映射表PERSONAL?我的问题是PERSONAL没有外键,因此我不知道如何映射PERSONAL。我需要从该表中获取NAMESURNAMEEMAIL

希望有人可以帮助我

谢谢!

2 个答案:

答案 0 :(得分:2)

Booking和Personal之间的关联可以映射为unidirectional many-to-one association

使用property-ref属性指定要加入的个人主键以外的字段,在您的情况下,它是与数据库列PERSONAL_ID对应的personalId属性,而不是Personal类的ID。 / p>

  

property-ref(可选)

     

相关属性的名称   加入此外键的类。如果没有指定,   使用关联类的主键。

请参阅Hibernate - why use many-to-one to represent a one-to-one?

上的此stackoverflow问题

映射

<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>

爪哇

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;
}

答案 1 :(得分:0)

如何使用HQL获取该表对象的列表?要获取映射对象,必须定义FKs不是它,否则hibernate将如何映射列?