我有项目类:
@Entity
public class Project implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", unique=true )
private Long id;
@Column(name="name", nullable=false, unique = true)
private String name;
@OneToMany(mappedBy="projectid", fetch = FetchType.EAGER)
private Set<Collaborator> collaborators = new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
然后协作者:
@Entity
public class Collaborator implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(nullable = true)
private Project projectid;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
public Project getProjectid() {
return projectid;
}
public void setProjectid(Project projectid) {
this.projectid = projectid;
}
在我的DAO中,我想执行此查询:
@Query("select c from Collaborator as c where c.projectid = ?1 and c.showable = true")
List<Collaborator> findAllShowableCollaboratorsByProjectidAndShowableTrue(Long projectId);
但是我收到了这个错误:
引起:org.hibernate.property.access.spi.PropertyAccessException:通过反射持久属性[gestionprojet.java]访问字段[private java.lang.Long gestionprojet.java.entities.beans.Project.id]时出错。 entities.beans.Project#id]:1 在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:4647) 在org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4358) 在org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226) at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:276) at org.hibernate.type.EntityType.getIdentifier(EntityType.java:462) 在org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:161) 在org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53) 在org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:628) 在org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1956) 在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1909) 在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1887) 在org.hibernate.loader.Loader.doQuery(Loader.java:932) 在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349) 在org.hibernate.loader.Loader.doList(Loader.java:2615) 在org.hibernate.loader.Loader.doList(Loader.java:2598) 在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2430) 在org.hibernate.loader.Loader.list(Loader.java:2425) 在org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502) 在org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371) 在org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) 在org.hibernate.internal.SessionImpl.list(SessionImpl.java:1460) 在org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1426) 在org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1398) 在org.hibernate.Query.getResultList(Query.java:417) 在org.springframework.data.jpa.repository.query.JpaQueryExecution $ CollectionExecution.doExecute(JpaQueryExecution.java:114) 在org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:78) 在org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:102) 在org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:92) at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482) at org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 在org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:280) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ...省略了59个常用帧 引起:java.lang.IllegalArgumentException:无法将java.lang.Long字段gestionprojet.java.entities.beans.Project.id设置为java.lang.Long at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source) at sun.reflect.UnsafeObjectFieldAccessorImpl.get(Unknown Source) at java.lang.reflect.Field.get(Unknown Source) 在org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:67) ...省略了98个常见帧
我的环境:
我试过回滚hibernate t版本4但仍然是同样的问题。
任何帮助都将不胜感激。谢谢你
答案 0 :(得分:0)
projectid
中的Collaborator
是Project
个对象,而不是Long
个对象。因此,将您的存储库方法更改为:
@Query("select c from Collaborator as c where c.projectid.id = ?1 and c.showable = true")
List<Collaborator> findAllShowableCollaboratorsByProjectidAndShowableTrue(Long projectId);
请注意,建议不要使用位置参数绑定,因为无法更改参数的顺序(在您的情况下,它似乎很好,因为只有一个参数)。我建议你改变一下:
@Query("select c from Collaborator as c where c.projectid.id = :pid and c.showable = true")
List<Collaborator> findAllShowableCollaboratorsByProjectidAndShowableTrue(@Param("pid") Long pid);
将projectid
用作类型为Project
的字段名称有点令人困惑。将名称更改为project
更好看。