N Chita
N Chita

Reputation: 11

Unable to create table with hibernate hbm2ddl set to update.

I am a newbie in hibernate and trying out basic stuff in hibernate . I am using hibernate 5.0.2 and PostgreSQL 9.3 The error I am getting is as below

INFO: HHH000228: Running hbm2ddl schema update
Exception in thread "main" org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [create schema user]
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:409)
....
....
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "user"

Here is my POJO:

package com.temp.ims.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(schema="user")
public class User {

@Id
private int id;
private String name;


public User() {
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
}

I have used the following 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="TestSessionFactory">
     <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
     <property name="hibernate.connection.password">admin</property>
     <property name="hibernate.connection.url">jdbc:postgresql://localhost:5433/xyzdb</property>
     <property name="hibernate.connection.username">admin</property>
     <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
     <property name="hibernate.connection.pool_size">5</property>
     <property name="hibernate.show_sql">true</property>
     <property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>

Below is code for getting SessionFactory:

standardServiceRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
metadata = new metadataSources(standardServiceRegistry).addAnnotatedClass(User.class).addAnnotatedClassName("com.temp.ims.model.User").getMetadataBuilder()                 .applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
                .build();

sessionFactory = metadata.getSessionFactoryBuilder().build();

Please provide the solution to resolve above error.

Upvotes: 1

Views: 1523

Answers (1)

tddmonkey
tddmonkey

Reputation: 21184

"user" is a reserved word in postgres so you can't use that. Using raw DDL you can enclose it in quotes, but with Hibernate it would probably be easier to just rename your table

Upvotes: 2

Related Questions