EntityNotfoundException-Hibernate和Springboot
我正在使用Springboot和hibernate以及MS SQL中的数据库。我有以下两个实体类别PS和CFO。CFO有两个主键—CFO_ID和LAST_UPDATE_DTS,它们在CFO表中都作为外键引用。当我尝试使用getter方法获取PS并访问cfo时,即使数据库中存在数据,我也会得到EntityNotFound异常EntityNotfoundException-Hibernate和Springboot,hibernate,spring-boot,Hibernate,Spring Boot,我正在使用Springboot和hibernate以及MS SQL中的数据库。我有以下两个实体类别PS和CFO。CFO有两个主键—CFO_ID和LAST_UPDATE_DTS,它们在CFO表中都作为外键引用。当我尝试使用getter方法获取PS并访问cfo时,即使数据库中存在数据,我也会得到EntityNotFound异常 @Entity @Table(name = "PS") @Data @IdClass(PlanSponsorId.class) public class PS impleme
@Entity
@Table(name = "PS")
@Data
@IdClass(PlanSponsorId.class)
public class PS implements Serializable {
private static final long serialVersionUID = -1685251075449473233L;
@Id
@Column(name = "PS_ID", columnDefinition = "INTEGER")
private Integer ps_id;
@Id
@Column(name = "LAST_UPDATE_DTS", columnDefinition = "DATETIME")
private Date lastUpateTimestamp;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(name = "CFO_ID",referencedColumnName="CFO_ID"),
@JoinColumn(name = "CFO_LAST_UPDATE_DTS",referencedColumnName="LAST_UPDATE_DTS") })
private CFO cfo;
//other fields
}
@Data
@NoArgsConstructor
public class PlanSponsorId implements Serializable {
private static final long serialVersionUID = 2224235254147911360L;
private Date lastUpateTimestamp;
private Integer ps_id;
}
@Entity
@Table(name = "CFO")
@Data
@IdClass(value = CFOModelId.class)
public class CFO implements Serializable {
private static final long serialVersionUID = -1082267441376710907L;
@Id
@Column(name = "CFO_ID", columnDefinition = "CHAR(3)")
private String cfoIdentifier;
@Id
@Column(name = "LAST_UPDATE_DTS", columnDefinition = "DATETIME")
private Date lastUpateTimestamp;
@OneToMany(mappedBy="cfo")
private List<PS> ps;
//other fields
}
@Data
@NoArgsConstructor
public class CFOModelId implements Serializable {
private static final long serialVersionUID = -325079478600059751L;
private String cfoIdentifier;
private Date lastUpateTimestamp;
}
javax.persistence.EntityNotFoundException: Unable to find com.abc.CFO with id CFOModelId(cfoIdentifier=1 , lastUpateTimestamp=2019-12-30 01:36:08.773)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:162)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:285)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:180)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:309)
at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45)
当我只保留一个作为主键——CFO_ID或LAST_UPDATE_DTS,并将Idclass全部删除时,它工作得非常好。这让我觉得idclass有问题,但我找不到任何问题。有人能帮忙吗?根据Hibernate文档,Hibernate似乎允许这样做,但它并不符合JPA
5.1.2.1.2. Multiple id properties without identifier type
Another, arguably more natural, approach is to place @Id on multiple properties of your entity. This approach is only supported by Hibernate (not JPA compliant) but does not require an extra embeddable component.
对于符合JPA的解决方案,您必须为此设置指定额外的@IDClass或使用EmbeddedId:
参考:
例如:
@Entity
@IdClass(ProjectId.class)
public class Project {
@Id int departmentId;
@Id long projectId;
}
谢谢你的回答。我也尝试过embeddedid,但没有成功。现在,我从上次更新dts上的列注释中删除了列定义,这样做没有问题。但我不确定这是否是正确的解决方案。