JPA CRUD

package com.example.demo.user;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
public class UserController {

    //创建日志
    private Logger logger = LoggerFactory.getLogger( UserController.class );

    @Autowired
    private UserJPA userJPA;

    //添加方法 或者更新  如果数据库有不更新 没有就更新 根据id去判断
    @RequestMapping(value = "add", method = RequestMethod.GET)
    public ResponseCode add(User user) {
        user.setName( "张三" );
        user.setAge( 66 );
        user.setAddress( "张家界" );
        user = userJPA.save( user );
        return ResponseCode.SUCCESS;
    }

    //删除
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public ResponseCode delete() {
        userJPA.deleteById( Long.valueOf( 1 ) );
        return ResponseCode.SUCCESS;
    }

    //查询
    @RequestMapping(value = "findById", method = RequestMethod.GET)
    public Optional <User> findById() {
        return userJPA.findById( Long.valueOf( 1 ) );
    }

}

使用JPA的查询方法报错

Provided id of the wrong type for class com.example.demo.user.User. Expected: class java.lang.Integer, got class java.lang.Long
原因如下 实体类的字段为int字段 需要实体类主键字段改成Long类型

使用getOne()报错

原因 getOne为懒加载模式 所以会出现这种问题 解决办法 @Bean 关掉懒加载

2018-08-01 18:09:26.689 ERROR 9032 --- [apr-8080-exec-9] o.s.b.w.servlet.support.ErrorPageFilter  : Forwarding to error page from request [/user/query] due to exception [Type definition error: [simple type, class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.kric.ball.entity.UserInfo_$$_jvst716_0["handler"])]

org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.kric.ball.entity.UserInfo_$$_jvst716_0["handler"])
解决方式:

    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper().disable( SerializationFeature.FAIL_ON_EMPTY_BEANS);
    }
    原文作者:VirtuaLenv
    原文地址: https://www.jianshu.com/p/8c2d54b88b0c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞