Zagrev
Zagrev

Reputation: 2020

trying to persist a map with compound key and basic value with JPA annotations

I have an entity with a map. I am using Hibernate on PostgreSQL. The classes in the map are defined by Java and cannot be annotated by me. The class I am modifying was generated from an XML Schema I cannot change.

So I have

   import java.util.HashMap;
   import java.util.Map;
   import javax.xml.namespace.QName;

   @Entity public class TestClass {
   @XmlTransient
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long pk;

   ...

   @XmlAnyAttribute
   @ElementCollection(fetch = FetchType.EAGER)
   @Embedded
   private final Map<QName, String> otherAttributes = new HashMap<QName, String>();

   ...

I am using PostGreSQL, and I am getting the following error:

Unsuccessful: create table TestClass_otherAttributes (TestClass_pk int8 not null, count int4 not null, hash int4 not null, offset int4 not null, value varchar(255), localPart varchar(255), namespaceURI varchar(255), prefix varchar(255), primary key (TestClass_pk, localPart, namespaceURI, prefix))
ERROR: syntax error at or near "offset" Position: 160

Clearly this means that the order field in one of the strings (most likely in QName, but it's hard to be certain) is reserved and so the create table is failing.

I have researched this and have found a number of other annotations that affect how the join table it built and named, but nothing that allows me to quote the field names (again, without the ability to annotate QName or String) nor affect the column name that doesn't require annotations in the key or value classes.

So my question is, what is the minimum number of annotations to add so I can persist this map?

Upvotes: 0

Views: 293

Answers (1)

kgrittn
kgrittn

Reputation: 19471

offset is a reserved word in PostgreSQL and in the 2008 standard:

http://www.postgresql.org/docs/current/interactive/sql-keywords-appendix.html

If you have access to the SQL, or can control how it is emitted, you could put the column name in quotes (the double-quote character: ["]). A quoted identifier is always taken as an identifier and never collides with reserved words. If you can't cause the column name to be changed, nor quote it, you can't get it to work in PostgreSQL or any SQL-2008 conforming database product.

Upvotes: 1

Related Questions