Hibernate始终获取java.lang.IllegalArgumentException:无法设置java.lang.Long字段

时间:2018-12-04 21:49:34

标签: java spring hibernate

我在休眠时遇到了麻烦,不知道如何解决。

所有可能分类的文件;

Bean:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@NamedQuery(name="Book.getBookList", query="from Book")
public class Book {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private String author;

    public Book(String name, String author) {
        super();
        this.name = name;
        this.author = author;
    }
}

控制器:

@Controller
public class BookController {

    @Autowired
    private BookRepository bookRepo;
    private DAO dao = new DAO();

    @GetMapping("/")
    public String showpage(Model model, @RequestParam(defaultValue="0") int page) {
        model.addAttribute("data", bookRepo.findAll(PageRequest.of(page,4)));
        model.addAttribute("currentPage",page);
        return "index";
    }

    @PostMapping("/save")
    public String save(Book book) {
        System.out.println(book);
        bookRepo.save(book);
        dao.saveBook(book);
        return "redirect:/";
    }

    @GetMapping("/delete")
    public String delete(Long id) {
        bookRepo.deleteById(id);
        dao.deleteBook(id);
        return "redirect:/";
    }

    @GetMapping("/findOne")
    @ResponseBody
    public Optional<Book> findone(Long id) {
        return bookRepo.findById(id);
    }
}

道:

public class DAO {

    SessionFactory sessionFactory = new Configuration()
            .configure("ca/sheridancollege/config/hibernate.cfg.xml")
            .buildSessionFactory();

    public DAO() {
        super();
    }

    public void saveBook(Book book) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        session.saveOrUpdate(book);

        session.getTransaction().commit();
        session.close();
    }

    public List<Book> getBookList() {
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Query query = session.createNamedQuery("Book.getBookList"); 
        List<Book> bookList = query.getResultList();

        session.getTransaction().commit();
        session.close();

        return bookList;
    }

    public void deleteBook(Long id) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Book toDelete = session.get(Book.class, id);
        session.delete(toDelete);

        session.getTransaction().commit();
        session.close();
    }
}

每当我调用/ save时,都会出现错误。

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Dec 04 16:34:24 EST 2018
There was an unexpected error (type=Internal Server Error, status=500).
Error accessing field [private java.lang.Long ca.sheridancollege.beans.Book.id] by reflection for persistent property [ca.sheridancollege.beans.Book#id] : Book(id=11, name=, author=)
org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Long ca.sheridancollege.beans.Book.id] by reflection for persistent property [ca.sheridancollege.beans.Book#id] : Book(id=11, name=, author=)
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:75)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:224)
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4931)
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4631)
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226)
    at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:540)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:83)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:678)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:670)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:665)
    at ca.sheridancollege.dao.DAO.saveBook(DAO.java:28)
    at ca.sheridancollege.controllers.BookController.save(BookController.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field ca.sheridancollege.beans.Book.id to ca.sheridancollege.beans.Book
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:393)
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:71)
    ... 66 more

我做错了什么?预先感谢。

3 个答案:

答案 0 :(得分:2)

下面的代码示例工作正常!我为此示例使用了spring-boot version 1.5.10

AppConfig

@Configuration
@ComponentScan(basePackages = "com.test")
public class AppConfig {

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @Bean
    public SessionFactory getSessionFactory() {
        if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
            throw new NullPointerException("factory is not a hibernate factory");
        }
        return entityManagerFactory.unwrap(SessionFactory.class);
    }

application.properties

spring.datasource.url=jdbc:mysql://test/testAPI?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=false

BookController

@RestController
@RequestMapping("/test")
public class BookController {

    @Autowired
    private DAO bookRepo;

    @PostMapping("/save")
    public String save(@RequestBody Book book) {
        System.out.println(book);
        bookRepo.saveBook(book);
        return "redirect:/";
    }
}

图书

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@NamedQuery(name="Book.getBookList", query="from Book")
public class Book {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private String author;

    public Book(String name, String author) {
        super();
        this.name = name;
        this.author = author;
    }
}

DAO

@Repository
public class DAO {

    @Autowired
    private SessionFactory sessionFactory;


    public void saveBook(Book book) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.saveOrUpdate(book);
        session.getTransaction().commit();
        session.close();
    }
}

SecurityConfig

@EnableWebSecurity
@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {
    public SecurityConfig() {

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors().and()
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .and()
                .exceptionHandling()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
        return source;
    }
}

然后致电localhost:8080/test/save 正文:

 {
    "name":"test",
    "author":"test1"
}

响应:

redirect:/

答案 1 :(得分:0)

您的Book Bean具有私有字段,但没有getter和setter。

添加:

public Long getId()
{
    return id;
}

public void setId(Long id)
{
    this.id = id;
}

答案 2 :(得分:0)

您已经指出 ID 列是自动递增的,但自动递增索引仅存储在数据库端。 因此,您必须在 DB 端的表配置(如果使用工作台)中设置启用 ID 列自动增量。

如果您可以通过 API 端创建表定义,Hibernate 注释会在 db 端设置相同的内容。 (您可以通过设置属性值条目 spring.jpa.hibernate.ddl-auto=create 来做到这一点) 值 create、create-drop、validate 和 update 基本上影响模式工具管理在启动时操作数据库模式的方式。 有关相同的更多信息,请参阅 How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?

快乐编码!!