我使用Spring Boot项目,我有以下实体,
@Entity
public class Ip {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "IP_ADDR_ID_2")
private Long id;
@Column(name = "IP_ADDRESS_2")
@NotEmpty
private String address;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "ip")
private List<IMAssociation> httpInfoMessages2;
}
下面提供了消息实体,
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "S_ID_2")
private Long sId;
@Column(name = "STATUS_ID_2")
private Long statusId;
@NotEmpty
@Column(name = "STATUS_2")
private String status;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "message")
private List<IMAssociation> message;
}
最后,实体提供了Ip和Message之间的关联,
@Entity
@Table(name = "IP_HTTP_MESSAGE")
//@IdClass(AssociationId.class)
public class IMAssociation implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Id
@Column(name = "IP_ADDRESS_ID")
private Long ipAddressId;
@Id
@Column(name = "HTTP_MESSAGE_ID")
private Long httpMessageId;
@ManyToOne
@PrimaryKeyJoinColumn(name = "ipAddressId", referencedColumnName = "IP_ADDR_ID_2")
// @JoinColumn(name = "ipAddressId", updatable = false, insertable = false, referencedColumnName = "IP_ADDR_ID_2")
private Ip ip;
@ManyToOne
@PrimaryKeyJoinColumn(name = "httpMessageId", referencedColumnName = "S_ID_2")
// @JoinColumn(name = "httpMessageId", updatable = false, insertable = false, referencedColumnName = "S_ID_2")
private Message message;
// @ManyToOne
@JoinColumn(name = "IP_ADDRESS_2", updatable = false, insertable = false, referencedColumnName = "IP_ADDRESS_2")
private String address;
@JoinColumn(name = "STATUS_CODE", updatable = false, insertable = false, referencedColumnName = "STATUS_ID_2")
private Long code;
@JoinColumn(name = "STATUS_MESSAGE", updatable = false, insertable = false, referencedColumnName = "STATUS_2")
private String codeMessage;
public IMAssociation() {
}
public IMAssociation(Long code, String codeMessage) {
this.code = code;
this.codeMessage = codeMessage;
}
}
List<IMAssociation>
是通过代码制作的,
List<IMAssociation> imAssociations = new ArrayList<>();
for(int j =0; j < 5; j++){
IMAssociation imAssociation = new IMAssociation(Long.valueOf(integerListEntry.getKey()),
message);
imAssociations.add(imAssociation);
}
Ip ip = new Ip('127.0.0.1', imAssociations);
List<Ip> ips = new ArrayList<>();
ips.add(ip);
当我尝试在数据库中保留List<Ip>
时,出现以下错误,
Caused by: org.springframework.orm.jpa.JpaSystemException: Could not set field value [POST_INSERT_INDICATOR] value by reflection : [class com.ef.entity.IMAssociation.id] setter of com.ef.entity.IMAssociation.id; nested exception is org.hibernate.PropertyAccessException: Could not set field value [POST_INSERT_INDICATOR] value by reflection : [class com.ef.entity.IMAssociation.id] setter of com.ef.entity.IMAssociation.id
Caused by: org.hibernate.PropertyAccessException: Could not set field value [POST_INSERT_INDICATOR] value by reflection : [class com.ef.entity.IMAssociation.id] setter of com.ef.entity.IMAssociation.id
at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:58) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.Component$ValueGenerationPlan.execute(Component.java:419) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.id.CompositeNestedGeneratedValueGenerator.generate(CompositeNestedGeneratedValueGenerator.java:97) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:765) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:758) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener$1.cascade(JpaPersistEventListener.java:80) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:398) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:162) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:431) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:363) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:326) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:162) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:111) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:278) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field com.ef.entity.IMAssociation.id to org.hibernate.id.IdentifierGeneratorHelper$2
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:1.8.0_144]
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[na:1.8.0_144]
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) ~[na:1.8.0_144]
这里有什么问题?
Update
我已更改IMAssociation
类及其下面提供的内容,
@Entity
@Table(name = "IP_HTTP_MESSAGE")
//@IdClass(AssociationId.class)
public class IMAssociation implements Serializable {
// @Id
// @GeneratedValue(strategy = GenerationType.IDENTITY)
// private Long id;
@Id
@Column(name = "IP_ADDRESS_ID")
private Long ipAddressId;
@Id
@Column(name = "HTTP_MESSAGE_ID")
private Long httpMessageId;
@ManyToOne
@PrimaryKeyJoinColumn(name = "IP_ADDRESS_ID", referencedColumnName = "IP_ADDR_ID_2")
// @JoinColumn(name = "ipAddressId", updatable = false, insertable = false, referencedColumnName = "IP_ADDR_ID_2")
private Ip ip;
@ManyToOne
@PrimaryKeyJoinColumn(name = "HTTP_MESSAGE_ID", referencedColumnName = "S_ID_2")
// @JoinColumn(name = "httpMessageId", updatable = false, insertable = false, referencedColumnName = "S_ID_2")
private Message message;
// @ManyToOne
@JoinColumn(name = "IP_ADDRESS_2", updatable = false, insertable = false, referencedColumnName = "IP_ADDRESS_2")
private String address;
@JoinColumn(name = "STATUS_CODE", updatable = false, insertable = false, referencedColumnName = "STATUS_ID_2")
private Long code;
@JoinColumn(name = "STATUS_MESSAGE", updatable = false, insertable = false, referencedColumnName = "STATUS_2")
private String codeMessage;
public IMAssociation() {
}
public IMAssociation(Long code, String codeMessage) {
this.code = code;
this.codeMessage = codeMessage;
}
}
现在,我在运行应用程序时遇到以下错误,
Caused by: javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.ef.entity.IMAssociation#IMAssociation{, ipAddressId=null, httpMessageId=null, ip=null, message=null, address='null', code=404, codeMessage='404_Not Found'}]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1664) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1152) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
答案 0 :(得分:2)
这是你的问题: https://hibernate.atlassian.net/browse/HHH-6044
SO中的类似帖子:Hibernate @GeneratedValue in a composite key
这里有3个选项:
IMAssociation
实体中自动生成的ID。您的复合密钥只包含2个属性 - ipAddressId
和httpMessageId
,它们分别来自Address
和Message
实体的派生/外键。ipAddressId
和httpMessageId
。id
,ipAddressId
和httpMessageId
,请删除ID自动生成(@GeneratedValue
注释)并手动分配您的IMAssociation
实体的ID值。答案 1 :(得分:0)
可能不需要ipAddressId
和httpMessageId
我认为保存这些值的列是通过@PrimaryKeyJoinColumn
注释在dblevel中自动生成的,并且可以在{{}}中访问该值{1}} IMAssociation
和ip.getMessage()
。