java.lang.IllegalArgumentException:使用Composite-id时需要IdClass映射

时间:2018-11-22 23:50:36

标签: java hibernate hibernate-mapping

我正在尝试使用Hibernate映射其中具有 composite-id 的表。我有一个映射器文件,一个映射该表的实体类和一个复合ID的序列化所需的IdClass。但是我遇到IllegalArgumentException: expecting IdClass mapping错误。这是映射器文件:

<hibernate-mapping>
    <class name="database.PermissionsEntity" table="Permissions" schema="interapp">
        <composite-id mapped="true" class="database.PermissionsEntityPK">
            <key-property name="id">
                <column name="id" sql-type="int(11)"/>
            </key-property>
            <key-property name="level">
                <column name="level" sql-type="char(1)" length="1"/>
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>

这是我写的Entity类:

@Entity
@Table(name = "Permissions", schema = "interapp", catalog = "")
@IdClass(PermissionsEntityPK.class)
public class PermissionsEntity {
    @Id
    private int id;

    @Id
    private String level;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PermissionsEntity that = (PermissionsEntity) o;

        if (id != that.id) return false;
        if (level != null ? !level.equals(that.level) : that.level != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (level != null ? level.hashCode() : 0);
        return result;
    }
}

我正在使用的IdClass是这样的:

public class PermissionsEntityPK implements Serializable {
    private int id;
    private String level;

    public PermissionsEntityPK(int id, String level) {
        this.id = id;
        this.level = level;
    }

    public PermissionsEntityPK() {

    }

    @Column(name = "id", nullable = false)
    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "level", nullable = false, length = 1)
    @Id
    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PermissionsEntityPK that = (PermissionsEntityPK) o;

        if (id != that.id) return false;
        if (level != null ? !level.equals(that.level) : that.level != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (level != null ? level.hashCode() : 0);
        return result;
    }
}

我认为可能有一些@Id放错了位置,但我不知道在哪里。我在这里想念什么?

0 个答案:

没有答案