1

我知道这个问题之前已经回答过,但似乎没有一个回答对我有帮助,因为我没有在我的项目中使用数据源。

我有一个 Maven 项目,与 Spring 和 Hibernate 集成,连接到 SQL Server 2014 数据库。

这是我的 pom.xml,您可以在其中查看版本:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>BachelorDegree</groupId>
    <artifactId>BuildingAdministration</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>BuildingAdministration Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <build>
        <finalName>BuildingAdministration</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.1.RELEASE</spring.version>
        <spring-security.version>4.1.1.RELEASE</spring-security.version>
        <hibernate.version>5.2.1.Final</hibernate.version>
        <hibernate.ga.version>3.5.4-Final</hibernate.ga.version>
        <hibernate-annotation.version>3.5.6-Final</hibernate-annotation.version>
        <javax-servlet-api.version>3.1.0</javax-servlet-api.version>
        <jquery.version>2.1.1</jquery.version>
        <bootstrap.version>3.2.0</bootstrap.version>
        <javax-servlet.version>1.2</javax-servlet.version>
        <log4j.version>2.6.2</log4j.version>
        <spring.simple>2.5.6</spring.simple>
        <jta.version>1.1</jta.version>
        <aspectjweaver.version>1.8.9</aspectjweaver.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>

        <!-- spring-context which provides core functionality -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>

        <!-- The spring-aop module provides an AOP Alliance-compliant aspect-oriented 
            programming implementation allowing you to define -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>

        <!-- The spring-webmvc module (also known as the Web-Servlet module) contains 
            Spring’s model-view-controller (MVC) and REST Web Services implementation 
            for web applications -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>

        <!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
        </dependency>
        <!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> 
            <version>${hibernate-annotation.version}</version> </dependency> -->
        <!-- <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> 
            <version>1.0.1.Final</version> </dependency> -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>
        <!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> 
            <version>${hibernate.ga.version}</version> </dependency> -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>${hibernate.ga.version}</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>${jta.version}</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.webjars</groupId>
                    <artifactId>jquery</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectjweaver.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>org.springframework.maven.milestone</id>
            <name>Spring Maven Milestone Repository</name>
            <url>http://repo.springsource.org/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>apache.snapshots</id>
            <name>Apache Release Distribution Repository</name>
            <url>
            http://repository.apache.org/snapshots
        </url>
        </repository>
    </repositories>
    <dependencyManagement>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-annotations</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>${hibernate.version}</version>
            </dependency>
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>jquery</artifactId>
                <version>${jquery.version}</version>
            </dependency>
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>bootstrap</artifactId>
                <version>${bootstrap.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-taglibs</artifactId>
                <version>${spring-security.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>${javax-servlet.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

这是hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver
        </property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=Licenta
        </property>
        <property name="connection.username">sa</property>
        <property name="connection.password">admin9</property>
        <property name="default_schema">dbo</property>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect
        </property>
        <property name="hbm2ddl.auto">create</property>

        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.cache.internal.NoCacheProvider
        </property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- <property name="transaction.factory_class">org.transaction.JDBCTransactionFactory</property> -->
        <property name="current_session_context_class">thread</property>
    </session-factory>
</hibernate-configuration>

以及我创建会话工厂的类:

@Configuration
@EnableTransactionManagement
@EnableWebSecurity
@EnableGlobalMethodSecurity
@Import({ SecurityConfig.class })
public class HibernateUtils { 

    @Autowired
    @Qualifier("sessionFactory")
    private static SessionFactory sessionFactory = buildSessionFactory();   

    @Bean
    private static SessionFactory buildSessionFactory() {
        try {
            if (sessionFactory == null) {
                org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration().configure("/hibernate.cfg.xml");
                addAnnotatedClasses(configuration);             
                ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                        configuration.getProperties()).build();

                sessionFactory = new org.hibernate.cfg.Configuration().configure().buildSessionFactory(serviceRegistry);
            }
            return sessionFactory;
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
        System.out.println("enter transactionManager");
        HibernateTransactionManager txManager = new org.springframework.orm.hibernate5.HibernateTransactionManager();
        txManager.setSessionFactory(sessionFactory);

        return txManager;
    }

我使用 Tomcat 8。每当我运行它时,我都会得到 UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]

我已经尝试添加 c3p0 连接池的详细信息,正如我在之前的帖子中看到的那样,我不再收到错误,但服务器也没有启动,只是在创建池后挂在那里。

有人可以帮忙吗?

4

1 回答 1

0

好吧,我已经想通了。似乎,而不是我当前的 sessionFactory 创建,我应该有类似的东西:

StandardServiceRegistryBuilder  serviceRegistryBuilder = new StandardServiceRegistryBuilder();  
                DataSource datasource = getDataSource();
                serviceRegistryBuilder.applySetting(Environment.DATASOURCE, datasource);
                serviceRegistryBuilder.applySettings(configuration.getProperties());
                Properties hibernateProperties = setHibernateProperties();
                serviceRegistryBuilder.applySettings(hibernateProperties);
                StandardServiceRegistry serviceRegistry = serviceRegistryBuilder.build();

                sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

这意味着我将 hibernate.cfg.xml 的属性外部化,如下所示:

@Bean
public static DataSource getDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    dataSource.setUrl("jdbc:sqlserver://localhost:1433;databaseName=Licenta");
    dataSource.setUsername("sa");
    dataSource.setPassword("admin9");
    return dataSource;
}

 private static Properties setHibernateProperties() {
     Properties properties = new Properties();
     properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServer2008Dialect");
     properties.put("hibernate.hbm2ddl.auto", "create");
     properties.put("hibernate.show_sql", "true");
     return properties;        
}
于 2016-08-24T13:37:11.357 回答