i'm stuck when I'm trying to make two primary key's in hibernate

491 Views Asked by At

i have a class attendance like

public class Attendance  implements Serializable
{
    private Integer id,userId;
    private Date date;
    private  OfficeTime officeTime;
    private Set<TimeSlice> timeSlices;
    public Attendance()
    {

    }
}

now i'm try to make two primary key's using hibernate mapping composite-id

 <hibernate-mapping>
          <class name="Attendance">
           <composite-id>
              <key-property name="id"><generator class="increment"/></key-property>
              <key-property name="date" />
          </composite-id>

          <property name="userId" />

          <set name="timeSlices" cascade="all" >
            <key column="attendanceId" />
            <one-to-many class="TimeSlice" />
          </set>

          <many-to-one name="officeTime" class="OfficeTime"
            column="office_id" unique="true" not-null="true"
            cascade="all" />

          </class>

          </hibernate-mapping>

while am doing like this i got error The content of element type "key-property" must match "(meta*,column*,type?)". how to set two primary key's and one must be increment automatically.Thanks in advance.

1

There are 1 best solutions below

0
On

Your current configuration is not following the schema that is set for the composite-id element. Here is the schema doc for the composite id:

<composite-id name="propertyName" class="ClassName" mapped="true|false" access="field|property|ClassName"> node="element-name|." <key-property name="propertyName" type="typename" column="column_name"/> <key-many-to-one name="propertyName class="ClassName" column="column_name"/> ...... </composite-id>

This is a sample configuration:

<composite-id>
            <key-many-to-one name="username" class="org.myclass.UserDB" column="user_name"/>
            <key-property name="role" type="string" column="role_name"/>
        </composite-id>      

The documentation for this is here: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-compositeid