Posts

Showing posts from June, 2018

Postgresql , Hibernate and issues with Schemas

In Hibernate you can easily map an entity class to a database table using @Entity @Table(name="student") public class Student implements Serializable {} But if you are using Postgresql and you are on a schema, you cannot use the above. If you use the above method, it will result follwoing error Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1460) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:511) at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3278) at org.hibern...

How to use Hibernate 5 to insert Postgresql auto incrementing primary key

Postgresql does not have auto_increment keyword functionality on table creation as in mySQL. The best way to do is to use SERIAL or BIGSERIAL data type on the primary key. Serial data type act as an auto increment sequence in the background. This automatically created sequence usually has following name pattern:  student_id_seq You can create a simple table with auto incrementing primary key as bellow in postgresql 9.x: CREATE TABLE student (     id serial NOT NULL ,     first_name character varying(45),     last_name character varying(45),      email character varying(45),      CONSTRAINT student_pkey PRIMARY KEY (id)  ) This will automatically create a sequence called student_id_seq in the background, which will increment the id value by 1 with each increment. When inserting data to the table using SQL you don't need to give any value for the ID You can simply insert data using inse...