2017-02-26 62 views
0

我已经检查了stackoverflow中的所有相关问题,但仍无法解决问题。请帮我解决这个问题。包括以下所有必需的细节。请让我知道是否需要进一步的细节。第一次问这个问题,如果在我将编辑它的问题中出现任何错误,请帮助我。获取JpaSystemException:尝试通过使用spring启动将对象保存在mySql数据库中时,无法通过反射设置字段值[2]值

主要类

  package com.example; 

     import java.util.concurrent.TimeUnit; 

     import javax.sql.DataSource; 

     import org.apache.catalina.connector.Connector; 
     import org.apache.log4j.Logger; 
     import org.hibernate.SessionFactory; 
     import org.springframework.beans.factory.annotation.Autowired; 
     import org.springframework.boot.SpringApplication; 
     import org.springframework.boot.autoconfigure.SpringBootApplication; 
     import org.springframework.boot.builder.SpringApplicationBuilder; 
     import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; 
     import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer; 
     import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; 
     import org.springframework.boot.web.servlet.ErrorPage; 
     import org.springframework.boot.web.support.SpringBootServletInitializer; 
     import org.springframework.context.ApplicationContext; 
     import org.springframework.context.annotation.Bean; 
     import org.springframework.http.HttpStatus; 
     import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder; 

     import com.dto.Product; 

     @SpringBootApplication 

     public class Application extends SpringBootServletInitializer{ 

      @Override 
       protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
       return application.sources(Application.class); 
       } 

      private static final Logger LOGGER = Logger.getLogger(Application.class); 

      public static void main(String[] args) { 
      try{ 
      ApplicationContext ctx = SpringApplication.run(Application.class, args); 
      System.out.println(ctx); 
      LOGGER.info("Spring Boot Beans:"); 
      LOGGER.info("Spring boot is ready"); 
      }catch(Exception e){ 
       LOGGER.error(e); 
      } 
      } 

      @Bean 
      public EmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() { 
      TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); 

      factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { 
       @Override 
       public void customize(Connector connector) { 
       connector.setProperty("maxKeepAliveRequests", "1"); 
       connector.setProperty("connectionTimeout", "20000"); 
       connector.setProperty("keepAliveTimeout", "1"); 
       connector.setProperty("maxThreads", "250"); 
       connector.setURIEncoding("UTF-8"); 
       } 
      }); 
      // Set timeout for tomcat and custom error pages. 
      factory.setSessionTimeout(10, TimeUnit.MINUTES); 
      factory.setContextPath("/spring-boot"); 
      factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html")); 
      factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html")); 
      return factory; 
      } 

      @Autowired 
      DataSource dataSource; 

      @Bean 
      public SessionFactory sessionFactory(){ 

      return new LocalSessionFactoryBuilder(dataSource) 


        .addAnnotatedClass(Product.class) 
         .buildSessionFactory(); 


      } 
     } 

DAO类

package com.example; 

import java.math.BigDecimal; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.jboss.logging.Logger; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 
import org.springframework.transaction.annotation.Transactional; 

import com.dto.Product; 



@Repository 
@Transactional 
public class DAOClass { 

    private static Logger logger = Logger.getLogger(DAOClass.class); 

    @Autowired 
    SessionFactory factory ; 

    public void saveProduct(){ 
     Session session = factory.openSession(); 
     Transaction transaction = null; 
     transaction = session.beginTransaction(); 
     Product product = new Product("TestProduct",new BigDecimal(111)); 
     session.save(product); 
     transaction.commit(); 



    } 


} 

产品实体类

package com.dto; 

import java.io.Serializable; 
import java.math.BigDecimal; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 

import org.hibernate.annotations.GenericGenerator; 
@Entity 
@Table(name="PRODUCT") 
public class Product implements Serializable{ 

    private static final long serialVersionUID = -1231235L; 

    @Id 
    @GenericGenerator(name="marksIdGenerator" , strategy="increment") 
    @GeneratedValue(generator="marksIdGenerator") 
    @Column(name = "PRODUCT_ID") 
    private Integer productId; 

    @Column(name = "PRODUCT_NAME") 
    private String productName; 

    @Column(name = "PRICE") 
    private BigDecimal price; 

    public Product(){} 

/* (non-Javadoc) 
* @see java.lang.Object#toString() 
*/ 
@Override 
public String toString() { 
    return "Product [productId=" + productId + ", productName=" + productName + ", price=" + price + "]"; 
} 

public Product(Integer productId, String productName, BigDecimal price) { 
    super(); 
    this.productId = productId; 
    this.productName = productName; 
    this.price = price; 
} 

public Product(String productName, BigDecimal price) { 
    super(); 
    this.productName = productName; 
    this.price = price; 
} 
/** 
* @return the productId 
*/ 
public int getProductId() { 
    return productId; 
} 
/** 
* @param productId the productId to set 
*/ 


public void setProductId(Integer productId) { 
    this.productId = productId; 
} 
/** 
* @return the productName 
*/ 


public String getProductName() { 
    return productName; 
} 
/** 
* @param productName the productName to set 
*/ 
public void setProductName(String productName) { 
    this.productName = productName; 
} 
/** 
* @return the price 
*/ 


public BigDecimal getPrice() { 
    return price; 
} 
/** 
* @param price the price to set 
*/ 
public void setPrice(BigDecimal price) { 
    this.price = price; 
}; 

} 

application.properties文件

spring.datasource.url=jdbc:mysql://localhost:3306/MyDb 
spring.datasource.username=root 
spring.datasource.password=677234 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.datasource.tomcat.max-active=5 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 
spring.jpa.hibernate.ddl-auto =update 
spring.jpa.show-sql = true 
server.port=8081 

的build.gradle文件

buildscript { 
    repositories { 
     //jcenter() 
       mavenLocal() 
     mavenCentral() 
     maven { url "http://repo.spring.io/release" } 
     maven { url "http://repo.spring.io/milestone" } 
     maven { url "http://repo.spring.io/snapshot" } 
    } 
    dependencies { 
     classpath('org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE') 
    } 
} 

apply plugin: 'java' 
apply plugin: 'eclipse' 
apply plugin: 'idea' 
apply plugin: 'org.springframework.boot' 
apply plugin: 'war' 

repositories { 
     mavenLocal() 
     mavenCentral() 
     maven { url "http://repo.spring.io/release" } 
     maven { url "http://repo.spring.io/milestone" } 
     maven { url "http://repo.spring.io/snapshot" } 
     maven { url "https://repository.jboss.org/nexus/content/repositories/releases" } 
} 

sourceCompatibility = 1.8 
targetCompatibility = 1.8 

dependencies { 
compile("org.springframework.boot:spring-boot-starter") 
    compile("org.springframework.boot:spring-boot-starter-web") 
    compile("org.springframework.boot:spring-boot-starter-jdbc") 
    compile("org.springframework.boot:spring-boot-starter-jersey") 
    compile("org.apache.tomcat.embed:tomcat-embed-jasper") 
    compile("mysql:mysql-connector-java") 
    compile("javax.inject:javax.inject") 
    compile("org.springframework.boot:spring-boot") 
    testCompile("org.springframework.boot:spring-boot-starter-test") 
compile("org.springframework.boot:spring-boot-devtools") 
compile("org.springframework.boot:spring-boot-starter-data-jpa") 



} 

错误跟踪(它包括球衣和servlet相关的错误,因为我通过REST调用DAO打这个可以忽略不计)

2017-02-26 08:30:27.158 ERROR 1076 --- [nio-8081-exec-1] o.s.boot.web.support.ErrorPageFilter  : Forwarding to error page from request [/services/health] due to exception [org.springframework.orm.jpa.JpaSystemException: Could not set field value [2] value by reflection : [class com.dto.Product.productId] setter of com.dto.Product.productId; nested exception is org.hibernate.PropertyAccessException: Could not set field value [2] value by reflection : [class com.dto.Product.productId] setter of com.dto.Product.productId] 

javax.servlet.ServletException: org.springframework.orm.jpa.JpaSystemException: Could not set field value [2] value by reflection : [class com.dto.Product.productId] setter of com.dto.Product.productId; nested exception is org.hibernate.PropertyAccessException: Could not set field value [2] value by reflection : [class com.dto.Product.productId] setter of com.dto.Product.productId 
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:489) ~[jersey-container-servlet-core-2.25.1.jar:na] 
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427) ~[jersey-container-servlet-core-2.25.1.jar:na] 
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388) ~[jersey-container-servlet-core-2.25.1.jar:na] 
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341) ~[jersey-container-servlet-core-2.25.1.jar:na] 
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228) ~[jersey-container-servlet-core-2.25.1.jar:na] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:115) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:59) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:90) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:108) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_45] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_45] 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45] 
Caused by: org.springframework.orm.jpa.JpaSystemException: Could not set field value [2] value by reflection : [class com.dto.Product.productId] setter of com.dto.Product.productId; nested exception is org.hibernate.PropertyAccessException: Could not set field value [2] value by reflection : [class com.dto.Product.productId] setter of com.dto.Product.productId 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:241) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at com.example.DAOClass$$EnhancerBySpringCGLIB$$960298ca.saveProduct(<generated>) ~[bin/:na] 
    at com.example.HealthController.health(HealthController.java:21) ~[bin/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45] 
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45] 
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81) ~[jersey-server-2.25.1.jar:na] 
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144) ~[jersey-server-2.25.1.jar:na] 
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161) ~[jersey-server-2.25.1.jar:na] 
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205) ~[jersey-server-2.25.1.jar:na] 
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99) ~[jersey-server-2.25.1.jar:na] 
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389) ~[jersey-server-2.25.1.jar:na] 
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347) ~[jersey-server-2.25.1.jar:na] 
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102) ~[jersey-server-2.25.1.jar:na] 
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326) ~[jersey-server-2.25.1.jar:na] 
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) ~[jersey-common-2.25.1.jar:na] 
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) ~[jersey-common-2.25.1.jar:na] 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315) ~[jersey-common-2.25.1.jar:na] 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297) ~[jersey-common-2.25.1.jar:na] 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267) ~[jersey-common-2.25.1.jar:na] 
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) ~[jersey-common-2.25.1.jar:na] 
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305) ~[jersey-server-2.25.1.jar:na] 
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154) ~[jersey-server-2.25.1.jar:na] 
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473) ~[jersey-container-servlet-core-2.25.1.jar:na] 
    ... 48 common frames omitted 
Caused by: org.hibernate.PropertyAccessException: Could not set field value [2] value by reflection : [class com.dto.Product.productId] setter of com.dto.Product.productId 
    at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:58) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.setIdentifier(AbstractEntityTuplizer.java:260) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.persister.entity.AbstractEntityPersister.setIdentifier(AbstractEntityPersister.java:4617) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:168) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at com.example.DAOClass.saveProduct(DAOClass.java:31) ~[bin/:na] 
    at com.example.DAOClass$$FastClassBySpringCGLIB$$f5c2d5f7.invoke(<generated>) ~[bin/:na] 
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    ... 78 common frames omitted 
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Integer field com.dto.Product.productId to com.dto.Product 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:1.8.0_45] 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[na:1.8.0_45] 
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58) ~[na:1.8.0_45] 
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75) ~[na:1.8.0_45] 
    at java.lang.reflect.Field.set(Field.java:764) ~[na:1.8.0_45] 
    at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:38) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    ... 96 common frames omitted 

上述错误指出org.hibernate作为.PropertyAccessException:无法通过反射设置字段值[2]值:[class com.dto.Product.productId]

不知道确切原因。

但我能够通过使用HQL获取产品表的所有细节,并且能够通过使用session.get(Product.class,productId)获得特定的产品详细信息。

虽然坚持我面临的问题的数据。一切工作正常,如果我使用Hibernate的XML映射。

面向onetomany映射的相同问题。

添加产品表描述图像

product table description

表:

创建表PRODUCT( PRODUCT_ID INT不为空, PRODUCT_NAME VARCHAR(50), PRICE DECIMAL(5,2) , 主键(PRODUCE_ID) );

我经历了以下网站:

https://hellokoding.com/jpa-one-to-many-relationship-mapping-example-with-spring-boot-hsql/

我想会话工厂对象,并应通过它来执行所有操作。

+0

它可能与该字段和getter/setter的int和Integer混合有关。你可以尝试将它们全部设置为原始的吗? –

+0

试过,还是一样的例外。 – shashikumar

+0

您可以尝试简化您的ID字段生成: @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer productId; GenerationTYpe.Sequence也是另一种选择。不知道什么是自动/默认为mysql –

回答

1

异常的原因是

Can not set java.lang.Integer field com.dto.Product.productId to com.dto.Product 

休眠试图设置字段Product.productId的值com.dto.Product,这是不是一个整数别的东西。

这很可能是由'marksIdGenerator'造成的。看看它是否真的产生Integer。

+0

将其更改为@GeneratedValue(strategy = GenerationType.IDENTITY)并尝试bist仍然存在相同的错误仍然存​​在 – shashikumar

相关问题