0

I have an application on Spring using JPA and Oracle DB. My table has following structure:

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

Here is my entity class:

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

Here is my JPA repository interface:

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

But when I try to deploy my application I have following exception:

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

I have read many different posts about this topic, but any of them is not my case. I suspect that problem can be in mapping varchar field is_active to boolean field of class Module, but as far as I know JPA and Hibernate can resolve such kind of mapping. Will be greateful for any suggestions and advices!

1
  • The property is named active not isActive the latter is the name of the field. Commented Dec 4, 2019 at 12:39

2 Answers 2

1

Your methods should look like:

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

Some remarks:

  1. Interfaces are public by default
  2. You shouldn't use Boolean wrapper type because this would allow null values
Sign up to request clarification or add additional context in comments.

Comments

0

try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.