2016-01-12 58 views
-1

[在這裏輸入的形象描述] [1]春BeanCreationException:錯誤創建名爲 'accountDaoBean'

Account.java

package com.hibernateWithSpring; 
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; 
} 

} 

AccountClient.java

package com.hibernateWithSpring; 
import java.util.List; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.FileSystemXmlApplicationContext; 

public class AccountClient { 

public static void main(String[] args){ 

    ApplicationContext context=new    FileSystemXmlApplicationContext("bin/beans.xml"); 
    AccountDao accountdao = context.getBean("accountDaoBean",AccountDao.class); 
    accountdao.createAccount(110, "varun",1000); 
    accountdao.createAccount(111, "vicky",1200); 
    System.out.println("account created"); 
    accountdao.updateBalance(111,2222); 
    System.out.println("account updated"); 
    accountdao.deleteAccount(111); 
    System.out.println("account deleted"); 


    List<Account> account= accountdao.getAllAccount(); 
    for(int i=0;i<account.size();i++){ 
     Account acc=account.get(i); 
       System.out.println(acc.getAccountNumber()+":"+acc.getOwner()+":"+acc.getBalance()); 
    } 

} 
} 

AccountDao.java豆

package com.hibernateWithSpring; 
import java.util.List; 

import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 

public class AccountDao extends HibernateDaoSupport 
{ 

public void createAccount(int accountNumbeer, String owner, double balane){ 
    Account account= new Account(accountNumbeer,owner,balane); 
    getHibernateTemplate().save(account); 
} 

public void updateBalance(int accountNumber, double newBalance){ 
    Account account= getHibernateTemplate().get(Account.class, accountNumber); 
    if(account !=null){ 
     account.setBalance(newBalance); 
    } 
    getHibernateTemplate().update(account); 
} 
public void deleteAccount(int accountNumber){ 
    Account account=getHibernateTemplate().get(Account.class, accountNumber); 
      if(account!=null){ 
       getHibernateTemplate().delete(account); 
      } 
} 

@SuppressWarnings("unchecked") 
public List<Account> getAllAccount() 
{ 
    return getHibernateTemplate().find("from Account"); 
} 

} 

Account.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
<class name="com.hibernateWithSpring.Account" table="account"> 
    <id name="accountNumber" column="account_number" type="int"></id> 
    <property name="owner" column="owner" type="string"></property> 
    <property name="balance" column="balance" type="double"></property> 
</class> 
</hibernate-mapping> 

的beans.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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 


<bean id="accountDaoBean" class="com.hibernateWithSpring.AccountDao"> 
<property name="hibernateTemplate" ref="hibernateTemplateBean"></property> 
</bean> 

<bean id="hibernateTemplateBean" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
    <property name="sessionFactory" ref="sfBean"></property> 
    </bean> 

<bean id="sfBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
<property name="dataSource" ref="dataSourceBean"></property> 
<property name="mappingResources"> 
    <value>com/hibernateWithSpring/Account.hbm.xml</value> 
</property> 

<property name="hibernateProperties"> 
<props> 
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
<prop key="hibernate.show_sql">true</prop> 
<prop key="hibernate.hbm2ddl.auto">create</prop> 
</props> 
</property> 
</bean> 
<bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
<property name="url" value="jdbc:mysql://localhost:3306/accountdb"></property> 
<property name="username" value="root"></property> 
<property name="password" value="root"></property> 
</bean> 
</beans> 

我的數據庫信息是正確的,但我不知道爲什麼我收到此錯誤: -

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDaoBean' defined in file [E:\new project\marsWorkspase\SpringProgram\bin\beans.xml]: Cannot resolve reference to bean 'hibernateTemplateBean' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplateBean' defined in file [E:\new project\marsWorkspase\SpringProgram\bin\beans.xml]: Cannot resolve reference to bean 'sfBean' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sfBean' defined in file [E:\new project\marsWorkspase\SpringProgram\bin\beans.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/TransactionManager 

回答

1

事情你需要修復:

1 - 將JAR(javaee-api)放入您的LIB文件夾中

http://mvnrepository.com/artifact/javax/javaee-api/7.0

2 - 如果你正在使用Maven,添加以下的依賴

<dependency> 
    <groupId>javax</groupId> 
    <artifactId>javaee-api</artifactId> 
    <version>6.0</version> 
</dependency> 

3 - 配置你的神器,包括這個罐子,允許部署

4 - 將JavaEE的-api jar在你的tomcat/jboss lib文件夾中

+0

把javaee-api jar之後我得到錯誤: - 調用init方法失敗;嵌套的異常是java.lang.NoClassDefFoundError:org/hibernate/context/CurrentSessionContext –

+0

現在,對hibernate的依賴性做同樣的事情。 鏈接,罐子[鏈接](http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager) 依賴 \t org.hibernate作爲 \t 休眠-的EntityManager \t XXXl

+0

我在哪裏放這個標籤?在哪個文件中? @victor –

相關問題