[英]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 ![]() |
version![]() |
column A ![]() |
---|---|---|
1 ![]() |
1 ![]() |
some data![]() |
1 ![]() |
2 ![]() |
some data![]() |
2 ![]() |
1 ![]() |
some data![]() |
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
.我不希望
id
是null
。 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.