簡體   English   中英

Spring Hibernate無需創建表

[英]Spring Hibernate without creating tables

我創建了一個數據庫,並從中生成了一些實體。 現在,我想插入Spring框架。 問題是,當我部署到Wildfly時,它會嘗試在數據庫中創建表,但由於表在那里而失敗。

我的context.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" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <context:annotation-config />

    <!--
           DATABASE SETUP 
    -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username" value="****" />
        <property name="password" value="****" />
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@1.1.1.1:1521:XE" />
    </bean>

    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="examplePU" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="userDao" class="com.foobar.dao.user.JpaUserDao">
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <!--
           INIT REST COMPONENTS 
    -->

    <context:component-scan base-package="com.foobar.rest.resources" />

    <bean id="objectMapper" class="org.codehaus.jackson.map.ObjectMapper" />

    <!-- 
            SPRING SECURITY SETUP
    -->

    <bean id="passwordEncoder" class="com.foobar.security.SaltedSHA256PasswordEncoder">
        <constructor-arg value="secret" />
    </bean>

    <security:authentication-manager id="authenticationManager">
        <security:authentication-provider user-service-ref="userDao">
            <security:password-encoder ref="passwordEncoder"></security:password-encoder>
        </security:authentication-provider>
    </security:authentication-manager>

    <security:http
        realm="Protected API"
        use-expressions="true"
        auto-config="false"
        create-session="stateless"
        entry-point-ref="unauthorizedEntryPoint"
        authentication-manager-ref="authenticationManager">
        <security:custom-filter ref="authenticationTokenProcessingFilter" position="FORM_LOGIN_FILTER" />
        <security:intercept-url pattern="/rest/user/authenticate" access="permitAll" />
        <security:intercept-url method="GET" pattern="/rest/news/**" access="hasRole('user')" />
        <security:intercept-url method="PUT" pattern="/rest/news/**" access="hasRole('admin')" />
        <security:intercept-url method="POST" pattern="/rest/news/**" access="hasRole('admin')" />
        <security:intercept-url method="DELETE" pattern="/rest/news/**" access="hasRole('admin')" />
    </security:http>

    <bean id="unauthorizedEntryPoint" class="com.foobar.rest.UnauthorizedEntryPoint" />

    <bean class="com.foobar.rest.AuthenticationTokenProcessingFilter" id="authenticationTokenProcessingFilter">
        <constructor-arg ref="userDao" />
    </bean>

</beans>

這是野蠅的日志

2014-04-02 22:31:54,170 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 41) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "portal-web-1.0-SNAPSHOT.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.persistenceunit.\"portal-web-1.0-SNAPSHOT.war#OneW2\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"portal-web-1.0-SNAPSHOT.war#OneW2\": javax.persistence.PersistenceException: Unable to execute JPA schema generation create command [***]
    Caused by: javax.persistence.PersistenceException: Unable to execute JPA schema generation create command [***]
    Caused by: org.h2.jdbc.JdbcSQLException: Table \"XXX\" already exists; SQL statement:
*** [42101-173]"}}
2014-04-02 22:31:54,172 ERROR [org.jboss.as.server] (management-handler-thread - 41) JBAS015870: Deploy of deployment "portal-web-1.0-SNAPSHOT.war" was rolled back with the following failure message: 
{"JBAS014671: Failed services" => {"jboss.persistenceunit.\"portal-web-1.0-SNAPSHOT.war#OneW2\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"portal-web-1.0-SNAPSHOT.war#OneW2\": javax.persistence.PersistenceException: Unable to execute JPA schema generation create command [***]
    Caused by: javax.persistence.PersistenceException: Unable to execute JPA schema generation create command [***]
    Caused by: org.h2.jdbc.JdbcSQLException: Table \"XXX\" already exists; SQL statement:
*** [42101-173]"}}
2014-04-02 22:31:54,174 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) JBAS010418: Stopped Driver service with driver-name = portal-web-1.0-SNAPSHOT.war_org.hsqldb.jdbc.JDBCDriver_2_2
2014-04-02 22:31:54,173 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 77) JBAS011410: Stopping Persistence Unit (phase 2 of 2) Service 'portal-web-1.0-SNAPSHOT.war#examplePU'
2014-04-02 22:31:54,177 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 76) JBAS011410: Stopping Persistence Unit (phase 1 of 2) Service 'portal-web-1.0-SNAPSHOT.war#examplePU'
2014-04-02 22:31:54,177 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 77) JBAS011410: Stopping Persistence Unit (phase 1 of 2) Service 'portal-web-1.0-SNAPSHOT.war#OneW2'
2014-04-02 22:31:54,336 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-5) JBAS015877: Stopped deployment portal-web-1.0-SNAPSHOT.war (runtime-name: portal-web-1.0-SNAPSHOT.war) in 163ms
2014-04-02 22:31:54,337 INFO  [org.jboss.as.controller] (management-handler-thread - 41) JBAS014774: Service status report
JBAS014775:    New missing/unsatisfied dependencies:
      service jboss.deployment.unit."portal-web-1.0-SNAPSHOT.war".component."com.sun.jersey.spi.spring.container.servlet.SpringServlet".CREATE (missing) dependents: [service jboss.deployment.unit."portal-web-1.0-SNAPSHOT.war".component."com.sun.jersey.spi.spring.container.servlet.SpringServlet".START] 
      service jboss.deployment.unit."portal-web-1.0-SNAPSHOT.war".component."org.springframework.web.context.ContextLoaderListener".CREATE (missing) dependents: [service jboss.deployment.unit."portal-web-1.0-SNAPSHOT.war".component."org.springframework.web.context.ContextLoaderListener".START] 
      service jboss.deployment.unit."portal-web-1.0-SNAPSHOT.war".ee.ComponentRegistry (missing) dependents: [service jboss.undertow.deployment.default-server.default-host."/portal-web-1.0-SNAPSHOT".UndertowDeploymentInfoService] 
JBAS014777:   Services which failed to start:      service jboss.persistenceunit."portal-web-1.0-SNAPSHOT.war#OneW2"

如何禁用桌面娛樂?

問題可能出在您的休眠配置文件中。

Please look into a property hibernate.hbm2ddl.auto

它可以是這些值之一,並且可以解釋每個值。

validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session.

因此,根據您的需要,在文件中設置屬性

嘗試將以下行放在jpaProperties屬性內

<prop key="hibernate.hbm2ddl.auto">validate</prop>

它應該工作。

我假設您使用的是Hibernate的hbm2ddl功能。

然后,您應該使用驗證值:

 <property name="hibernate.hbm2ddl.auto" value="validate">

有關此功能,請參見Hibernate文檔

在JPA屬性中添加以下內容

<prop key="hibernate.hbm2ddl.auto">update</prop>

這將在表結構發生更改的情況下更改表,在表不存在的情況下創建新表,在表存在的情況下將插入到表中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM