我怎样才能让hibernate创建表,只有它们不存在?

时间:2017-02-16 04:39:49

标签: java postgresql hibernate

我怎样才能让hibernate创建表,只有它们不存在?我只是设置了#34; hibernate.hbm2ddl.auto"到"更新"但它在重启服务器时出错,即错误就像"关系已经存在"

我的persistence.xml: -

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0">
<persistence-unit name="com.adwork">
    <description>Adwork</description>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!-- <class>com.mkyong.bean.User</class>  -->
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
        <property name="show_sql" value="true"/>
        <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>

        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/adwork"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="root"/>
    </properties>
</persistence-unit>

请帮忙。感谢

这是错误: -

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationStartUp' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: com.adwork] Unable to build Hibernate SessionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: com.adwork] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:877)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:805)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at com.adwork.survey.config.ApplicationStartUp.initIt(ApplicationStartUp.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1706)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1645)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 25 more
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [alter table person add column createdDate timestamp not null]
at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:59)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlString(SchemaMigratorImpl.java:420)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlStrings(SchemaMigratorImpl.java:438)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.migrateTable(SchemaMigratorImpl.java:247)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigrationToTargets(SchemaMigratorImpl.java:170)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:60)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:129)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:97)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:481)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:802)
... 36 more
Caused by: org.postgresql.util.PSQLException: ERROR: column "createddate" of relation "person" already exists
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2270)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1998)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:406)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:56)
... 46 more

我的域名类: -

@Entity
@Table(name = "person")
public class Person implements Serializable
{
/**
 * 
 */
private static final long serialVersionUID = 5129388836708407722L;

@Id
private Long id;

@OneToOne
@JoinColumn(name = "userId")
private Users user;

@Column(name = "firstName",nullable = false)
private String firstName;

@Column(name = "middleName",nullable = false)
private String middleName;

@Column(name = "lastName",nullable = false)
private String lastName;

@Column(name = "gender",nullable = false)
private String gender;

@Column(name = "dateOfBirth",nullable = false)
private Date dateOfBirth;

@Column(name = "age",nullable = false)
private Byte age;

@Column(name = "createdDate",nullable = false)
private Timestamp createdDate;

@Column(name = "lastUpdatedDate",nullable = false)
private Timestamp lastUpdateDate;

public Person() 
{
    // TODO Auto-generated constructor stub
}
//and setters / getters 
}

数据库创建表脚本: -

CREATE TABLE person
(
  id bigint NOT NULL,
  age smallint NOT NULL,
  createddate timestamp without time zone NOT NULL,
  dateofbirth date NOT NULL,
  firstname character varying(255) NOT NULL,
  gender character varying(255) NOT NULL,
  lastname character varying(255) NOT NULL,
  lastupdateddate timestamp without time zone NOT NULL,
  middlename character varying(255) NOT NULL,
  userid bigint,
  CONSTRAINT person_pkey PRIMARY KEY (id),
  CONSTRAINT fkd8ja1o2m8k25sf0mxvhu4c0ti FOREIGN KEY (userid)
  REFERENCES users (user_id) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE person
  OWNER TO root;

更改了列名的表: -

CREATE TABLE person
(
  id bigint NOT NULL,
  age smallint NOT NULL,
  "createdDate" timestamp without time zone NOT NULL,
  dateofbirth date NOT NULL,
  firstname character varying(255) NOT NULL,
  gender character varying(255) NOT NULL,
  lastname character varying(255) NOT NULL,
  lastupdateddate timestamp without time zone NOT NULL,
  middlename character varying(255) NOT NULL,
  userid bigint,
  CONSTRAINT person_pkey PRIMARY KEY (id),
  CONSTRAINT fkd8ja1o2m8k25sf0mxvhu4c0ti FOREIGN KEY (userid)
  REFERENCES users (user_id) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE person
  OWNER TO root;

现在我重新启动服务器并发生新错误: -

Feb 21, 2017 11:26:07 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1572 ms
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/sts-bundle/pivotal-tc-server- developer-3.2.2.RELEASE/base-instance/wtpwebapps/FeePaymentPortal/WEB-INF/lib/slf4j-log4j12-1.6.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/sts-bundle/pivotal-tc-server-developer-3.2.2.RELEASE/base-instance/wtpwebapps/FeePaymentPortal/WEB-INF/lib/slf4j-simple-1.7.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Tue Feb 21 11:26:11 IST 2017]; 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.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/security-context.xml]
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
WARN : org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl - HHH000402: Using Hibernate built-in connection pool (not for production use!)
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table role drop constraint FKgg3583634e0ydkacyk8wbbm19
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - ERROR: relation "role" does not exist
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table user_roles drop constraint FKhfh9dx7w3ubf1co1vdev94g3f
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - ERROR: relation "user_roles" does not exist
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 7665 ms
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext -  Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Tue Feb 21 11:26:18 IST 2017]; parent: Root WebApplicationContext
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET]}" onto public java.lang.String org.giks.controllers.HomeController.home(org.springframework.ui.ModelMap)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/payment-month],methods=[GET]}" onto public java.lang.String org.giks.controllers.HomeController.paymentMonth(org.springframework.ui.ModelMap)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/payment-type],methods=[GET]}" onto public java.lang.String org.giks.controllers.HomeController.paymentMethod(org.springframework.ui.ModelMap)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/payment-verified],methods=[GET]}" onto public java.lang.String org.giks.controllers.HomeController.paymentVerified(org.springframework.ui.ModelMap)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/student-fees],methods=[GET]}" onto public java.lang.String org.giks.controllers.HomeController.getStudentFees(org.springframework.ui.ModelMap)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/student-details],methods=[GET]}" onto public java.lang.String org.giks.controllers.HomeController.getStudentDetails(org.springframework.ui.ModelMap)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'appServlet-servlet': startup date [Tue Feb 21 11:26:18 IST 2017]; parent:  Root WebApplicationContext
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'appServlet-servlet': startup date [Tue Feb 21 11:26:18 IST 2017]; parent: Root WebApplicationContext
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 'appServlet': initialization completed in 2426 ms
Feb 21, 2017 11:26:21 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 14090 ms

1 个答案:

答案 0 :(得分:1)

相信主要问题是PostgreSQL列名不区分大小写(默认情况下它会将未加引号的列名转换为小写),但Hibernate是。

例如,使用原始createddate列:

  • 您的域类指定createdDate,大写D.
  • 您的表格使用createddate,小写字母D.
  • Hibernate,在尝试更新架构时(hibernate.hbm2ddl.auto = update),查找域类中指定的名为createdDate的列,找不到因为"createddate" != "createdDate",{case}} -sensitive,因此认为需要添加列并尝试这样做。
  • 添加操作失败,因为根据PostgreSQL(不区分大小写)存在具有等效名称的列。

所以你的选择是:

  1. 在域类中指定小写列名称,或
  2. 确保表中的列名使用匹配大小写,并注意列创建语句中的you must quote the column name以使其保留大小写。
  3. 如您所见,当您将表列名称手动更改为createdDate时,它解决了第一个错误(从而暴露了新错误)。

    至于关于&#34;关系的新错误...不存在&#34;,我不完全确定,但我认为 this is also connected to case-sensitivity issues in table names,你和#39;我必须进一步调查。至少,这是一种可能性。

    我没有足够地使用PostgreSQL与Hibernate知道是否有更简单的选择,例如一些方言选项或某种东西可以让它们一起玩得很好。