Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java and PostgreSQL Arrays

Question is quite simple: is there any JDO/JPA/anything else "object-to-DB" mapping tools that can handle PG arrays? Multi-dimensional arrays? Mostly of strings and integers/longs.

Second one: can it handle hstore column types?

I'm trying to implement something to handle these types in DataNucleus JDO, but still no result. The most impressive and simple to implement support I've seen so far is python's SQLAlchemy.

like image 527
Igore Vitaller Avatar asked Jan 25 '12 11:01

Igore Vitaller


People also ask

Can I use PostgreSQL with Java?

The PostgreSQL JDBC Driver allows Java programs to connect to a PostgreSQL database using standard, database independent Java code.

Does PostgreSQL support array?

PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any built-in or user-defined base type, enum type, composite type, range type, or domain can be created.

What is Postgres Java?

PostgreSQL (which goes by the moniker Postgres) is famous for its object-relational nature. In contrast, other database systems are usually relational. Due to its nature, it's a great pairing with Java, which is heavily object-oriented.

How do I insert a array values into mysql database using Java?

setArray(2, aArray); int count = preparedStatement. executeUpdate(); System. out. println(count + " inserted");


1 Answers

First of, SQLAlchemy is awesome... All the stuff that others can't do out-of-the-box works like a charm in this ORM.

The solution that you might want involves Hibernate:

mapping a postgres array with hibernate might be interessting for you. Basically, all the suggestions you will see involve the writing of a UserType extension. This would also apply to Postgres Enums types (and probably hstore, but that would only be a guess). An example for such a userType could be found on the Hibernate forums. This in-depth article explains the interface fairly detailed. After writing the UserType, you only need to annotate the property and Hibernate will be able to do the mapping. In newer versions (I've used it with Hibernate 4.x) the interface has changed slightly, but nothing you can't figure out.

like image 170
DrColossos Avatar answered Oct 13 '22 16:10

DrColossos