我的spring-hibernate应用程序出错

时间:2012-11-06 21:03:39

标签: spring hibernate

我是Spring的新手,我正在尝试使用spring和带有注释的Hibernate创建一个简单的项目(不使用配置和映射XML)。我对3天以来的错误感到不满。

这是我看到的错误..

`ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/Spring]] (MSC service thread 1-2) StandardWrapper.Throwable: java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;
at java.lang.Class.getDeclaredFields0(Native Method) [rt.jar:1.6.0_25]
at java.lang.Class.privateGetDeclaredFields(Class.java:2291) [rt.jar:1.6.0_25]
at java.lang.Class.getDeclaredFields(Class.java:1743) [rt.jar:1.6.0_25]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:373) [org.springframework.orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:321) [org.springframework.orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:830) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:493) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:591) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) [org.springframework.context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469) [org.springframework.context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at javax.servlet.GenericServlet.init(GenericServlet.java:242) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1202) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3655) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3873) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_25]
at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_25]
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider from [Module "deployment.Spring.war:main" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:423)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120)
... 32 more

16:42:59,393 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/Spring]] (MSC service thread 1-2) Servlet /Spring threw load() exception: java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider from [Module "deployment.Spring.war:main" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:423)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120)
at java.lang.Class.getDeclaredFields0(Native Method) [rt.jar:1.6.0_25]
at java.lang.Class.privateGetDeclaredFields(Class.java:2291) [rt.jar:1.6.0_25]
at java.lang.Class.getDeclaredFields(Class.java:1743) [rt.jar:1.6.0_25]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:373) [org.springframework.orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:321) [org.springframework.orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:830) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:493) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:591) [org.springframework.beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) [org.springframework.context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469) [org.springframework.context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) [org.springframework.web.servlet-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at javax.servlet.GenericServlet.init(GenericServlet.java:242) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1202) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3655) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3873) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_25]
at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_25]`

以下是代码

[ Spring-servlet.xml]
<?xml version="1.0" encoding="UTF-8"?>
<beans ...

<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.spring.form.Items</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<context:component-scan base-package="com.spring" />

<tx:annotation-driven />


</beans>

Controller


import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.spring.form.Items;
import com.spring.service.ReceiptsService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/receiptsnew")
public class ReceiptsSpringController {

@Autowired
private ReceiptsService receiptsService;

public ReceiptsSpringController()
{

}

@RequestMapping(value="/save", method=RequestMethod.POST)
public ModelAndView saveItems(@ModelAttribute("item")Items item, BindingResult result)
{
    receiptsService.addItem(item);

    return new ModelAndView("redirect:/receiptsnew.html");

}

@RequestMapping(method=RequestMethod.GET)
public ModelAndView viewItems()
{
    Map model= new HashMap();
    model.put("items", receiptsService.getAllItems());
    //items is just a key values

    return new ModelAndView("itemsView",model);
}

@RequestMapping(value="/add", method=RequestMethod.GET)
public ModelAndView addItems(@ModelAttribute("item")Items item, BindingResult result) 
{
    return new ModelAndView("itemAdd"); 
}
}

DAO



import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.hibernate.SessionFactory;
import com.spring.form.Items;
@Repository("itemDao")
public class HibernateItemDao implements ItemDao {

@Autowired
private SessionFactory sessionFactory;


public void saveItem(Items item) {
    // TODO Auto-generated method stub
    sessionFactory.getCurrentSession().saveOrUpdate(item);
}

@SuppressWarnings("unchecked")
public List<Items> getAllItems() {
    // TODO Auto-generated method stub
    return sessionFactory.getCurrentSession().createCriteria(Items.class).list();
}

}




Service layer  


import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.spring.dao.ItemDao;
import com.spring.form.Items;



@Service("receiptsService")
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class ReceiptsServiceImpl implements ReceiptsService {

@Autowired
private ItemDao itemDao;

public ReceiptsServiceImpl()
{
}


@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
public void addItem(Items item) {
    // TODO Auto-generated method stub
    itemDao.saveItem(item);
}

public List<Items> getAllItems() {
    // TODO Auto-generated method stub
    return itemDao.getAllItems();
}

}

Form data


@Entity
@Table(name="INVP200B") 
public class Items {

@Id
@Column(name="ITEMB")
private String itemno;

@Column(name="QUARTB")
private String quantity;

@Column(name="QTYPLB")
private String pallet;

public Items()
{

}

public String getItemno() {
    return itemno;
}

public void setItemno(String itemno) {
    this.itemno = itemno;
}

public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public String getPallet() {
    return pallet;
}

public void setPallet(String pallet) {
    this.pallet = pallet;
}

public String toString()
{
    return " ITEM NO IS "+itemno+" QUANTITY IS"+quantity+" PALLET QUANTITY IS "+pallet ;
}
}

properties
database.driver=oracle.jdbc.driver.OracleDriver
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.connection.pool_size=10
database.url=jdbc\:oracle\:thin\:@app11.corebusiness.com\:2521\:oracle1
database.username=Alleg
database.password=Alleg

以下是罐子清单:http://i.stack.imgur.com/18rKU.png

我曾经得到“error-creating-bean-sessionfactory -nested exception”,直到我在spring-servlet代码中改变了我的bean声明的顺序。

4 个答案:

答案 0 :(得分:3)

作为以下异常的解决方案

  

引起:java.lang.NullPointerException at   org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.j AVA:207)

请为sessionFactory定义以下条目

<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>

答案 1 :(得分:1)

你正在混合使用hibernate3和hibernate4类:

<bean id="sessionFactory" 
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
                                              ^-- 4
...

<bean id="transactionManager" 
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                                              ^-- 3

如果你正在使用Hibernate 4,请确保只使用hibernate4包中的类,或者如果你使用的是Hibernate 3,则只使用hibernate3包中的类。

此外,似乎你在JBoss中运行它。那为什么不使用它的JTA事务管理器呢?

答案 2 :(得分:1)

上面的例外可以通过添加这个Hibernate属性来解决:

<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>

答案 3 :(得分:0)

当你使用注释时,你必须使用AnnotationSessionFactoryBean类

更改此

  <bean id="sessionFactory"   
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

     <bean id="sessionFactory" 
class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean">