gpt4 book ai didi

java - 持久实体中的 InstantiationException

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:51 25 4
gpt4 key购买 nike

从 hibernate 中获取对象时出现问题。堆栈跟踪是:

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();
}
}

然后我在 spring 文件中引用映射文件:

<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

所以,我不知道为什么返回错误的原因。我需要帮助

谢谢!

最佳答案

也许你的类中没有默认的公共(public)构造函数,或者(我不使用 .hbm 文件而不是注释)User.hbm.xml 类名中的类名应该是完全限定的?

请发布您的用户类代码。

关于java - 持久实体中的 InstantiationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6056680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com