我有一些实体:
@Entity
public class Event{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "event_id")
private long eventId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "host_id")
private Host host;
}
@Entity
public class Host{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "host_id")
private long hostId;
}
@Repository
public class EventDao{
@PersistenceContext
private EntityManager em;
@Transactional
public List<Event> findEventAfterId(long eventId, long hostId){
Query q=em.createNativeQuery("SELECT * FROM event WHERE event_id>? AND host_id=?, Event.class);
q.setParameter(1, eventId);
q.setParameter(2, hostId);
return (List<Event>)q.getResultList();
}
}
我正在尝试从此原生查询中获取事件:
SELECT * FROM event WHERE event_id>4442 AND host_id=35;
并获得此异常javax.persistence.EntityNotFoundException: Unable to find entity.Host with id 35
但是在我的数据库中我有host_id 35的主机。如何使用host_id列将我的事件实体映射到主机?谢谢。
UPD:
Query q=em.createNativeQuery("SELECT * FROM event WHERE event_id>? AND host_id=?, Event.class);
q.setParameter(1, eventId);
q.setParameter(2, hostId);
return (List<Event>)q.getResultList();
以下是完全例外:
javax.persistence.EntityNotFoundException: Unable to find monitoring.reporter.entity.Host with id 35
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:181)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:218)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:274)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:150)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:989)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:716)
at org.hibernate.type.EntityType.resolve(EntityType.java:502)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1114)
at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
at org.hibernate.loader.Loader.doQuery(Loader.java:920)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
at org.hibernate.loader.Loader.doList(Loader.java:2553)
at org.hibernate.loader.Loader.doList(Loader.java:2539)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
at org.hibernate.loader.Loader.list(Loader.java:2364)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:353)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1873)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
答案 0 :(得分:0)
尝试SQL注入的代码
Query q=em.createNativeQuery("SELECT * FROM event WHERE event_id>:event_id AND host_id=:host_id, Event.class);
q.setParameter("event_id", eventId);
q.setParameter("host_id", hostId);
答案 1 :(得分:0)
你的错误太小了,要清楚地解释清楚我必须在这里写出很多理论,这里的好消息是我有足够的耐心在这里写下所有的理论。
让我从Event类开始,在Event类中,您有Host和Event指向Host的主键。
在Host类中,主键名是&#39; id&#39;,但在Event类中,对应的列名是host_id。
最后你的表结构如下所示
在Event类中,host_id存在,但它指向Host类的主键,即id
所以Event的host_id指向Host的id列,现在让我们说主机表id列由1组成,因为它是自动递增的,而host_id = 35就是你。
但在事件表中,host_id指向值为1的主机表id。 但您的查询是在事件表中搜索host_id = 35。 要避免此问题,在Host类中,您必须自动生成host_id主键并且不生成,将手动值分配给host_id。你可以在下面看到相应的类:
@Entity
public class Event{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "event_id")
private long eventId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "host_id")
private Host host;
}
@Entity
public class Host{
@Column(name = "id")
private long id;
@Id
@Column(name = "host_id")
private long hostId;
}
@Repository
public class EventDao{
@PersistenceContext
private EntityManager em;
@Transactional
public List<Event> findEventAfterId(long eventId, long hostId){
Query q=em.createNativeQuery("SELECT * FROM event WHERE event_id>? AND host_id=?, Event.class);
q.setParameter(1, eventId);
q.setParameter(2, hostId);
return (List<Event>)q.getResultList();
}
}