2017-09-15 85 views
1

我已經使用Spring和Hibernate配置了我的web項目。下面是POM:Spring + Hibernate - 表沒有在MySQL中創建表

<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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.medosa</groupId> 
    <artifactId>MedoSa-backend</artifactId> 
    <version>1</version> 
    <packaging>war</packaging> 
    <name>MedoSa-backend</name> 
    <description>A hand for a needed hand</description> 

    <properties> 
     <jdk.version>1.8</jdk.version> 
     <spring.version>4.3.4.RELEASE</spring.version> 
     <jackson.version>1.9.13</jackson.version> 
     <hibernate.version>4.3.11.Final</hibernate.version> 
    </properties> 

    <repositories> 
     <repository> 
      <id>JBoss repository</id> 
      <url>http://repository.jboss.com/maven2/</url> 
     </repository> 
    </repositories> 
    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.12</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>4.3.4.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-tx</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>1.7.3</version> 
     </dependency> 

     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>${jackson.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.9</version> 
     </dependency> 

     <!-- Hibernate library dependecy start --> 
     <dependency> 
      <groupId>com.jolbox</groupId> 
      <artifactId>bonecp</artifactId> 
      <version>0.8.0.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.validation</groupId> 
      <artifactId>validation-api</artifactId> 
      <version>1.1.0.Final</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <version>3.1.0</version> 
     </dependency> 
     <!-- Hibernate framework --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>5.4.1.Final</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations --> 
     <!-- <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-annotations</artifactId> 
      <version>3.5.6-Final</version> 
     </dependency> --> 
    </dependencies> 

    <build> 
     <finalName>MedoSa-Backend</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <target>${jdk.version}</target> 
        <source>${jdk.version}</source> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

以下是我的調度員servlet.xml文件:

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd「>

<context:component-scan base-package="com.medosa" /> 
<mvc:annotation-driven /> 

<context:property-placeholder location="classpath:AppConfig.properties" /> 

<!-- BoneCP configuration --> 
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" 
    destroy-method="close"> 
    <property name="driverClass" value="${db.driverClassName}" /> 
    <property name="jdbcUrl" value="${db.url}" /> 
    <property name="username" value="${db.username}" /> 
    <property name="password" value="${db.password}" /> 
    <property name="idleConnectionTestPeriod" value="60" /> 
    <property name="idleMaxAge" value="240" /> 
    <property name="maxConnectionsPerPartition" value="30" /> 
    <property name="minConnectionsPerPartition" value="10" /> 
    <property name="partitionCount" value="3" /> 
    <property name="acquireIncrement" value="5" /> 
    <property name="statementsCacheSize" value="100" /> 
    <property name="releaseHelperThreads" value="3" /> 
</bean> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">${db.dialect}</prop> 
      <prop key="hibernate.hbm2ddl.auto">create-drop</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 

<tx:annotation-driven /> 
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 
<bean id="persistenceExceptionTranslationPostProcessor" 
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

我已經創建了我的模型類。並用@Entity註釋它們。我也設置了hbm2ddl屬性來創建。無論何時我試圖將其部署到服務器中,都不會生成表。而且也沒有錯誤。

我試圖搜索谷歌上的所有解決方案,嘗試了很多東西,但沒有奏效。 將感謝您的親切幫助

+0

有沒有實體。指定'您'LocalSessionFactoryBean' packagesToScan'檢測實體 –

+0

真的很感謝!它對我有用。 –

回答

1

看起來好像你沒有添加模型類的類路徑。您可以在您的sessionFactory中添加此項。
</property>標籤,添加

<mapping class="<full class path>"/> 

例如 - <mapping class="com.hibernate.UserDetails">

此外,檢查您的控制檯,如果正在顯示的SQL語句。如果表是越來越創建,然後下降,更換

<prop key="hibernate.hbm2ddl.auto">create-drop</prop> 

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

創建拖放:創建一個架構,然後刪除模式時,顯式關閉SessionFactory通常在應用程序停止時。

更新:如果模型類中有任何更改,則創建模式和更新。在服務器重新啓動時,其作爲數據庫使用的更安全的選項不會被刪除或數據不受影響。

create:創建模式,破壞以前的數據。 (使用它,只有在第一次或重新啓動服務器時,下一次可能會導致所有數據被破壞。

希望它可以幫助!