java.lang.IllegalArgumentException : Unable to locate Attribute with the the given name

标签 java spring jpa spring-data-jpa

我有一个使用 JPA 和 Oracle DB 的 Spring 应用程序。我的表具有以下结构:

CREATE TABLE MODULE
(
 id NUMBER(19,0),
 name VARCHAR2(255 CHAR) NOT NULL,
 label VARCHAR(255 CHAR) NOT NULL,
 is_active VARCHAR(1 CHAR) NOT NULL,
 CONSTRAINT PK_MODULE PRIMARY KEY (id)
)

这是我的实体类:

@Entity
@Table(name="module")
public class Module {
private Long id;
private String name;
private String label;
private Boolean isActive;

@Id
@Column(name = "id", updatable = false)
public Long getId() {
    return id;
}

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

@Column(name = "name")
public String getName() {
    return name;
}

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

@Column(name = "label")
public String getLabel() {
    return label;
}

public void setLabel(String label) {
    this.label = label;
}

@Column(name = "is_active")
public Boolean getActive() {
    return isActive;
}

public void setActive(Boolean active) {
    isActive = active;
}
}

这是我的 JPA 存储库界面:

public interface ModuleDAO extends JpaRepository<Module, Long> {
     List<Module> findModulesByNameAndIdNot(String name, Long id);
     List<Module> findModulesByNameAndIsActive(String name, Boolean isActive);
     List<Module> findModulesByIsActive(Boolean isActive);
 }

但是当我尝试部署我的应用程序时,出现以下异常:

java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.sbt.azimuth_forms.dao.ModuleDAO.findModulesByNameAndIsActive(java.lang.String,java.lang.Boolean)! Unable to locate Attribute  with the the given name [isActive] on this ManagedType [com.sbt.azimuth_forms.entities.Module]
            at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:103)
            at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:106)
            at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:211)
            at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:79)
            at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lookupQuery(RepositoryFactorySupport.java:574)
            at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(RepositoryFactorySupport.java:567)
            at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
            at java.util.Iterator.forEachRemaining(Iterator.java:116)
            at java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.java:1049)
            at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
            at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
            at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
            at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
            at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
            at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
            at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.mapMethodsToQuery(RepositoryFactorySupport.java:569)
            at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$new$0(RepositoryFactorySupport.java:559)
            at java.util.Optional.map(Optional.java:215)
            at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:559)
            at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:332)
            at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:297)
            at org.springframework.data.util.Lazy.getNullable(Lazy.java:212)
            at org.springframework.data.util.Lazy.get(Lazy.java:94)
            at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:300)
            at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:121)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695)
            ... 60 more
 Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name 
 [isActive] on this ManagedType [com.sbt.azimuth_forms.entities.Module]
            at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.checkNotNull(AbstractManagedType.java:147)
            at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:118)
            at org.hibernate.metamodel.model.domain.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:43)
            at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:633)
            at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:617)
            at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.getTypedPath(JpaQueryCreator.java:385)
            at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:308)
            at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:211)
            at org.springframework.data.jpa.repository.query.JpaQueryCreator.and(JpaQueryCreator.java:133)
            at org.springframework.data.jpa.repository.query.JpaQueryCreator.and(JpaQueryCreator.java:59)
            at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:122)
            at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:95)
            at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:81)
            at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:230)
            at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:363)
            at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:98)
            ... 86 more

我读过很多关于这个主题的不同帖子,但其中任何一个都不是我的情况。我怀疑问题可能出在将 varchar 字段 is_active 映射到类 Module 的 boolean 字段中,但据我所知 JPA 和 Hibernate 可以解决此类映射。如有任何建议和建议,我们将不胜感激!

最佳答案

你的方法应该是这样的:

interface ModuleDAO extends JpaRepository<Module, Long> {
     List<Module> findByNameAndIdNot(String name, Long id);
     List<Module> findByNameAndActive(String name, boolean active);
     List<Module> findByActive(boolean isActive);
}

一些评论:

  1. 接口(interface)默认是公开的
  2. 您不应使用 boolean 包装类型,因为这会允许空值

关于java.lang.IllegalArgumentException : Unable to locate Attribute with the the given name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59176221/

相关文章:

java - 共享首选项不起作用。始终显示相同的值

java - 在表中插入数组列表的各个值

hibernate - Hibernate在更新时可以忽略实体成员吗?

java - 如何从父序列值填充子复合键(Hibernate、JPA)?

java - Hibernate OneToOne 之间具有惰性行为的 PK

java - Eclipse 自动完成功能类似于 Netbeans

java - 获取 JLabel 中的图标位置

java - Spring MVC 中 content-type application/x-www-form-urlencoded 的请求参数顺序

java - Spring分层Fat Jar和Gradle插件layertools jarmode

java - REST 中通过另一个资源 ID 来命名获取资源的做法是什么