java.lang.IllegalArgumentException:无法找到具有给定名称的属性

时间:2019-12-04 12:35:57

标签: java spring jpa spring-data-jpa

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

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的布尔字段中,但是据我所知,JPA和Hibernate可以解决此类映射。如有任何建议,将不胜感激!

2 个答案:

答案 0 :(得分:1)

您的方法应类似于:

.csv

一些评论:

  1. 默认情况下接口是公开的
  2. 您不应使用布尔包装类型,因为这将允许空值

答案 1 :(得分:0)

尝试

@Column(name = "is_active")
public Boolean getIsActive() {
    return isActive;
}
public void setIsActive(Boolean isActive) {
    this.isActive = isActive;
 }
}