为什么JPA @Transient在使用Hibernate时不能使用数组属性

时间:2016-10-21 04:11:07

标签: java spring hibernate jpa transient

当我在数组属性上使用@Transient时,我的应用程序无法启动。我使用的是Spring 3.1 + Hibernate 4.2.3。

  

Maven配置

<dependency>
  <groupId>org.hibernate.javax.persistence</groupId>
  <artifactId>hibernate-jpa-2.0-api</artifactId>
  <version>1.0.1.Final</version>
</dependency>
  

实体类

    private Map<String,Integer>[] toyCountMapArr=null;

    @Transient
    public Map<String, Integer>[] getToyCountMapArr() {
      return toyCountMapArr;
    }

应用程序启动时出现错误消息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/applicationContext-hibernate.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No PropertyTypeExtractor available for type void 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:464)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
Caused by: java.lang.IllegalArgumentException: No PropertyTypeExtractor available for type void 
    at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.toXType(JavaReflectionManager.java:207)
    at org.hibernate.annotations.common.reflection.java.JavaXProperty.create(JavaXProperty.java:43)
    at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getXProperty(JavaReflectionManager.java:160)
    at org.hibernate.annotations.common.reflection.java.JavaXClass.getDeclaredMethodProperties(JavaXClass.java:114)
    at org.hibernate.annotations.common.reflection.java.JavaXClass.getDeclaredProperties(JavaXClass.java:129)
    at org.hibernate.annotations.common.reflection.java.JavaXClass.getDeclaredProperties(JavaXClass.java:121)
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:263)
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:665)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3512)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3466)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1756)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 56 more

以下所有声明都有效:

private Map<String,Integer> toyCountMap

private String[] toyCountMapArr=null

private HashMap<String,Integer>[] toyCountMapArr=null

这是因为Map<String, Integer>不是Serializable所以Map<String,Integer>[]无法序列化吗?

0 个答案:

没有答案