姬長信(Redy)

Hibernate:保存时java.lang.ClassCastException


我今天遇到HIBERNATE一个奇怪的问题,当我尝试将对象保存到数据库时,它给了我java.lang.ClassCastException.

这是错误的堆栈跟踪:

15:57:24,307 DEBUG AnnotationTransactionAttributeSource:106 - Adding transactional method 'createNombreConnexion' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
15:57:24,307 DEBUG AnnotationTransactionAttributeSource:106 - Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
15:57:24,682 ERROR stderr:71 - java.lang.ClassCastException: java.lang.Long cannot be cast to ma.dataprotect.sensipro.model.NombreConnexion

15:57:24,687 ERROR stderr:71 -  at ma.dataprotect.sensipro.dao.impl.NombreConnexionDaoImpl.save(NombreConnexionDaoImpl.java:30)

15:57:24,687 ERROR stderr:71 -  at ma.dataprotect.sensipro.dao.impl.NombreConnexionDaoImpl.save(NombreConnexionDaoImpl.java:1)

15:57:24,687 ERROR stderr:71 -  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

15:57:24,687 ERROR stderr:71 -  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

15:57:24,692 ERROR stderr:71 -  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

15:57:24,692 ERROR stderr:71 -  at java.lang.reflect.Method.invoke(Method.java:498)

15:57:24,692 ERROR stderr:71 -  at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)

15:57:24,692 ERROR stderr:71 -  at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)

15:57:24,697 ERROR stderr:71 -  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)

15:57:24,697 ERROR stderr:71 -  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)

15:57:24,697 ERROR stderr:71 -  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

15:57:24,697 ERROR stderr:71 -  at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)

15:57:24,702 ERROR stderr:71 -  at com.sun.proxy.$Proxy407.save(Unknown Source)

15:57:24,702 ERROR stderr:71 -  at ma.dataprotect.sensipro.services.impl.NombreConnexionServiceImpl.createNombreConnexion(NombreConnexionServiceImpl.java:28)

15:57:24,702 ERROR stderr:71 -  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

15:57:24,702 ERROR stderr:71 -  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

15:57:24,707 ERROR stderr:71 -  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

15:57:24,707 ERROR stderr:71 -  at java.lang.reflect.Method.invoke(Method.java:498)

15:57:24,707 ERROR stderr:71 -  at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)

15:57:24,707 ERROR stderr:71 -  at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)

15:57:24,712 ERROR stderr:71 -  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)

15:57:24,712 ERROR stderr:71 -  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)

15:57:24,712 ERROR stderr:71 -  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

15:57:24,712 ERROR stderr:71 -  at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)

15:57:24,712 ERROR stderr:71 -  at com.sun.proxy.$Proxy410.createNombreConnexion(Unknown Source)

15:57:24,712 ERROR stderr:71 -  at ma.dataprotect.sensipro.util.LoginBean.login(LoginBean.java:85)

15:57:24,712 ERROR stderr:71 -  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

15:57:24,712 ERROR stderr:71 -  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

15:57:24,717 ERROR stderr:71 -  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

15:57:24,717 ERROR stderr:71 -  at java.lang.reflect.Method.invoke(Method.java:498)

15:57:24,717 ERROR stderr:71 -  at com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:181)

15:57:24,717 ERROR stderr:71 -  at com.sun.el.parser.AstValue.invoke(AstValue.java:289)

15:57:24,717 ERROR stderr:71 -  at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)

15:57:24,717 ERROR stderr:71 -  at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)

15:57:24,717 ERROR stderr:71 -  at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)

15:57:24,722 ERROR stderr:71 -  at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)

15:57:24,722 ERROR stderr:71 -  at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)

15:57:24,722 ERROR stderr:71 -  at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)

15:57:24,722 ERROR stderr:71 -  at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)

15:57:24,722 ERROR stderr:71 -  at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)

15:57:24,722 ERROR stderr:71 -  at javax.faces.component.UICommand.broadcast(UICommand.java:315)

15:57:24,722 ERROR stderr:71 -  at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)

15:57:24,722 ERROR stderr:71 -  at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)

15:57:24,722 ERROR stderr:71 -  at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)

15:57:24,727 ERROR stderr:71 -  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)

15:57:24,727 ERROR stderr:71 -  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)

15:57:24,727 ERROR stderr:71 -  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)

15:57:24,727 ERROR stderr:71 -  at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)

15:57:24,727 ERROR stderr:71 -  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)

15:57:24,727 ERROR stderr:71 -  at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:78)

15:57:24,727 ERROR stderr:71 -  at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)

15:57:24,727 ERROR stderr:71 -  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)

15:57:24,732 ERROR stderr:71 -  at ma.dataprotect.sensipro.util.FiltreUTF8.doFilter(FiltreUTF8.java:48)

15:57:24,732 ERROR stderr:71 -  at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)

15:57:24,732 ERROR stderr:71 -  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)

15:57:24,732 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)

15:57:24,732 ERROR stderr:71 -  at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)

15:57:24,732 ERROR stderr:71 -  at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)

15:57:24,732 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,737 ERROR stderr:71 -  at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)

15:57:24,737 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,737 ERROR stderr:71 -  at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)

15:57:24,737 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,737 ERROR stderr:71 -  at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)

15:57:24,737 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,737 ERROR stderr:71 -  at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:139)

15:57:24,737 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)

15:57:24,742 ERROR stderr:71 -  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,742 ERROR stderr:71 -  at org.springframework.security.web.access.channel.ChannelProcessingFilter.doFilter(ChannelProcessingFilter.java:144)

15:57:24,747 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

15:57:24,747 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)

15:57:24,747 ERROR stderr:71 -  at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)

15:57:24,747 ERROR stderr:71 -  at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)

15:57:24,747 ERROR stderr:71 -  at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)

15:57:24,747 ERROR stderr:71 -  at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)

15:57:24,747 ERROR stderr:71 -  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)

15:57:24,747 ERROR stderr:71 -  at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)

15:57:24,747 ERROR stderr:71 -  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

15:57:24,747 ERROR stderr:71 -  at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)

15:57:24,747 ERROR stderr:71 -  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)

15:57:24,747 ERROR stderr:71 -  at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)

15:57:24,747 ERROR stderr:71 -  at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)

15:57:24,747 ERROR stderr:71 -  at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)

15:57:24,747 ERROR stderr:71 -  at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)

15:57:24,747 ERROR stderr:71 -  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

15:57:24,747 ERROR stderr:71 -  at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)

15:57:24,752 ERROR stderr:71 -  at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)

15:57:24,752 ERROR stderr:71 -  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

15:57:24,752 ERROR stderr:71 -  at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)

15:57:24,752 ERROR stderr:71 -  at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)

15:57:24,752 ERROR stderr:71 -  at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)

15:57:24,752 ERROR stderr:71 -  at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)

15:57:24,752 ERROR stderr:71 -  at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)

15:57:24,752 ERROR stderr:71 -  at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)

15:57:24,752 ERROR stderr:71 -  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

15:57:24,752 ERROR stderr:71 -  at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)

15:57:24,752 ERROR stderr:71 -  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

15:57:24,752 ERROR stderr:71 -  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

15:57:24,752 ERROR stderr:71 -  at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)

15:57:24,752 ERROR stderr:71 -  at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)

15:57:24,752 ERROR stderr:71 -  at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)

15:57:24,752 ERROR stderr:71 -  at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)

15:57:24,752 ERROR stderr:71 -  at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)

15:57:24,757 ERROR stderr:71 -  at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)

15:57:24,757 ERROR stderr:71 -  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

15:57:24,757 ERROR stderr:71 -  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

15:57:24,757 ERROR stderr:71 -  at java.lang.Thread.run(Thread.java:745)

这是存在问题的实体:

package ma.dataprotect.sensipro.model;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@SuppressWarnings("serial")
@Entity
@Table(name = "nombre_connexion")
public class NombreConnexion implements java.io.Serializable{

    private Long id;
    private Organism organism;
    private Date dateConnexion;
    private Long nombreConn;

    public NombreConnexion() {
        super();
        // TODO Auto-generated constructor stub
    }

    public NombreConnexion(Organism organism, Date dateConnexion,
            Long nombreConnexion) {
        super();
        this.organism = organism;
        this.dateConnexion = dateConnexion;
        this.nombreConn= nombreConnexion;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Long getId() {
        return id;
    }

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

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "id_organism", nullable = false)
    public Organism getOrganism() {
        return organism;
    }

    public void setOrganism(Organism organism) {
        this.organism = organism;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "date_connexion", unique=true, nullable = false)
    public Date getDateConnexion() {
        return dateConnexion;
    }

    public void setDateConnexion(Date dateConnexion) {
        this.dateConnexion = dateConnexion;
    }

    @Column(name = "nombre_connexion", nullable = false)
    public Long getNombreConn() {
        return nombreConn;
    }

    public void setNombreConn(Long nombreConn) {
        this.nombreConn = nombreConn;
    }
}

通用保存方法:

@SuppressWarnings("unchecked")
    public T save(T t) {
        return (T) getSessionFactory().getCurrentSession().save(t);
    }

产生问题的代码:

nombreConnexion = new NombreConnexion();
                                        nombreConnexion.setOrganism(user.getOrganism());
nombreConnexion.setDateConnexion(new Date());
nombreConnexion.setNombreConn((long) 1);
System.out.println(nombreConnexion);
                                        nombreConnexionService.createNombreConnexion(nombreConnexion);

编辑:nombreConnexionServiceImpl.createNombreConnexion(nombreConnexion)的代码[没什么重要的!]

public void createNombreConnexion(NombreConnexion nombreConnexion) {

    nombreConnexionDao.save(nombreConnexion);

}

编辑2(我希望这会有所帮助):我将id的类型更改为Integer,现在错误是:

16:30:46,818 ERROR stderr:71 - java.lang.ClassCastException: java.lang.Integer cannot be cast to ma.dataprotect.sensipro.model.NombreConnexion

我们正在使用spring和hibernate,一切工作正常,我不知道可能是什么问题.