【问题标题】:Mysql Spring Data JPA booleanMysql Spring Data JPA 布尔值
【发布时间】:2017-02-17 09:18:56
【问题描述】:

我的 Action 类中的布尔类型有问题。

我在 mysql 中的表有 'removed' 列,它是 BIT(1) 类型,默认值为 0。

@Entity
@Table(name = "Actions")
     public class Action {
      //other fields

      @Column(name = "removed")
      private boolean removed;

      //setters and getters
      }

当我尝试编译我的 Spring Boot 应用程序时,我看到如下错误:

          Caused by: org.springframework.data.mapping.PropertyReferenceException: No property by found for type boolean! Traversed path: Action.removed.
            at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:339) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:363) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:87) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:63) ~[spring-data-jpa-1.10.2.RELEASE.jar:na]
            at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103) ~[spring-data-jpa-1.10.2.RELEASE.jar:na]
            at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:214) ~[spring-data-jpa-1.10.2.RELEASE.jar:na]
            at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) ~[spring-data-jpa-1.10.2.RELEASE.jar:na]
            at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:435) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:220) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:266) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
            at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) ~[spring-data-jpa-1.10.2.RELEASE.jar:na]
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
            ... 44 common frames omitted

我尝试对我的操作删除字段进行其他注释,例如:

@Column(name = "removed",columnDefinition = "BIT", length = 1)
@Column(name = "removed",columnDefinition = "TINYINT", length = 1)

@Type(type = "org.hibernate.type.NumericBooleanType")

但是没有不起作用...

我的 ActionDao 界面:

 public interface ActionDao extends CrudRepository<Action, Integer>{
public Action findByActionname(String actionname);
public List<Action> findAllByRemovedByOrderByActionnameAsc(boolean removed);}

我的 pom.xml

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>

和 application.properties

        spring.datasource.url=jdbc:mysql://localhost/System?useUnicode=yes&characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=test
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
    spring.jpa.show-sql=true
    spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
    spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    spring.jpa.hibernate.ddl-auto=validate

有人可以帮助我吗?

【问题讨论】:

  • 你用的是什么版本的 MySQL?
  • 5.5.52-0ubuntu0.14.04.1
  • 当您尝试使用Boolean 而不是boolean 时会发生什么?
  • 如果您使用的是 5.5,那么映射应该可以工作。试试 Serge 的建议并使用Boolean
  • 同样的问题...它不工作...

标签: mysql hibernate spring-boot spring-data-jpa


【解决方案1】:

您在错误的位置搜索问题。问题不是删除,而是您的查询方法。

public List<Action> findAllByRemovedByOrderByActionnameAsc(boolean removed);

应该是:

public List<Action> findAllByRemovedOrderByActionnameAsc(boolean removed); 

如果我们回顾您的错误信息

'没有找到布尔类型的 by 属性!遍历路径:Action.removed。'

by 之后通常是属性的名称。

【讨论】:

    猜你喜欢
    • 2021-08-27
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 2017-11-29
    • 2017-04-01
    • 2020-06-16
    • 2020-01-27
    • 2019-05-17
    相关资源
    最近更新 更多