小编典典

SpringMVC-休眠:java.base / [Ljava.lang.Object; 无法投射到

hibernate

Group.java

package com.keating.model;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "t_group")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Group implements Serializable {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid")
    @Column(name = "uuid", unique = true)
    private String uuid;

    @Column(name = "name", nullable = false, unique = true)
    private String name;

    @Column(name = "description", nullable = false, unique = false)
    private String description;

    @ManyToOne
    @JoinColumn(name = "creatorUUID", nullable = false, unique = false)
    private User creator;

    @Column(name = "create_time", nullable = false)
    private Date time = new Date();

    public Group() {
    }

    public Group(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public Group(String name, String description, User creator) {
        this.name = name;
        this.description = description;
        this.creator = creator;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }
}

这是我的方法。

public Group getInfo(String groupUUID) {
    String hql = "SELECT group.uuid, group.name, group.description, group.time FROM Group group WHERE group.uuid=?";
    Query query = this.getCurrentSession().createQuery(hql);
    query.setParameter(0, groupUUID);

    return (Group) query.uniqueResult();
}

我认为还可以,但是有一些例外:

HTTP状态500 –内部服务器错误h1 {font-family:Tahoma,Arial,sans-serif; color:white;
background-color:#525D76; font-size:22px;} h2 {font-family:Tahoma,Arial,sans-
衬线;颜色:白色;背景颜色:#525D76;字体大小:16px;} h3 {font-family:Tahoma,Arial,sans-
serif;颜色:白色;背景颜色:#525D76;字体大小:14px ;}身体{font-family:Tahoma,Arial,sans-
serif;颜色:黑色;背景色:白色;} b {font-family:Tahoma,Arial,sans-serif;颜色:白色;背景色:#
525D76;} p {font-family:Tahoma,Arial,sans-serif; background:white;
color:black; font-size:12px;} a {color:black;} a.name {color:black;} .line

HTTP状态500 –内部服务器错误

类型 异常报告

消息 请求处理失败;嵌套的异常是java.lang.ClassCastException:java.base /
[Ljava.lang.Object; 无法转换为com.keating.model.Group

描述 服务器遇到意外情况,阻止其满足请求。

例外

 org.springframework.web.util.NestedServletException:请求处理失败;嵌套的异常是java.lang.ClassCastException:java.base / [Ljava.lang.Object; 无法转换为com.keating.model.Group
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

根本原因

 java.lang.ClassCastException:java.base / [Ljava.lang.Object; 无法转换为com.keating.model.Group
    com.keating.dao.impl.GroupDaoImpl.getInfo(GroupDaoImpl.java:26)
    com.keating.service.impl.GroupServiceImpl.getInfo(GroupServiceImpl.java:23)
    java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
    java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.base / java.lang.reflect.Method.invoke(Method.java:564)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    com.sun.proxy。$ Proxy37.getInfo(来源不明)
    com.keating.controller.GroupController.getInfo(GroupController.java:56)
    java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
    java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.base / java.lang.reflect.Method.invoke(Method.java:564)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

注意 在服务器日志中可以找到根本原因的完整堆栈跟踪。

Apache Tomcat / 9.0.2


阅读 257

收藏
2020-06-20

共1个答案

小编典典

删除您要查找的字段列表

String hql = "FROM Group group WHERE group.uuid=?";
2020-06-20