【问题标题】:i'm stuck when I'm trying to make two primary key's in hibernate当我试图让两个主键处于休眠状态时,我被卡住了
【发布时间】:2015-06-08 06:35:29
【问题描述】:

我有一个像这样的课堂出勤

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

    }
}

现在我正在尝试使用休眠映射复合 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>

当我这样做时,我得到了错误元素类型“key-property”的内容必须匹配“(meta *,column *,type?)”。 如何设置两个主键,一个必须自动递增。在此先感谢。

【问题讨论】:

  • 你只能有一个主键。
  • 你看过这个http://stackoverflow.com/questions/21284175/how-to-make-two-column-as-a-primary-key-in-hibernate-annotation-class
  • 你希望如何拥有两个“第一”元素?
  • 我一天只需要一个对象
  • Hari:重点是:一个“主”键(主,是“第一个”元素)是唯一的。您可以基于多个列创建键,但一个表中不能有多个主键。

标签: java hibernate


【解决方案1】:

您当前的配置未遵循为复合 ID 元素设置的架构。 这是复合 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>

这是一个示例配置:

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

相关文档在这里:https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-compositeid

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 2023-02-09
    • 1970-01-01
    • 2017-05-29
    相关资源
    最近更新 更多