Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

java - 无法实例化[org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]:构造方法抛出异常;(Failed to instantiate [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception;)

Spring with JPA Example (Spring with JPA示例)

I was trying to config a Spring Data JPA Project.But i get error plz provide solution.

(我试图配置一个Spring Data JPA Project。但是我遇到了错误,请提供解决方案。)

the error is Failed to instantiate [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception.

(错误无法实例化[org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]:构造函数引发了异常。)
Thank you.

(谢谢。)

1.Account.java

(1.Account.java)

package com.rks.beans;

public class Account 
{
        private int accountNumber;  
        private String owner;  
        private double balance; 

        public Account()
        {

        }
        public Account(int accountNumber,String owner,double balance)
        {
            this.accountNumber=accountNumber;
            this.owner=owner;
            this.balance=balance;
        }
        public int getAccountNumber() {
            return accountNumber;
        }
        public void setAccountNumber(int accountNumber) {
            this.accountNumber = accountNumber;
        }
        public String getOwner() {
            return owner;
        }
        public void setOwner(String owner) {
            this.owner = owner;
        }
        public double getBalance() {
            return balance;
        }
        public void setBalance(double balance) {
            this.balance = balance;
        }


}

2.Account.xml

(2.Account.xml)

<?xml version="1.0" encoding="UTF-8"?>  
<persistence xmlns="http://java.sun.com/xml/ns/persistence"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence  
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">  

    <persistence-unit name="ForAccountsDB">  
        <mapping-file>com/rks/beans/Account.xml</mapping-file>  
        <class>com.rks.beans.Account</class>  
    </persistence-unit>  
</persistence> 

3.AccountsDao.java

(3.AccountsDao.java)

package com.rks.beans;

import java.util.List;

import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.transaction.annotation.Transactional;

@Transactional  
public class AccountsDao
{  
    JpaTemplate template;  

    public void setTemplate(JpaTemplate template) {  
        this.template = template;  
    }  
    public void createAccount(int accountNumber,String owner,double balance){  
        Account account = new Account(accountNumber,owner,balance);  
        template.persist(account);  
    }  
    public void updateBalance(int accountNumber,double newBalance){  
        Account account = template.find(Account.class, accountNumber);  
        if(account != null){  
            account.setBalance(newBalance);  
        }  
        template.merge(account);  
    }  
    public void deleteAccount(int accountNumber){  
        Account account = template.find(Account.class, accountNumber);  
        if(account != null)  
            template.remove(account);  
    }  
    public List<Account> getAllAccounts(){  
        List<Account> accounts =template.find("select acc from Account acc");  
        return accounts;  
    }  
} 

4.persistence.xml

(4.persistence.xml)

<?xml version="1.0" encoding="UTF-8"?>  
<persistence xmlns="http://java.sun.com/xml/ns/persistence"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence  
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">  

    <persistence-unit name="ForAccountsDB">  
        <mapping-file>com/rks/beans/Account.xml</mapping-file>  
        <class>com.rks.beans.Account</class>  
    </persistence-unit>  
</persistence>  

5.spring.xml

(5.spring.xml)

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:tx="http://www.springframework.org/schema/tx"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  

     <tx:annotation-driven transaction-manager="jpaTxnManagerBean" proxy-target-class="true"/>  

     <bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>  
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:system"></property>  
        <property name="username" value="hr"></property>  
        <property name="password" value="hr"></property>  
    </bean>  

      <bean id="hbAdapterBean" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
        <property name="showSql" value="true"></property>  
        <property name="generateDdl" value="true"></property>  
        <property name="databasePlatform" value="org.hibernate.dialect.OracleDialect"></property>  
     </bean>  

    <bean id="emfBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
        <property name="dataSource" ref="dataSourceBean"></property>  
        <property name="jpaVendorAdapter" ref="hbAdapterBean"></property>  
     </bean>  

     <bean id="jpaTemplateBean" class="org.springframework.orm.jpa.JpaTemplate">  
        <property name="entityManagerFactory" ref="emfBean"></property>  
     </bean>  

     <bean id="accountsDaoBean" class="com.rks.beans.AccountsDao">  
        <property name="template" ref="jpaTemplateBean"></property>  
     </bean>  
      <bean id="jpaTxnManagerBean" class="org.springframework.orm.jpa.JpaTransactionManager">  
        <property name="entityManagerFactory" ref="emfBean"></property>  
    </bean>  

</beans> 

6.Accountsclient .java

(6.Accountsclient.java)

package com.rks.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Accountsclient {

    public static void main(String[] args) 
    {
         ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");  
         AccountsDao accountsDao = context.getBean("accountsDaoBean",AccountsDao.class);  

         accountsDao.createAccount(15, "Jai Kumar", 41000);  
         accountsDao.createAccount(20, "Rishi ", 35000);  
         System.out.println("Accounts created");  

    }

}

Error:

(错误:)

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hbAdapterBean' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1320)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    at com.rks.beans.Accountsclient.main(Accountsclient.java:10)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:213)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1312)
    ... 13 more
Caused by: java.lang.NoClassDefFoundError: org/hibernate/ejb/HibernatePersistence
    at org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter.<init>(HibernateJpaVendorAdapter.java:54)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:200)
    ... 15 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 21 more

Image of Hierachy

(希拉基的形象)

在此处输入图片说明


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...