Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

All i want is to execute the following SQL on my PostgreSQL server after my Hibernate SessionFactory has been initialized:

CREATE SCHEMA IF NOT EXISTS "fooschema" AUTHORIZATION "foouser";

Currently I am using the following routine:

Session s      = factory.withOptions().openSession();
SQLQuery query = s.createSQLQuery(sql);
int res        = query.executeUpdate();
// res is 0 and the schema has NOT been created
s.flush();
s.disconnect();
s.close();

The connected user has the permissions to chreate new schemata. So this is a simple question: What am i doing wrong?

Attachments:

Turning hibernate show_sql on prints the following:

Hibernate:
    CREATE SCHEMA IF NOT EXISTS "fooschema" AUTHORIZATION "foouser";
share|improve this question
    
What is the error message? Btw: stuff like that is better done using proper (versioned!) DDL scripts. – a_horse_with_no_name Nov 15 '13 at 12:21
    
There is no error message. – vralfy Nov 15 '13 at 12:25
    
I don't see a commit in there. Maybe that's the problem? And if you are already using Liquibase, why don't you put this into the liquibase change set? – a_horse_with_no_name Nov 15 '13 at 12:29
    
@a_horse_with_no_name i think creating schema cannot be reverted (its executed immidiatly) so commit ist necessary – smajlo Nov 15 '13 at 12:39
    
You are right ... The solution is to put the query in a transaction. – vralfy Nov 15 '13 at 12:39

Try setting property

 <property name="hbm2ddl.auto" value="create"/>

in hibernate config

share|improve this answer
    
Does that also create the schema? I though it would only create the tables. – a_horse_with_no_name Nov 15 '13 at 12:22
    
I am not using hbm2ddl.auto. I just wanted to create the schema for liquibase, which actually creates the tables. – vralfy Nov 15 '13 at 12:25
    
@vralfy can you turn on hibernate.show_sql to true and show what is logged to console when you execute this sql ? – smajlo Nov 15 '13 at 12:27
    
@smajlo attached it above – vralfy Nov 15 '13 at 12:31
    
@vralfy its silly by maybe schema is already created so thats why you getting 0 in return ? – smajlo Nov 15 '13 at 12:34
up vote 0 down vote accepted

The solution for my problem (Thanks to @a_horse_with_no_name)

Session s      = factory.withOptions().openSession();
Transaction tx = s.beginTransaction();
SQLQuery query = s.createSQLQuery(sql);
int res        = query.executeUpdate();
tx.commit();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.