2011-05-19 110 views
2

從休眠狀態獲取對象時遇到問題。堆棧跟蹤是:持久化實體中的InstantiationException

org.hibernate.InstantiationException: Could not instantiate entity: main.java.com.gwt.app.server.User 
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:114) 
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:123) 
at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:603) 
at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:3911) 
at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1422) 
at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1411) 
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1446) 
at org.hibernate.loader.Loader.getRow(Loader.java:1355) 
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:611) 
at org.hibernate.loader.Loader.doQuery(Loader.java:829) 
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274) 
at org.hibernate.loader.Loader.loadEntity(Loader.java:2037) 
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86) 
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:76) 
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3293) 
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:496) 
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:477) 
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227) 
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285) 
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152) 
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090) 
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:1005) 
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:998) 
at main.java.com.gwt.app.server.User.loadUser(User.java:50) 
at main.java.com.gwt.app.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:22) 
at main.java.com.gwt.app.client.Main.main(Main.java:11) 
Caused by: java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:111) 
... 25 more 
Caused by: java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:900) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455) 
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93) 
at main.java.com.gwt.app.server.User.<init>(User.java:33) 
... 30 more 

我User.hbm.xml文件是:

<hibernate-mapping package="main.java.com.gwt.app.server"> 
<class name="User" table="USR"> 
    <id column="login" name="login" type="java.lang.String"/> 
    <property column="password" name="password" type="java.lang.String"/> 
</class> 
</hibernate-mapping> 

和用戶等級:

package main.java.com.gwt.app.server; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.core.io.ClassPathResource; 

public class User { 
private String login; 
private String password; 
private SessionFactory sesionFactory; 
private Session sesion; 

public void setSesionFactory(SessionFactory sesionFactory){ 
    this.sesionFactory = sesionFactory; 
    this.sesion = this.sesionFactory.openSession(); 
} 

public User(String user, String pass) { 
    ClassPathResource res = new ClassPathResource("resources/spring.xml"); 
    XmlBeanFactory factory = new XmlBeanFactory(res); 
    this.sesionFactory = (SessionFactory)factory.getBean("sessionFactory"); 
    this.sesion = this.sesionFactory.openSession(); 
    this.login = user; 
    this.password = pass; 
} 
public User() { 
} 

public String getLogin() { 
    return login; 
} 
public void setLogin(String login) { 
    this.login = login; 
} 
public String getPassword() { 
    return password; 
} 
public void setPassword(String password) { 
    this.password = password; 
} 

public String loadUser(String log){ 
    return ((User)sesion.get(User.class, log)).getLogin(); 
} 
} 

我在春天文件引用映射文件:

<property name="mappingResources"> 
     <list> 
      <value> 
       User.hbm.xml 
      </value> 
     </list> 
    </property> 

我使用的庫有:

hibernate3.jar 
org.springframework.aop-3.1.0.M1.jar 
org.springframework.asm-3.1.0.M1.jar 
org.springframework.aspects-3.1.0.M1.jar 
org.springframework.beans-3.1.0.M1.jar 
org.springframework.context-3.1.0.M1.jar 
org.springframework.context.support-3.1.0.M1.jar 
org.springframework.core-3.1.0.M1.jar 
org.springframework.expression-3.1.0.M1.jar 
org.springframework.instrument-3.1.0.M1.jar 
org.springframework.instrument.tomcat-3.1.0.M1.jar 
org.springframework.jdbc-3.1.0.M1.jar 
org.springframework.jms-3.1.0.M1.jar 
org.springframework.orm-3.1.0.M1.jar 
org.springframework.oxm-3.1.0.M1.jar 
org.springframework.transaction-3.1.0.M1.jar 
hibernate-validator-4.0.2.GA.jar 
validation-api-1.0.0.GA.jar 

所以,我不知道爲什麼是返回錯誤的原因。我需要幫助

謝謝!

+0

和庫? – 2011-05-19 09:41:47

+0

如果您查看異常堆棧跟蹤,您將看到一個由:NoClassDefFoundError引起的錯誤。這是應該解決的錯誤的原因。我懷疑Andreas_D是對的。 – SJuan76 2011-05-19 09:47:42

+0

您的圖書館如何管理?並沒有進攻,但爲什麼你使用M1發佈的框架,你不熟悉? – abalogh 2011-05-19 10:05:10

回答

0

也許你沒有默認的公共構造函數在你的類或者可能(我不使用.hbm文件而不是註釋)User.hbm.xml類名中的類名應該完全限定嗎?

請發佈您的用戶代碼。

+0

@Jose Hdez:哪一個是答案? – 2011-05-26 15:40:51

0

包名爲main.java.com.gwt.app.server?我很確定,包名真的應該是com.gwt.app.server,源目錄是./main/java

變化User包裝聲明

package com.gwt.app.sever; 

,並在您的User.hbm.xml文件中設置新的包名。很有可能它已經解決了這個問題。


這纔是真正的問題:

Caused by: java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException 

FatalBeanException類不是在類路徑中。它應該位於springframeworks庫中的一個庫中,因此您應該(1)檢查哪個庫包含此類,以及(2)仔細檢查此庫是否在運行時位於類路徑中。你錯過了

+0

該軟件包與main.java.com.gwt.app.server相同是正確的 – 2011-05-19 09:51:39

+0

對於軟件包名稱來說,這是一個非常不合乎標準的選擇。在最常見的構建設置(例如Maven)中,您的java代碼存儲在src/main/java目錄中,但您的包名不包含這些,因爲構建系統會假定src/main/java是源目錄。 – 2011-05-26 00:30:53

0

的一件事是你要有你的映射文件的完整路徑,包括目錄,爲你的包:

<property name="mappingResources"> 
    <list> 
     <value> 
      main/java/com/gwt/app/server/User.hbm.xml 
     </value> 
    </list> 
</property> 
相關問題