【问题标题】:SpringData JPA - Provided id of the wrong type for class . Expected: class java.lang.Integer, got class java.lang.LongSpring Data JPA - 为 class 提供了错误类型的 id。预期:类 java.lang.Integer,得到类 java.lang.Long
【发布时间】:2018-04-08 04:19:33
【问题描述】:

我在使用 Spring JPA 并尝试检索对象列表时遇到了这个问题。

这是我要检索的类

@Entity
@Table(name="OBJECTSTERMIC")
public class TermicObject {

    @Id
    @Column(name="TERMICID")
    private long termicId;

    @MapsId
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="OBJECTID",columnDefinition="INTEGER")
    private Object object;

    @Column(name="CONTECA_RIF")
    private int contecaRif;

    @Column(name="CONTECA_VAL")
    private int contecaVal;

    @Column(name="TYPE")
    private String type;

//getters and setters

Object 类在 MySQL 上的主键存储为 Integer,这确实是 Object

@Entity
public class Object {

    @Column(name="OBJECTID")
    @Id
    @JsonProperty("OBJECTID")
    private int objectId;
    ....

所以,没有地方设置 Long...

现在,我只需调用一个服务类

@Override
    public List<TermicObject> findAll() {

        return repository.findAll();
    }

得到了这个异常

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TypeMismatchException: Provided id of the wrong type for class it.besmart.db_eipo.persistence.model.Object. Expected: class java.lang.Integer, got class java.lang.Long; nested exception is java.lang.IllegalArgumentException: org.hibernate.TypeMismatchException: Provided id of the wrong type for class it.besmart.db_eipo.persistence.model.Object. Expected: class java.lang.Integer, got class java.lang.Long

Object Id应该是Long的设置在哪里?

【问题讨论】:

    标签: mysql spring-data-jpa


    【解决方案1】:

    查看存储库的定义。它有正确的泛型类型吗?你有整数作为第二个参数吗?恕我直言,这可能是根本原因。查看建议的正确版本:

    @RepositoryRestResource
    public interface TermicObjectRepository extends JpaRepository<TermicObject, Integer> {
        public Optional<TermicObject> findById(Integer id);
        public List<TermicObject> findAll()
    }
    

    【讨论】:

      【解决方案2】:

      根据@Lubo 的回答,在我的情况下,我遇到了 String 和 Long 类型之间的兼容性问题,并且由于我的模型需要 Long 自动生成的 id,我不得不更改存储库 from

      public interface ProductRepository extends JpaRepository<Product, String> {
      }
      

      public interface ProductRepository extends JpaRepository<Product, Long> {
      }
      

      我的控制器来自

      @RequestMapping(path = "/products/delete/{id}", method = RequestMethod.DELETE)
      public void deleteProduct(@PathVariable(name = "id") String id) {
          productRepository.deleteById(id);
      }
      

      @RequestMapping(path = "/products/delete/{id}", method = RequestMethod.DELETE)
      public void deleteProduct(@PathVariable(name = "id") Long id) {
          productRepository.deleteById(id);
      }
      

      【讨论】:

        【解决方案3】:

        因为

        public class MyEntity {
          @Id()
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          @Column(name = "id", nullable = false)
          private int id;  // <-------- int
        
          ...
        
          public long getId() { return id; } // <-------- long
        
        }
        

        【讨论】:

        • 我遇到了同样的异常,我没有注意到我已经为孩子的 id 字段声明了一个 long 类型。感谢您的提示。
        【解决方案4】:

        不完全确定,但我认为这个映射

        @Id
        @Column(name="TERMICID")
        private long termicId;
        
        @MapsId
        @OneToOne(fetch = FetchType.LAZY)
        @JoinColumn(name="OBJECTID",columnDefinition="INTEGER")
        private Object object;
        

        使Object 的id 与termicId 的值相匹配,这是一个long。

        【讨论】:

          【解决方案5】:

          您必须将您的 id 定义为 Long 数据类型。

          @Id
          @Column(name="TERMICID")
          private Long termicId;
          

          同时在您的存储库界面中进行更改:

          public interface ProductRepository extends JpaRepository<Product, Long> {
          }
          

          【讨论】:

            猜你喜欢
            • 2015-01-12
            • 1970-01-01
            • 2022-10-05
            • 1970-01-01
            • 2018-10-27
            • 1970-01-01
            • 2012-06-04
            • 2021-07-21
            • 2015-08-02
            相关资源
            最近更新 更多