无法将java.lang.Long字段Event.idEvent设置为java.lang.Long

时间:2019-07-03 08:37:41

标签: java hibernate java-ee

我有一个实体“ Event”,当我想通过id选择一个Event时,出现此错误: 无法将java.lang.Long Event.idEvent设置为java.lang.Long

javax.persistence.PersistenceException: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Long Event.idEvent] by reflection for persistent property [Event#idEvent] : 1
    org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1538)
    org.hibernate.query.Query.getResultList(Query.java:165)
    fr.eni.sortircom.dal.dao.hibernate.HibernateRegistrationDAO.selectById(HibernateRegistrationDAO.java:19)
    fr.eni.sortircom.bll.RegistrationManager.selectParticipantEvent(RegistrationManager.java:27)
    fr.eni.sortircom.servlet.ShowEventServlet.doGet(ShowEventServlet.java:28)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
cause mère

org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Long fr.eni.sortircom.bo.Event.idEvent] by reflection for persistent property [fr.eni.sortircom.bo.Event#idEvent] : 1
    org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:75)
    org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:224)
    org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4940)
    org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4640)
    org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:297)
    org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:347)
    org.hibernate.type.EntityType.getIdentifier(EntityType.java:495)
    org.hibernate.type.EntityType.nullSafeSet(EntityType.java:288)
    org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53)
    org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:645)
    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2006)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1920)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1898)
    org.hibernate.loader.Loader.doQuery(Loader.java:937)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
    org.hibernate.loader.Loader.doList(Loader.java:2695)
    org.hibernate.loader.Loader.doList(Loader.java:2678)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2512)
    org.hibernate.loader.Loader.list(Loader.java:2507)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:504)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:396)
    org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:224)
    org.hibernate.internal.SessionImpl.list(SessionImpl.java:1538)
    org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1561)
    org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1529)
    org.hibernate.query.Query.getResultList(Query.java:165)
    fr.eni.sortircom.dal.dao.hibernate.HibernateRegistrationDAO.selectById(HibernateRegistrationDAO.java:19)
    fr.eni.sortircom.bll.RegistrationManager.selectParticipantEvent(RegistrationManager.java:27)
    fr.eni.sortircom.servlet.ShowEventServlet.doGet(ShowEventServlet.java:28)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
cause mère

java.lang.IllegalArgumentException: Can not set java.lang.Long field fr.eni.sortircom.bo.Event.idEvent to java.lang.Long
    sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    java.lang.reflect.Field.get(Field.java:393)
    org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:71)
    org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:224)
    org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4940)
    org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4640)
    org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:297)
    org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:347)
    org.hibernate.type.EntityType.getIdentifier(EntityType.java:495)
    org.hibernate.type.EntityType.nullSafeSet(EntityType.java:288)
    org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53)
    org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:645)
    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2006)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1920)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1898)
    org.hibernate.loader.Loader.doQuery(Loader.java:937)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
    org.hibernate.loader.Loader.doList(Loader.java:2695)
    org.hibernate.loader.Loader.doList(Loader.java:2678)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2512)
    org.hibernate.loader.Loader.list(Loader.java:2507)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:504)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:396)
    org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:224)
    org.hibernate.internal.SessionImpl.list(SessionImpl.java:1538)
    org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1561)
    org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1529)
    org.hibernate.query.Query.getResultList(Query.java:165)
    fr.eni.sortircom.dal.dao.hibernate.HibernateRegistrationDAO.selectById(HibernateRegistrationDAO.java:19)
    fr.eni.sortircom.bll.RegistrationManager.selectParticipantEvent(RegistrationManager.java:27)
    fr.eni.sortircom.servlet.ShowEventServlet.doGet(ShowEventServlet.java:28)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是我的实体活动:

public class Event implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_event")
    private Long idEvent;

,这里是按ID选择:

 public Event selectById(Long id) {
        Session session = ConnectionProvider.getConnection();
        Query q = session.createQuery("FROM Event WHERE idEvent=:id");
        q.setParameter("id", id);
        List<Event> events = q.getResultList();
        Event event = events.get(0);
        return event;
    }

我检查了龙,他是正确的。而且我对另一个实体有相同的要求,并且可以正常工作。我不明白。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您的参数不匹配,即您尝试为用户类型指定Long作为参数:

您可以通过以下两种方式之一对其进行更改:我添加了一个示例示例

@Query("DELETE FROM Collaborator c WHERE c.user.id = :userId AND c.task.id =:taskId")
void deleteUserFromTask(@Param("userId") Long userId, @Param("taskId") Long taskId);

@Query("DELETE FROM Collaborator c WHERE c.user = :user AND c.task = :task")
void deleteUserFromTask(@Param("user") User user, @Param("task") Task task);

谢谢。

答案 1 :(得分:0)

不确定这是否真的有帮助,但似乎您选择了非常复杂的方式。您可以简单地在存储库中实现CrudRepository<Event, Long>接口,并使用其findById方法进行选择。

这实际上非常容易,只需遵循以下简单步骤即可。

I。实施CrudRepository

public interface EventRepository implements CrudRepository<Event, Long> {}

II。通过自动安装findById在服务中使用EventRepository方法。

Long id  = ...;
Optional<Event> event = eventRepository.findById(id);