无法创建表:Hibernate

时间:2016-04-18 12:31:58

标签: java spring hibernate spring-mvc

以下是我的spring-config.xml

var reader = new Baml2006Reader(stream);

和persistence.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:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:annotation-config/>
<context:component-scan base-package="com.nm"/>

<mvc:annotation-driven/>


<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
    <property name="driverClassName" value="${dbdriverClassName}"/>
    <property name="url" value="${db-url}"/>
    <property name="username" value="${db-username}"/>
    <property name="password" value="${db-password}"/>
    <!-- <property name="initialPoolSize" value="1" /> <property name="minPoolSize"
        value="1" /> <property name="maxPoolSize" value="10" /> -->
</bean>


<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" >
        <list>
            <value>com.nm</value>
        </list>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy"</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
</bean>



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

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:property-files/config-info.properties"/>
</bean>

<tx:annotation-driven/>
</beans>

和build.gradle中包含hibernate依赖项的片段:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
        <property name="hibernate.connection.charSet" value="UTF-8"/>
        <property name="show_sql" value="true"/>
        <property name="format_sql" value="true"/>
    </properties>
</persistence-unit>

服务器启动后,除了名为//hibernate dependenciess compile 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final' compile 'org.hibernate:hibernate-core:5.1.0.Final' compile 'org.hibernate:hibernate-entitymanager:5.1.0.Final' compile 'org.hibernate:hibernate-search-orm:5.5.2.Final' compile 'org.hibernate:hibernate-validator:5.1.0.Final' 的表外,数据库中没有任何表。该实体粘贴在下面:

'hibernate_sequence'

在日志中找到的查询:

import javax.persistence.*;
import java.sql.Timestamp;

/**
 * Created by pallav on 15/4/16.
 */

@Entity
public class Tags {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String seoTitle;
private String desc;
private String questions;
private short status;
- - -

请帮助我。感谢

3 个答案:

答案 0 :(得分:1)

<persistence-unit>

中添加您的课程

<class>your.project.Tags</class>

答案 1 :(得分:0)

您可以添加以下属性

<property name="packagesToScan">
    <list>
        <value>com.test.core.domain</value> //Replace this with the package where you have your domain entities.
        <value>...</value>
    </list>
<property>

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">

让spring自动检测您的实体。

如果您将所有实体都放在一个包中,则可以将其简化为

<property name="packagesToScan" value="com.test.core.domain" />

答案 2 :(得分:0)

Obi Wan - PallavJha

为什么spring-config.xml和persistence.xml都具有相同的持久性信息。在我看来,弹簧上下文容器中可能存在冲突,以确定应该优先考虑哪两个。在我看来,删除一个(可能是persistence.xml)并尝试查看问题是否仍然存在。