记录存在时EntityNotFoundException

时间:2019-07-04 12:47:31

标签: java mysql hibernate jpa

我有一个表BusStops,它与另一个表RouteTrip有外键关系,该应用程序使用Hibernate,因此使用@ManyToOne反映在代码中。约束,突然我在检索数据时看到以下消息类型。

javax.persistence.EntityNotFoundException: Unable to find com.mys.employeeproject.model.RouteTrip with id 1
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:162)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:230)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:281)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:124)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:92)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1257)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1140)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:682)
at org.hibernate.type.EntityType.resolve(EntityType.java:464)
at org.hibernate.type.ManyToOneType.resolve(ManyToOneType.java:239)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:172)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1151)
at org.hibernate.loader.Loader.processResultSet(Loader.java:1010)
at org.hibernate.loader.Loader.doQuery(Loader.java:948)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
at org.hibernate.loader.Loader.doList(Loader.java:2689)
at org.hibernate.loader.Loader.doList(Loader.java:2672)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2506)
at org.hibernate.loader.Loader.list(Loader.java:2501)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:504)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:395)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:220)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1508)
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1537)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)
at com.mys.employeeproject.service.LoopService.getAllRouteTripStops(LoopService.java:4144)
at com.mys.employeeproject.service.LoopService$$FastClassBySpringCGLIB$$3f06600.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.mys.employeeproject.service.LoopService$$EnhancerBySpringCGLIB$$6b337c86.getAllRouteTripStops(<generated>)
at com.mys.employeeproject.controller.LoopController.getAllRouteTripStops(LoopController.java:10744)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)

下面是这种情况的两个类。

RouteTrip.java:

@Entity
@Table(name = "route_trip")
public class RouteTrip implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

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

@JoinColumn(name = "academic_id", referencedColumnName = "id", nullable = true)
@OneToOne(optional = false)
private AcademicPeriod academicId;

@Column(name = "type")
private int type;

@Column(name = "start_date")
private Date startDate;

@Column(name = "end_date")
private Date endDate;

@Column(name = "active")
private boolean active;

@Column(name = "created_by")
private long createdBy;

@Column(name = "created_on")
private Timestamp createdOn;

@Column(name = "modified_by")
private long modifiedBy;

@Column(name = "modified_on")
private Timestamp modifiedOn;

public RouteTrip() {

}

public RouteTrip(int id, String name, AcademicPeriod academicId, int type, Date startDate, Date endDate, boolean active, long createdBy, Timestamp createdOn, long modifiedBy, Timestamp modifiedOn) {
    this.id = id;
    this.name = name;
    this.academicId = academicId;
    this.type = type;
    this.startDate = startDate;
    this.endDate = endDate;
    this.active = active;
    this.createdBy = createdBy;
    this.createdOn = createdOn;
    this.modifiedBy = modifiedBy;
    this.modifiedOn = modifiedOn;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public AcademicPeriod getAcademicId() {
    return academicId;
}

public void setAcademicId(AcademicPeriod academicId) {
    this.academicId = academicId;
}

public int getType() {
    return type;
}

public void setType(int type) {
    this.type = type;
}

public Date getStartDate() {
    return startDate;
}

public void setStartDate(Date startDate) {
    this.startDate = startDate;
}

public Date getEndDate() {
    return endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

public boolean isActive() {
    return active;
}

public void setActive(boolean active) {
    this.active = active;
}

public long getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(long createdBy) {
    this.createdBy = createdBy;
}

public Timestamp getCreatedOn() {
    return createdOn;
}

public void setCreatedOn(Timestamp createdOn) {
    this.createdOn = createdOn;
}

public long getModifiedBy() {
    return modifiedBy;
}

public void setModifiedBy(long modifiedBy) {
    this.modifiedBy = modifiedBy;
}

public Timestamp getModifiedOn() {
    return modifiedOn;
}

public void setModifiedOn(Timestamp modifiedOn) {
    this.modifiedOn = modifiedOn;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    RouteTrip routeTrip = (RouteTrip) o;
    return id == routeTrip.id &&
            type == routeTrip.type &&
            active == routeTrip.active &&
            createdBy == routeTrip.createdBy &&
            modifiedBy == routeTrip.modifiedBy &&
            Objects.equals(name, routeTrip.name) &&
            Objects.equals(academicId, routeTrip.academicId) &&
            Objects.equals(startDate, routeTrip.startDate) &&
            Objects.equals(endDate, routeTrip.endDate) &&
            Objects.equals(createdOn, routeTrip.createdOn) &&
            Objects.equals(modifiedOn, routeTrip.modifiedOn);
}

@Override
public int hashCode() {
    return Objects.hash(id, name, academicId, type, startDate, endDate, active, createdBy, createdOn, modifiedBy, modifiedOn);
}
}

BusStops.java:

@Entity
@Table(name = "bus_stops")
public class BusStops implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

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

@ManyToOne
@JoinColumn(name="route_trip_id", referencedColumnName = "id", nullable=true)
private RouteTrip routeTripId;

@Column(name = "active")
private boolean active;

@Column(name = "created_by")
private long createdBy;

@Column(name = "created_on")
private Timestamp createdOn;

@Column(name = "modified_by")
private long modifiedBy;

@Column(name = "modified_on")
private Timestamp modifiedOn;

public BusStops() {

}

public BusStops(int id, String name, RouteTrip routeTripId, boolean active, long createdBy, Timestamp createdOn, long modifiedBy, Timestamp modifiedOn) {
    this.id = id;
    this.name = name;
    this.routeTripId = routeTripId;
    this.active = active;
    this.createdBy = createdBy;
    this.createdOn = createdOn;
    this.modifiedBy = modifiedBy;
    this.modifiedOn = modifiedOn;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public RouteTrip getRouteTripId() {
    return routeTripId;
}

public void setRouteTripId(RouteTrip routeTripId) {
    this.routeTripId = routeTripId;
}

public boolean getActive() {
    return active;
}

public void setActive(boolean active) {
    this.active = active;
}

public long getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(long createdBy) {
    this.createdBy = createdBy;
}

public Timestamp getCreatedOn() {
    return createdOn;
}

public void setCreatedOn(Timestamp createdOn) {
    this.createdOn = createdOn;
}

public long getModifiedBy() {
    return modifiedBy;
}

public void setModifiedBy(long modifiedBy) {
    this.modifiedBy = modifiedBy;
}

public Timestamp getModifiedOn() {
    return modifiedOn;
}

public void setModifiedOn(Timestamp modifiedOn) {
    this.modifiedOn = modifiedOn;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    BusStops busStops = (BusStops) o;
    return id == busStops.id &&
            active == busStops.active &&
            createdBy == busStops.createdBy &&
            modifiedBy == busStops.modifiedBy &&
            Objects.equals(name, busStops.name) &&
            Objects.equals(routeTripId, busStops.routeTripId) &&
            Objects.equals(createdOn, busStops.createdOn) &&
            Objects.equals(modifiedOn, busStops.modifiedOn);
}

@Override
public int hashCode() {
    return Objects.hash(id, name, routeTripId, active, createdBy, createdOn, modifiedBy, modifiedOn);
}
}

我在执行以下代码时遇到此错误。

Query query = session.createQuery("FROM BusStops where active = 1 and routeTripId.id = :routeTripId");
        query.setParameter("routeTripId", routeTripId);
        busStopList = query.list();

即使数据库中存在数据,为什么也会出现此错误?奇怪的是,如果我在上述查询之前执行另一个查询,就不会出现此错误,并且我会得到数据

List<RouteTrip> routeTripList = new ArrayList<RouteTrip>();
        Query query1 = session.createQuery("FROM RouteTrip where active = 1 and id = :id");
        query1.setParameter("id", routeTripId);
        routeTripList = query1.list();
        Query query = session.createQuery("FROM BusStops where active = 1 and routeTripId.id = :routeTripId");
        query.setParameter("routeTripId", routeTripId);
        busStopList = query.list();

1 个答案:

答案 0 :(得分:1)

问题是您将routeTripId.id(ID)与routeTripId(对象)进行了比较。

您可以尝试:

Query query = session.createQuery("FROM BusStops where active = 1 and routeTripId.id = :routeTripId");
    query.setParameter("routeTripId", routeTripId.getId());
    busStopList = query.list();