Nagaraj S Kharvi
Nagaraj S Kharvi

Reputation: 73

Hibernate is not creating Tables in PostgreSQL in given Schema

I am using PostgreSQL and created table and schema called hib. When i create alien table in db and run the application it successfully persist the data into database. But if the table is not there in database then Hibernate is not creating table inside hib schema i think hibernate is not reading hdm2ddl.auto property which is mentioned in hibernate.cfg.xml. As per logs its not giving logs like creating or updating table if exists. It directly tries to insert values. Tried many ways but its not working like create, update, create-drop. Every time same error ERROR: relation "hib.alien" does not exist. Here is persistent class.

@Entity()
@Table(name="hib.alien")
public class Alien {
    @Id
    private int aid;
    private String aname;
    private String acolor;

    public Alien() {}

    public Alien(int aid, String aname, String acolor) {
        this.aid = aid;
        this.aname = aname;;
        this.acolor = acolor;
    }

    public int getAid() {
        return aid;
    }

    public void setAid(int aid) {
        this.aid = aid;
    }

    public String getAname() {
        return aname;
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public String getAcolor() {
        return acolor;
    }

    public void setAcolor(String acolor) {
        this.acolor = acolor;
    }

    @Override
    public String toString() {
        return "Alien [aid=" + aid + ", aname=" + aname + ", acolor=" + acolor + "]";
    }
}

hibernate.cfg.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hib</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">123</property>
        <property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

And Main Class

public class App 
{
    public static void main( String[] args )
    {
        Alien alien = new Alien(1, "Nagaraj Kharvi", "Red");
        System.out.println(alien);

        Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class);
        SessionFactory sf = con.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        s.save(alien);
        tx.commit();
    }
}

Here is logs :

INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
Mar 03, 2019 10:28:08 AM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: insert into hib.alien (acolor, aname, aid) values (?, ?, ?)
Mar 03, 2019 10:28:09 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42P01
Mar 03, 2019 10:28:09 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: relation "hib.alien" does not exist
  Position: 13
Mar 03, 2019 10:28:09 AM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.exception.SQLGrammarException: could not execute statement]
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement

Upvotes: 1

Views: 1987

Answers (1)

Anuj Tiwari
Anuj Tiwari

Reputation: 26

For PostgreSQL entity should be like below, So that table will create in that schema.

 @Entity()
 @Table(name = "alien"  schema = "\"hib\"")
 public class Alien {
    @Id
    private int aid;
    public int getAid() {
        return aid;
    }
    public void setAid(int aid) {
        this.aid = aid;
    }
  }

Upvotes: 1

Related Questions