[英]Unable to create Table : Hibernate
以下是我的spring-config.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>
和persistence.xml
<?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 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;
- - -
在日志中找到查詢:
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'
請幫我。 謝謝
將類添加到<persistence-unit>
<class>your.project.Tags</class>
您可以添加以下屬性
<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">
春季自動檢測您的實體。
如果所有實體都放在一個包中,則可以簡化為
<property name="packagesToScan" value="com.test.core.domain" />
為什么要有一個均具有相同持久性信息的spring-config.xml和persistence.xml。 在我看來,春季上下文容器在確定應優先考慮2個中的哪個方面可能存在沖突。 我認為,刪除一個(可能是persistence.xml)並嘗試查看問題是否仍然存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.