Wednesday, March 30, 2011

Accessing a PostgreSQL table using Hibernate

I was goofing around with PostgreSQL, and I wanted to give it a try, so I decided to replicate my Payment table from HSQLDB to PostgreSQL and I got something like this
CREATE TABLE "PAYMENTS"
(
  id_order integer NOT NULL,
  id_customer integer NOT NULL,
  total_amount_transaction double precision,
  transaction_number character varying(50),
  creation_ts timestamp with time zone,
  CONSTRAINT pk_payments PRIMARY KEY (id_order, id_customer)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "PAYMENTS" OWNER TO payments;
As you well know with Hibernate the switch should be not very painful, and it won't have any impact on existing code!
But...Surprise!

The impact is not a huge impact but it's something really specific, and that's why I'm creating this post.

The usual steps to chnage from one DB to another DB is Hibernate is really simple:
This is my previous DataSource - Because, to accelerate the process I did the change locally before deploying this to my app server, so I can test locally using my existing JUnit test cases
And this is my new DataSource for PostgreSQL
Important: Remove the "defaultAutoCommit" attribute and add "defaultCatalog" attribute
This is my new Dialect

And this is the second Important detail:
Add double quotes to your table name!, just like this
My original code used to be
@Table(name = "PAYMENTS")


This is my new Payments DB :)

Hibernate did't again!

No comments:

Post a Comment