Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9245499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:16:49+00:00 2026-06-18T09:16:49+00:00

I am using org.hibernate.tool.hbm2ddl.SchemaExport to generate an SQL script for DB creation on PostgreSQL.

  • 0

I am using org.hibernate.tool.hbm2ddl.SchemaExport to generate an SQL script for DB creation on PostgreSQL.

Simplified example:

Properties entityManagerFactoryProperties = new Properties();
entityManagerFactoryProperties.put( "hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect" );
[…]
Configuration configuration = new Configuration();
configuration.setProperty( "hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect" );
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( persistenceUnitName, entityManagerFactoryProperties );
Metamodel metamodel = entityManagerFactory.getMetamodel();
for ( final ManagedType< ? > managedType : metamodel.getManagedTypes() ) {
    Class< ? > entityClass = managedType.getJavaType();
    configuration.addAnnotatedClass( entityClass );
}
SchemaExport schemaExport = new SchemaExport( configuration );
schemaExport.setOutputFile( schemaFile );
schemaExport.setFormat( true );
schemaExport.setDelimiter( ";" );
schemaExport.create( true, false );

The generated schema looks like that

alter table TableName 
    drop constraint FK101841AFEA9FC;
[…]
drop table if exists TableName cascade;
[…]
create table TableName (
    dbId int8 not null,
    […]
    compositeSingle_dbId int8,
    primary key (dbId)
);
alter table TableName 
    add constraint FK101841AFEA9FC 
    foreign key (compositeSingle_dbId) 
    references TableName2;

The problem is that if the database is empty the first series of command (ALTER TABLE) will fail as they make sense only if the table exists. PostgreSQL then performs a complete roll back and nothing is created.

This happens only for the constraints: for the table the DROP statement is correctly enhanced with IF EXISTS

Is this an Hibernate bug or am I doing something wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:16:50+00:00Added an answer on June 18, 2026 at 9:16 am

    Instead of

    schemaExport.create( true, false );
    

    call

    schemaExport.execute(true, false, false, true)
    

    It will create DDL without alter/drop statements

    (see javadoc for details http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/tool/hbm2ddl/SchemaExport.html)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to generate SQL DDL scripts from Hibernate/JPA Annotation Classes using ANT.
Using Hibernate 4.1.1.Final. When I try to add @ManyToOne, schema creation fails with: org.hibernate.MappingException:
using this in my hibernate cfg: <property name=hibernate.ejb.naming_strategy>org.hibernate.cfg.ImprovedNamingStrategy</property> <property name=hibernate.hbm2ddl.auto>create</property> This should make all
When using hibernate to retrieve data from Oracle 11g DB using either org.hibernate.dialect.Oracle10gDialect or
I am using Hibernate and getting Exception in thread main org.hibernate.ObjectNotFoundException: No row with
In my spring-hibernate application, we are using org.apache.tomcat.jdbc.pool.DataSource for connection pooling. When we start
I am using hibernate as ORM tool. It's working properly, but the problem is
I am using org.hibernate.validator.Pattern annotation in my jsf managed bean to validate an <h:inputText>
I am using org.hibernate.cfg.ImprovedNamingStrategy, But for a table I have specified the table name
We're currently using org.springframework.orm.hibernate3.LocalSessionFactoryBean to configure Hibernate in our app. It loads about fifty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.