2013-01-17 128 views
0

要理解Spring中的真正問題是否真的需要關於結構的更好的知識。我在這個問題上討論了兩天,找不到任何解決方案。在我的項目,使用Hibernate的時候我建立它,給出以下錯誤消息:Spring Bean創建異常

2013-01-17 06:28:50,251 INFO [org.hibernate.cfg.SettingsFactory] - Database -> 
    name : MySQL 
version : 5.0.96-community-nt 
    major : 5 
    minor : 0 
2013-01-17 06:28:50,251 INFO [org.hibernate.cfg.SettingsFactory] - Driver -> 
    name : MySQL-AB JDBC Driver 
version : mysql-connector-java-5.1.12 (Revision: ${bzr.revision-id}) 
    major : 5 
    minor : 1 
2013-01-17 05:13:45,060 ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'langService': 
Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'langDao' defined in URL [jar:file:/C:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/NCFrameworkAdmin/WEB-INF/lib/NCFramework-0.0.1-SNAPSHOT.jar!/com/ns/commerce/framework/lang/dao/LangDaoImpl.class]: 
Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/admin-hibernate.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Wrong column type in db.nc_alert_log for column alerted. Found: bit, expected: TINYINT(1) DEFAULT 0; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/admin-hibernate.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Wrong column type in db.nc_alert_log for column alerted. Found: bit, expected: TINYINT(1) DEFAULT 0 

有趣的是blogController,langServices和朗道是相關的,但是他們什麼都沒有關於「nc_alert_log」表。

LangDaoImpl

package com.ns.commerce.framework.lang.dao; 
import org.hibernate.Criteria; 
import org.hibernate.SessionFactory; 
import org.hibernate.criterion.Restrictions; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.stereotype.Repository; 
import com.ns.commerce.framework.lang.model.Lang; 
import com.ns.commerce.framework.generic.dao.GenericDAOImpl; 

@Repository("langDao") 
public class LangDaoImpl extends GenericDAOImpl<Lang, Long> implements LangDao { 

@Autowired 
public LangDaoImpl(@Qualifier("sessionFactory") SessionFactory sessionFactory) { 
    this.setSessionFactory(sessionFactory); 
} 

@Override 
public Lang findByLocaleCode(String localeCode) { 
    Criteria criteria = getCriteria(); 
    criteria.add(Restrictions.eq("localeCode", localeCode));   
    return findByCriteriaFirst(criteria);  
} 

@Override 
public Lang findBySubdomain(String subdomain) { 
    Criteria criteria = getCriteria(); 
    criteria.add(Restrictions.eq("subdomain", subdomain)); 
    return findByCriteriaFirst(criteria);  
} 

@Override 
public Lang findDefaultLang() { 
    Criteria criteria = getCriteria(); 
    criteria.add(Restrictions.eq("defaultFlag", true)); 
    return findByCriteriaFirst(criteria);  
} 
} 

AlertLog.Java模型

@Column(name="alerted", columnDefinition = "TINYINT(1) DEFAULT 0") 
private int alerted; 

在數據庫:驚動柱> TINYINT(1)和deafult值爲0

DB .properties

hibernate.hbm2ddl.auto=validate 
#hibernate.hbm2ddl.auto=create-drop 
hibernate.hbm2ddl.import_files=/import_standard.sql 
hibernate.show_sql=false 
hibernate.format_sql=true 
hibernate.generate_statistics=false 
hibernate.use_sql_comments=true 
hibernate.query.factory_class=org.hibernate.hql.ast.ASTQueryTranslatorFactory 
hibernate.cache.use_query_cache=true 
hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory 
hibernate.cache.use_second_level_cache=true 
#------------------------------------------------------------------------------- 
# MySQL Settings 

jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost:3306/asd?autoReconnect=true 
jdbc.username=asd 
jdbc.password=asd 

# Property that determines which Hibernate dialect/MySQL5Dialect || MySQLDialect 
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 

的pom.xml

<?xml version="1.0"?> 
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<modelVersion>4.0.0</modelVersion> 
<parent> 
<artifactId>NC_Core</artifactId> 
<groupId>com.ns.commerce</groupId> 
<version>0.0.1-SNAPSHOT</version> 
</parent> 
<artifactId>NCFrameworkAdmin</artifactId> 
<packaging>war</packaging> 
<name>NCFrameworkAdmin Maven Webapp</name> 
<url>http://maven.apache.org</url> 
<properties> 
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 
<dependencies> 
    <dependency> 
     <groupId>com.nc.commerce</groupId> 
     <artifactId>NCFramework</artifactId> 
    </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <exclusions> 
       <!-- Exclude Commons Logging in favor of SLF4j --> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-core</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
     </dependency>      
     <dependency> 
      <groupId>org.springframework.security</groupId>   
      <artifactId>spring-security-config</artifactId>   
     </dependency> 

     <!-- Logging --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>jcl-over-slf4j</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <exclusions> 
       <exclusion> 
        <groupId>javax.mail</groupId> 
        <artifactId>mail</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>javax.jms</groupId> 
        <artifactId>jms</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jdmk</groupId> 
        <artifactId>jmxtools</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jmx</groupId> 
        <artifactId>jmxri</artifactId> 
       </exclusion> 
      </exclusions> 
      <scope>runtime</scope> 
     </dependency> 

     <!-- Servlet --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
     </dependency> 


     <!-- Joda Time Library --> 
     <dependency> 
      <groupId>joda-time</groupId> 
      <artifactId>joda-time</artifactId> 
     </dependency> 

     <!-- Jackson JSON Mapper --> 
     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
     </dependency> 

     <!--Regular Expression Libraries --> 
     <dependency> 
      <groupId>oro</groupId> 
      <artifactId>oro</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>jakarta-regexp</groupId> 
      <artifactId>jakarta-regexp</artifactId> 
     </dependency> 

     <!-- Commons validator --> 
     <dependency> 
      <groupId>commons-validator</groupId> 
      <artifactId>commons-validator</artifactId> 
     </dependency>   

     <!-- Tiles --> 
     <dependency> 
      <groupId>org.apache.tiles</groupId> 
      <artifactId>tiles-jsp</artifactId> 
     </dependency> 

     <!-- AOP dependency --> 
     <dependency> 
      <groupId>cglib</groupId> 
      <artifactId>cglib</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>c3p0</groupId> 
      <artifactId>c3p0</artifactId> 
     </dependency> 
    </dependencies> 
<build> 
<finalName>NCFrameworkAdmin</finalName> 

如果需要任何其他來源請評論。

+1

如何創建數據庫?您是否使用hibernate生成/更新/創建模式,或者您已經存在表? 我猜'AlertLog'被映射到'nc_alert_log'。看看'alerts'的列定義,我認爲它是有點而不是tinyint,並導致錯誤。 –

+0

仔細檢查您是否爲正確的_dialect_配置了hibernate以匹配您正在使用的數據庫。 –

+0

András:我正在使用現有的數據庫。 伊恩:INFO [org.hibernate.cfg.SettingsFactory] ​​ - 數據庫 - > 名稱:MySQL的 版本:5.0.96社區-NT 大:5 輕微:0 2013年1月17日05:13: 4476 INFO [org.hibernate.cfg.SettingsFactory] ​​ - 驅動程序 - > 名稱:MySQL-AB JDBC驅動程序 版本:mysql-connector-java-5.1.12(修訂版:$ {bzr.revision-id}) major: 5 輕微:1 我嘗試改變方言def。從 hibernate.dialect = org.hibernate.dialect。MySQL5Dialect到 hibernate.dialect = org.hibernate.dialect.MySQLDialect,但不起作用。 – efirat

回答

1
Wrong column type in db.nc_alert_log for column alerted. 
Found: bit, expected: TINYINT(1) DEFAULT 0 

導致該問題。檢查數據庫中alerted列的類型,並確保在AlertLog.java中使用相同的列。

+0

我檢查了'驚動'並且它的TININT(1)默認值是0. – efirat

1

nc_alert_log與LangDao沒有直接關係,但LangDao和hibernate的sessionFactory都是。 sessionFactory不能被創建,因爲數據庫定義是錯誤的。 eror表示數據庫中的columntype不是TINYINT,而是BIT(布爾值)。這就是sessionFactory無法啓動的原因。如果沒有sesionFactory的朗道不能被創建等等等等

+0

所以我應該將「驚動」列「TINYINT(1)DEFAULT 0」的定義更改爲「BOOLEAN」? – efirat

1
private int alerted; 

嘗試更改爲:

private boolean alerted;