1

この質問は以前に回答されていることは知っていますが、プロジェクトでデータソースを使用していないため、どの回答も役に立たないようです。

私は、Spring および Hibernate と統合され、SQL Server 2014 データベースに接続する Maven プロジェクトを持っています。

これは私の 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 に答える