Is there a way to query a PostgreSQL hstore with Hibernate/JPQL?
Asked Answered
D

2

7

Assuming I have a Hibernate/JPA Entity like the following:

@Entity
public class FooEntity {

  ...

  @Type(type = "hstore")
  HashMap<String, String> tags;
}

... and the hstore Type is a simple UserType implementation from this resource.

Is there a way to access the hstore in a JPQL query similar to this Pseudocode:

SELECT f FROM FooEntity f WHERE f.tags CONTAINS KEY(:key)
Doings answered 1/7, 2014 at 18:36 Comment(0)
B
1

Hibernate offers a common query abstraction across many DBs so a non-SQL syntax is hard to be abstracted out.

I would go for a native query to fetch the ids and use those to get Hibernate entities if you really need those anyway.

If you are only interested in projections than a native query is your best choice.

Bendicta answered 1/7, 2014 at 20:51 Comment(0)
T
2

You can also simply create a Hibernate org.hibernate.usertype.UserType. You extend that class; an example from our own implementation:

public class HstoreUserType implements UserType {

/**
 * PostgreSQL {@code hstore} field separator token.
 */
private static final String HSTORE_SEPARATOR_TOKEN = "=>";

/**
 * {@link Pattern} used to find and split {@code hstore} entries.
 */
private static final Pattern HSTORE_ENTRY_PATTERN = Pattern.compile(String.format("\"(.*)\"%s\"(.*)\"", HSTORE_SEPARATOR_TOKEN));

/**
 * The PostgreSQL value for the {@code hstore} data type.
 */
public static final int HSTORE_TYPE = 1111;

@Override
public int[] sqlTypes() {
    return new int[] { HSTORE_TYPE };
}

@SuppressWarnings("rawtypes")
@Override
public Class returnedClass() {
    return Map.class;
}

@Override
public boolean equals(final Object x, final Object y) throws HibernateException {
    return x.equals(y);
}

@Override
public int hashCode(final Object x) throws HibernateException {
    return x.hashCode();
}

@Override
public Object nullSafeGet(final ResultSet rs, final String[] names,
        final SessionImplementor session, final Object owner)
        throws HibernateException, SQLException {
    return convertToEntityAttribute(rs.getString(names[0]));
}

@SuppressWarnings("unchecked")
@Override
public void nullSafeSet(final PreparedStatement st, final Object value, final int index,
        final SessionImplementor session) throws HibernateException, SQLException {
    st.setObject(index, convertToDatabaseColumn((Map<String,Object>)value), HSTORE_TYPE);

}

@SuppressWarnings("unchecked")
@Override
public Object deepCopy(final Object value) throws HibernateException {
    return new HashMap<String,Object>(((Map<String,Object>)value));
}

@Override
public boolean isMutable() {
    return true;
}

@Override
public Serializable disassemble(final Object value) throws HibernateException {
    return (Serializable) value;
}

@Override
public Object assemble(final Serializable cached, final Object owner)
        throws HibernateException {
    return cached;
}

@Override
public Object replace(final Object original, final Object target, final Object owner)
        throws HibernateException {
    return original;
}


private String convertToDatabaseColumn(final Map<String, Object> attribute) {
    final StringBuilder builder = new StringBuilder();
    for (final Map.Entry<String, Object> entry : attribute.entrySet()) {
        if(builder.length() > 1) {
            builder.append(", ");
        }
        builder.append("\"");
        builder.append(entry.getKey());
        builder.append("\"");
        builder.append(HSTORE_SEPARATOR_TOKEN);
        builder.append("\"");
        builder.append(entry.getValue().toString());
        builder.append("\"");
    }
    return builder.toString();
}

private Map<String, Object> convertToEntityAttribute(final String dbData) {
    final Map<String, Object> data = new HashMap<String, Object>();
    if (dbData != null) {
        final StringTokenizer tokenizer = new StringTokenizer(dbData, ",");
        while(tokenizer.hasMoreTokens()) {
            final Matcher matcher = HSTORE_ENTRY_PATTERN.matcher(tokenizer.nextToken().trim());
            if(matcher.find()) {
                data.put(matcher.group(1), matcher.group(2));
            }
        }
    }
    return data;
}

}

Now you may use it within in Entity bean like so:

@Entity
@Table(name="YourEntityBeanTable")
@TypeDefs({
    @TypeDef(name = "hstore",  typeClass = HstoreUserType.class)
})

public class YourEntityBean {

.....

    @Type(type = "hstore")    
    @Column(name= "an_hstore_column", columnDefinition = "hstore")
    private Map<String, String> anHStoreColumn = new HashMap<>();



}
Thiouracil answered 29/1, 2016 at 14:53 Comment(0)
B
1

Hibernate offers a common query abstraction across many DBs so a non-SQL syntax is hard to be abstracted out.

I would go for a native query to fetch the ids and use those to get Hibernate entities if you really need those anyway.

If you are only interested in projections than a native query is your best choice.

Bendicta answered 1/7, 2014 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.