gpt4 book ai didi

java - Hibernate ManyToOne 映射不在 PKey 上

转载 作者:太空宇宙 更新时间:2023-11-04 14:33:15 27 4
gpt4 key购买 nike

我有一些实体:

@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();
}
}

我正在尝试从此 native 查询获取事件:

SELECT * FROM event WHERE event_id>4442 AND host_id=35;

并得到此异常javax.persistence.EntityNotFoundException:无法找到id为35的entity.Host但是在我的数据库中我有host_id为35的主机。我如何将我的事件实体映射到主机host_id 列?谢谢。

更新:

   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();

这是完整的异常(exception):

    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)

最佳答案

尝试使用此代码进行 SQLInjection

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);

关于java - Hibernate ManyToOne 映射不在 PKey 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25892270/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com