0
@Entity
class MyEntity {
}

hibernate.hbm2ddl.auto=update

Is it possible that hibernate will generate a CREATE unlogged TABLE statement instead of CREATE TABLE when the table for MyEntity does not exist?

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • what do you mean by CREATE unlogged TABLE???? – Dev Apr 23 '15 at 09:41
  • http://www.postgresql.org/docs/9.1/static/sql-createtable.html Data written to unlogged tables is not written to the write-ahead log (see Chapter 29), which makes them considerably faster than ordinary tables. – membersound Apr 23 '15 at 09:53
  • related http://stackoverflow.com/questions/7938610/how-to-apply-postgresql-9-1-unlogged-feature-to-an-existing-table – isalgueiro Apr 23 '15 at 10:37

3 Answers3

1

I think you can do this by subclassing the relevant Postgres dialect and overriding the getCreateTableString() method as below.

public class CustomPostgresSQLDialect extends PostgresSQLxxDialect{

    @Override
    public String getCreateTableString() {
        return "create unlogged table";
    }   
}

And obviously set this as your dialect:

<property name="hibernate.dialect" value="org.foo.CustomPostgresSQLDialect "/>
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • That's a great idea. Anyhow that would force all of my tables to be an unlogged table? (I only want to define one specific). Or can I tie the dialect to an entity explicit? – membersound Apr 23 '15 at 10:03
  • I do not see any easy way. If you look the source of org.hibernate.mapping.Table.sqlCreateString(..) you have info about the table/mapping at this point but obviously it is not passed to the method above. – Alan Hay Apr 23 '15 at 10:16
  • Another option may be to override org.hibernate.cfg.Configuration/AnnotationConfiguration and post-process the relevant String returned by generateSchemaCreationScript(...) or generateSchemeUpdateScript(...) – Alan Hay Apr 23 '15 at 10:22
1

I think there's no param in @Entity or @Table to pass that kind of options to the DBMS. Found this feature request in hibernate's jira though, it covers your use case, maybe you want to vote for it.

Anyway you can try and add an auxiliary database object in the hibernate configuration to run ALTER TABLE MyEntity SET UNLOGGED, it's the easiest way i can think of doing that DDL modification.

isalgueiro
  • 1,973
  • 16
  • 20
0

You should use update rather than create

this is helpful:

Hibernate hbm2ddl.auto default value

Community
  • 1
  • 1
Nimesh
  • 794
  • 3
  • 8
  • 18
  • 1
    So what do you mean by unlogged table. Hbm2dll auto update will create those table for you which does not exist in schema – Nimesh Apr 23 '15 at 09:28
  • Data written to unlogged tables is not written to the write-ahead log (see Chapter 29), which makes them considerably faster than ordinary tables. postgresql.org/docs/9.1/static/sql-createtable.html – membersound Apr 23 '15 at 09:53
  • It is not possible to do it from hibernate. You have to set it through alter sql query on your database – Nimesh Apr 23 '15 at 10:02