我有一个简单的项目构建,在TomCat 6上运行maven2
和Spring3MVC
。我希望hibernate在运行时创建表,如果它们不存在的话。但是我没有成功。这是我的文件:
hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="DKB">
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">dbk</property>
<property name="hibernate.connection.url">jdbc:postgresql:dbk</property>
<property name="hibernate.connection.username">dbk</property>
<property name="hibernate.default_catalog">dbk</property>
<property name="hibernate.default_schema">dbk</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
</session-factory>
</hibernate-configuration>
servlet-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:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<mvc:annotation-driven />
<mvc:resources
mapping="/resources/**"
location="/resources/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="prefix"
value="/WEB-INF/views/" />
<property
name="suffix"
value=".jsp" />
</bean>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property
name="persistenceUnitName"
value="jtdb" />
<property
name="dataSource"
ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property
name="showSql"
value="true" />
<property
name="generateDdl"
value="true" />
<property
name="databasePlatform"
value="org.hibernate.dialect.PostgreSQLDialect" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<context:component-scan base-package="pl.spot.dkb" />
</beans>
和示例实体类:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Roles")
public class Role {
@Id
@GeneratedValue
@Column(name = "id_r")
int id_r;
@Column(name = "name")
String name;
public int getId_r() {
return id_r;
}
public void setId_r(int id_r) {
this.id_r = id_r;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
此类的persistence.xml
的一部分:
<persistence-unit name="Role">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>foo.bar.Role</class>
</persistence-unit>
我有什么遗漏的吗?
修改 这是TomCat日志:
Using CATALINA_BASE: "C:\Projekty\SpringMVC\Tomcat6"
Using CATALINA_HOME: "C:\Projekty\SpringMVC\Tomcat6"
Using CATALINA_TMPDIR: "C:\Projekty\SpringMVC\Tomcat6\temp"
Using JRE_HOME: "C:\Program Files\Java\jdk1.6.0_26\jre\"
Using CLASSPATH: "C:\Projekty\SpringMVC\Tomcat6\bin\bootstrap.jar"
2013-03-05 11:19:23 org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C
:\Program Files\Java\jdk1.6.0_26\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\app\K.Olejniczak\product\11.2.0\dbhome_1\bin;D:\app
\K.Olejniczak\product\11.2.0\dbhome_2\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Progr
am Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DT
S\Binn\;"C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\";"C:\Program Files (x86)\GtkSharp\2.12\bin";"C:\Program Files (x86)\Java\jdk
1.6.0_38\bin";C:\Program Files\TortoiseSVN\bin;D:\app\K.Olejniczak\product\11.2.0\dbhome_1\BIN;C:\Program Files (x86)\msls;C:\Program Files (x86)\Subv
ersion\bin;C:\Program Files (x86)\gource;C:\Program Files\apache-ant-1.8.4\bin;D:\Research\PhoneGap\DevEnv\adt-bundle-windows-x86\sdk\tools;C:\Program
Files (x86)\ant\bin;D:\Research\PhoneGap\DevEnv\Cordova\android\bin;"C:\Program Files (x86)\Tail for Win32";"C:\Program Files (x86)\apache-maven-3.0.
4\bin";.
2013-03-05 11:19:23 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
2013-03-05 11:19:23 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 759 ms
2013-03-05 11:19:23 org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
2013-03-05 11:19:23 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.36
2013-03-05 11:19:23 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor host-manager.xml
2013-03-05 11:19:23 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor manager.xml
2013-03-05 11:19:24 org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive dbk.war
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Tue Mar 05 11:19:25 CET
2013]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/root
-context.xml]
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.suppor
t.DefaultListableBeanFactory@41759d12: defining beans []; root of factory hierarchy
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 324 ms
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'springServlet': initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'springServlet-servlet': star
tup date [Tue Mar 05 11:19:25 CET 2013]; parent: Root WebApplicationContext
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/spri
ngServlet/servlet-context.xml]
INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for compone
nt scanning
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supporte
d for autowiring
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.suppor
t.DefaultListableBeanFactory@3d5b89c: defining beans [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springf
ramework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,
org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionRe
solver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springfra
mework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.se
rvlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,adminController,homeController,org.springf
ramework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.s
pringframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.spr
ingframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.Def
aultListableBeanFactory@41759d12
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/admin],methods=[GET],params=[],headers=[],consu
mes=[],produces=[],custom=[]}" onto public java.lang.String foo.bar.admin.AdminController.adminHome(java.util.Locale,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[
],produces=[],custom=[]}" onto public java.lang.String foo.bar.HomeController.home(java.util.Locale,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet
.resource.ResourceHttpRequestHandler#0'
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'springServlet': initialization completed in 8123 ms
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'adminServlet': initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'adminServlet-servlet': start
up date [Tue Mar 05 11:19:33 CET 2013]; parent: Root WebApplicationContext
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/spri
ngServlet/adminServlet-context.xml]
INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for compone
nt scanning
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supporte
d for autowiring
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.suppor
t.DefaultListableBeanFactory@1b609c13: defining beans [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.spring
framework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0
,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0
,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionR
esolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springfr
amework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.s
ervlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,adminController,org.springframework.conte
xt.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework
.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.c
ontext.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.DefaultListableBe
anFactory@41759d12
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/admin],methods=[GET],params=[],headers=[],consu
mes=[],produces=[],custom=[]}" onto public java.lang.String foo.bar.admin.AdminController.adminHome(java.util.Locale,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet
.resource.ResourceHttpRequestHandler#0'
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'adminServlet': initialization completed in 190 ms
2013-03-05 11:19:33 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory docs
2013-03-05 11:19:33 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
2013-03-05 11:19:33 org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory ROOT
2013-03-05 11:19:33 org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
2013-03-05 11:19:33 org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
2013-03-05 11:19:33 org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/24 config=null
2013-03-05 11:19:34 org.apache.catalina.startup.Catalina start
INFO: Server startup in 10451 ms
答案 0 :(得分:1)
在hibernate.cfg.xml
中,您需要将hibernate.hbm2ddl.auto
设置为create
。有关该属性的可能值及其确切含义的详细信息,请参阅this post。
答案 1 :(得分:0)
<property name="hibernate.hbm2ddl.auto">create</property>