简体   繁体   English

复合主键,使用@IdClass - 列“id”不能是 null

[英]Composite Primary key, using @IdClass - Column 'id' cannot be null

I have an entity with a composite primary key.我有一个具有复合主键的实体。

id - version will be the primary key. id - version将是主键。

id ID version版本 column A A列
1 1个 1 1个 some data一些数据
1 1个 2 2个 some data一些数据
2 2个 1 1个 some data一些数据
2 2个 2 2个 some data一些数据

I am using @IdClass for handling the composite primary key.我正在使用@IdClass来处理复合主键。

@Entity
@IdClass(MyKey.class)
public class YourEntity {
   @Id
   private int id;
   @Id
   private int version;
}


public class MyKey implements Serializable {
   private int id;
   private int version;
}

When I want to insert new row to the table, in other words I want to add new id , it complains that Column 'id' cannot be null .当我想向表中插入新行时,换句话说,我想添加新的id时,它会抱怨Column 'id' cannot be null

I don't want id be null .我不希望idnull According to my table, when I insert new row, it should add new id with value 3 .根据我的表,当我插入新行时,它应该添加值为3的新id

If I understand you correctly, you want to use AUTO_INCREMENT for id column.如果我理解正确的话,您想对id列使用AUTO_INCREMENT You should be able to use @GeneratedValue(strategy = GenerationType.IDENTITY) for id field of your entity.您应该能够将@GeneratedValue(strategy = GenerationType.IDENTITY)用于实体的id字段。 But, unfortunately, you can not do it due to HHH-9662 .但是,不幸的是,由于HHH-9662你不能这样做。 And this is not critical bug as it is not violate jpa specification.这不是严重错误,因为它没有违反 jpa 规范。

As workaround, you can use an approach that was described in Vlad Mihalcea's article .作为解决方法,您可以使用 Vlad Mihalcea 的文章中描述的方法。

Assuming that you have the following table:假设您有下表:

create table test_my_entity (
    id int not null AUTO_INCREMENT,
    version int,
    name  varchar(50),

    primary key (id, version)
);

You can use the following mapping:您可以使用以下映射:

import org.hibernate.annotations.SQLInsert;
import javax.persistence.EmbeddedId;
// ...

@Entity
@Table(name = "test_my_entity")
@SQLInsert(sql = "insert into test_my_entity(name, id, version) values (?, ?, ?)")
public class MyEntity {

    @EmbeddedId
    private MyEntityPk pk;

    @Column(name = "name")
    private String name;

    // getters and setters ...
}


@Embeddable
public class MyEntityPk implements Serializable {
    private int id;
    private int version;

    public MyEntityPk() {
    }

    public MyEntityPk(int version) {
        this.version = version;
    }

    public MyEntityPk(int id, int version) {
        this.id = id;
        this.version = version;
    }

    public int getId() {
        return id;
    }

    public int getVersion() {
        return version;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyEntityPk that = (MyEntityPk) o;
        return version == that.version && id == that.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, version);
    }

}

and example how you can insert new row:以及如何插入新行的示例:

MyEntity myEntity = new MyEntity();
myEntity.setPk(new MyEntityPk(5));
myEntity.setName("Yulia");
entityManager.persist(myEntity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用IdClass使用3个主键的Spring JPA数据JPA复合 - Spring jpa data jpa composite with 3 primary key using IdClass CrudRepository,无法使用 IdClass 保存具有复合主键的实体 - CrudRepository, cant save entity with a composite primary key using IdClass 使用JPA创建复合主键时如何解决找不到IdClass属性的问题 - How to resolve IdClass Property not found issue when creating composite primary key using JPA Spring CrudRepository无法在复合主键中共享生成的ID - Spring CrudRepository cannot share generated id in composite primary key 使用 IdClass 声明的复合键的用于 deleteById 的 JPA 查询 - JPA Query to deleteById for a Composite Key declared using IdClass 不是使用@IdClass创建和组合ID的托管类型 - Not a managed type creating and composite id with @IdClass 没有@EmbeddedId或@IdClass的超级简单复合键 - Super simple composite key without @EmbeddedId or @IdClass EclipseLink错误:实体类未指定主键。 它应该定义@ Id,@ EmbeddedId或@IdClass - EclipseLink error: Entity class has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass 使用 DataJpaTest 时在 Hibernate 上使用 IdClass 的复合键作为复合键 - Composite keys using IdClass on Hibernate for composite keys when using DataJpaTest Spring JPA 复合键:此类未定义 IdClass - Spring JPA Composite key: This class does not define an IdClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM