1

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.

Hari
  • 11
  • 4
  • You can have only one primary key. – Jens Jun 08 '15 at 06:36
  • Have you seen this `http://stackoverflow.com/questions/21284175/how-to-make-two-column-as-a-primary-key-in-hibernate-annotation-class` – Rookie007 Jun 08 '15 at 06:38
  • How do you want to have two "first" elements? – Stultuske Jun 08 '15 at 06:38
  • i need only one object for a day – Hari Jun 08 '15 at 06:39
  • Hari: the point is: a "primary" key (primary, being the "first" element), is unique. You can create keys based on more than one column, but you can not have more than one primary key in one table. – Stultuske Jun 08 '15 at 06:42
  • Your ID column is based on generated serial number. Therefore, it is unique already, and you don't need a composite key. And of course, a composite key is not the same as having two keys. – RealSkeptic Jun 08 '15 at 06:44
  • @Stultuske actually what i need is one key i.e id should be unique and another one i.e date is also be unique. – Hari Jun 08 '15 at 07:16

1 Answers1

0

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

Praba
  • 1,373
  • 10
  • 30