I am trying to map entity with my existing database table which is having two primary keys.
Out of two keys, one primary key is auto generated.
I am using `Spring Boot`, `JPA`, `Hibernate` and `MySQL`. I have used `@IdClass` to map with the composite primary class (with public constructor, setter/getters, same property names, equals and hash code methods).
`org.springframework.data.repository.CrudRepository` save method to save the entity.
下面的代码段。
@Entity
@Table(name = "data")
@IdClass(DataKey.class)
public class DeviceData {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private BigInteger id;
@Column(name="name")
private String name;
@Id
@Column(name="device_id")
private int deviceId;
getters/setters
}
public class DataKey implements Serializable {
private static final long serialVersionUID = 1L;
private BigInteger id;
private int deviceId;
//setter/getters
public DataKey() {
}
public DataKey(BigInteger id, int deviceId) {
this.id = id;
this.deviceId = deviceId;
}
public int hashCode() {
return Objects.hash(id, deviceId);
}
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof DataKey))
return false;
DataKey dk = (DataKey) obj;
return dk.id.equals(this.id) && dk.deviceId == (this.deviceId);}
}
I am using org.springframework.data.repository.CrudRepository save method for persisting the entity.
DeviceData data = new DeviceData();
data.setName(“ device1”); data.setDeviceId(“ 1123”);
dao.save(data); // dao扩展crudrepository接口。
但是我遇到了以下错误:
org.springframework.orm.jpa.JpaSystemException: Could not set field
value [POST_INSERT_INDICATOR] value by reflection.[class DataKey.id]
setter of DataKey.id; nested exception is
org.hibernate.PropertyAccessException: Could not set field value
[POST_INSERT_INDICATOR] value by reflection : [class class DataKey.id]
setter of class DataKey.id.
原因:java.lang.IllegalArgumentException:无法将java.math.BigInteger字段DataKey.id设置为org.hibernate.id.IdentifierGeneratorHelper $ 2
答案 0 :(得分:0)
我也曾在我的一个项目中尝试过相同的操作,但最终我发现无法在JPA中自动生成键属性之一(如果是复合主键)。因此,我通过在查询的帮助下调用序列来单独进行此操作。
答案 1 :(得分:0)
I have achieved this by creating a one orderId class with parameters -- Mysql
@Getter
@Setter
public class OrderId implements Serializable {
private int oId;
private int customerId;
private int productId;
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
OrderId orderId = (OrderId) o;
return oId == orderId.oId && customerId == orderId.customerId && productId == orderId.productId;
}
@Override
public int hashCode() {
return Objects.hash(oId, customerId, productId);
}
the class OrderData with orderid as auto generated field in mysql
@Entity
@Table(name = "OrderData")
@Getter
@Setter
@NoArgsConstructor
@IdClass(OrderId.class)
public class OrderData{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int oId;
@Id
private int customerId;
@Id
private int productId;
private int quanity;
private String paymentMode;
private String orderStatus;
}
答案 2 :(得分:0)
我能够使用序列生成器解决它。
@Id
@SequenceGenerator(name="my_seq", sequenceName="my_db_table_seq", allocationSize=1)
@GeneratedValue(generator = "my_seq")
@Column(name = "id", updatable = false, nullable = false)
private BigInteger id
在我的例子中,我使用的是 Postgres,我的表以 SERIAL
作为列数据类型在 pg_catalog.pg_sequences
表中也有一个对应的条目(例如 my_db_table_seq
在我的例子中)以上)。
如果你没有指定一个特定的命名序列生成器(而只是在你的 @GeneratedValue(strategy = GenerationType.SEQUENCE)
列上使用 @Id
),它看起来像 Hibernate 默认为一个名为 hibernate_sequence
的,我有看到了运行 db 命令 CREATE SEQUENCE hibernate_sequence START 1
以在数据库中创建的一些建议。但这将对通过 Hibernate 插入行的每个表使用 same 序列,所以可能不是一个好主意。最好使用在数据库中为该表创建的序列,然后使用 @SequenceGenerator
注释进行明确。
在具有 @GeneratedValue(strategy = GenerationType.IDENTITY)
的实体的复合键中使用 @IdClass
似乎确实存在问题,因此如果 Hibernate 能够支持此功能就好了。
但看起来序列生成器是一种有效的解决方法,所以这就是我要使用的方法。