I got a class:
class Person{
String name;
String surname;
String miasto;
}
And I would like to put object of this class into a single cell of database table. Is there any way to do this? How?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As a alternatives to using
hstoreas mu is too short explained, you havehstore, composite types, Java-serializedbytea, andxml.xmldata typeJSONvalue using thejsondata type in Pg 9.2 beta and it’s 9.1-backported versionhstoreI think hstore is probably a better choice, but this is a viable alternative and can be better for some uses. It’s hard to recommend one over the other since you haven’t explained what you’re trying to do and why.
An explanation of each of the options listed above follows.
Composite type
Composite types are structured and strongly typed, easy to query, quite expressive, fast, and preserve the full capabilities of the type system so you can have all sorts of complex values, arrays, etc in them. However, they’re a real pain to query and write in text form from application code, so you’ll often land up sending extra queries to work with them.
Don’t even think about using them from JPA and other ORMs like Hibernate – you’d think they’d support composite types with their
@Embeddedclasses, but you’d be wrong.It’s a truly painful experience to try to add to or alter a composite type once it’s in use. Keep that in mind.
Access with:
or, to avoid having to parse composite-type row syntax:
or as a separate SELECT:
XML
The XML datatype implements the SQL/XML standard. It’s rich and portable. SQL/XML offers
xpathsupport for querying XML values, you can write some quite complex and powerful queries on structured XML data. XML is easy-ish to work with in most applications, and in Java can be easily marshalled and unmarshalled from native Java objects using JAXB and the JAXB annotations.You don’t need any extensions to Java or Pg to use XML.
XML values are structured, but the type isn’t, it accepts any well-formed free-form XML document or fragment. Add an
IS DOCUMENTconstraint to disallow fragments. Enforcing stricter structure is a pain. AFAIK Pg can’t currently validate XML against XML DSDs or other schema definitions in-database, so enforcing structure requires messy and slowxpathconstraints.XML values are not typed, values are stored as strings.
JSON
JSON is a standard format that’s widely understood by many languages and applications. It’s fairly easy to work with. The JSON support in Pg is very new, and currently doesn’t have any functions or operators for manipulating and querying JSON, so it’s pretty opaque to the database right now; you can’t write queries like “find me json objects where the ‘name’ key begins with ‘a'”. Expect that to improve in 9.3.
JSON support doesn’t exist in the core Java SDK, but is available from several libraries, many of which support marshalling/unmarshalling to POJOs via JAXB binding extensions. See, eg Jackson’s JAXB support.
The JSON type accepts any well-formed JSON document. It doesn’t offer any way to enforce a particular structure. JSON values are typed, but only the limited set of types supported by JSON is accepted, everything else has to be stored as a string.
HSTORE
hstoreis powerful and gives you lots of good predicates and operators to work with when querying the data. However, the format is non-standard and can be a pain to work with in application code. If using JDBC,org/postgresql/util/HStoreConverter.javaand the PgJDBC hstore support will help a bit.The
hstoretype accepts anyhstorevalue. Structure may be enforced viaCHECKconstraints.hstorevalues are text; there are no data types in hstore.Serialization to binary in java
Probably the uliest option as the value is completely opaque to the database, but you can also serialize the value in Java and store it as
byteain the DB.Whenever the class changes you have to write custom de-serialization routines to handle the old versions of the class still in the database. No non-java code can work with the value, and you can’t query it within the database.
—
It’d really help if you could explain what you’re trying to achieve with this and why.