0

要理解 Spring 中真正的问题是什么,需要对结构有很好的了解。我在这个问题上搜索了两天,找不到任何解决方案。在我的项目中,当我构建它时使用 Hibernate 会给出以下错误消息:

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 和 LangDao 是相关的,但是它们与“nc_alert_log”表无关。

浪道实现

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;

在数据库上:Alerted Column > TINYINT(1) 并且默认值为 0。

数据库属性

    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>

如果需要任何其他来源,请发表评论。

4

3 回答 3

1
private int alerted;

尝试更改为:

private boolean alerted;
于 2013-01-17T11:16:11.067 回答
1
Wrong column type in db.nc_alert_log for column alerted.   
Found: bit, expected: TINYINT(1) DEFAULT 0

导致问题。检查alerted数据库中列的类型,并确保您在AlertLog.java.

于 2013-01-17T10:19:37.583 回答
1

nc_alert_log 和 LangDao 没有直接关系,但是 LangDao 和 hibernate sessionFactory 有关系。sessionFactory 无法创建,因为数据库定义错误。该错误表明数据库中的列类型不是 TINYINT,而是 BIT(布尔值)。这就是 sessionFactory 无法启动的原因。没有 sessionFactory 就无法创建 LangDao 等等。

于 2013-01-17T10:21:11.310 回答