首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法创建表: Hibernate

无法创建表: Hibernate
EN

Stack Overflow用户
提问于 2016-04-18 20:31:58
回答 3查看 729关注 0票数 0

下面是我的spring-config.xml

代码语言:javascript
复制
<?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>

和persistence.xml

代码语言:javascript
复制
<?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>

以及build.gradle中包含hibernate依赖项的代码片段:

代码语言:javascript
复制
//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'的表之外,数据库中没有任何表。实体被粘贴到下面:

代码语言:javascript
复制
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;
- - -

在日志中找到查询:

代码语言:javascript
复制
18:39:02.650 [localhost-startStop-1] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'persistenceUnit' 
Hibernate: 
drop table if exists hibernate_sequence
Hibernate: 
drop table if exists Tags
Hibernate: 
create table hibernate_sequence (
    next_val bigint
) ENGINE=InnoDB
Hibernate: 
insert into hibernate_sequence values ( 1 )
Hibernate: 
create table Tags (
    id bigint not null,
    createdDate datetime,
    desc varchar(255),
    questions varchar(255),
    seoTitle varchar(255),
    status smallint not null,
    title varchar(255),
    primary key (id)
) ENGINE=InnoDB
18:39:04.618 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 

请帮帮我。谢谢

EN

回答 3

Stack Overflow用户

发布于 2016-04-18 20:55:22

<persistence-unit>中添加您的类

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

票数 1
EN

Stack Overflow用户

发布于 2016-04-18 21:05:30

您可以添加以下属性

代码语言:javascript
复制
<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>

代码语言:javascript
复制
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">

spring自动检测你的实体。

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

代码语言:javascript
复制
<property name="packagesToScan" value="com.test.core.domain" />
票数 0
EN

Stack Overflow用户

发布于 2016-04-19 04:12:20

Obi Wan - PallavJha

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36694108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档