net.backtothefront.HstoreHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-postresql Show documentation
Show all versions of hibernate-postresql Show documentation
A collection of Hibernate mappings I got from https://github.com/jamesward/spring_hibernate_hstore_demo and https://hibernate.atlassian.net/browse/HB-450
The newest version!
package net.backtothefront;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
// courtesy of: http://backtothefront.net/2011/storing-sets-keyvalue-pairs-single-db-column-hibernate-postgresql-hstore-type/
public class HstoreHelper {
private static final String K_V_SEPARATOR = "=>";
public static String toString(Map m) {
if (m.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
int n = m.size();
for (String key : m.keySet()) {
sb.append("\"" + key + "\"" + K_V_SEPARATOR + "\"" + m.get(key) + "\"");
if (n > 1) {
sb.append(", ");
n--;
}
}
return sb.toString();
}
public static Map toMap(String s) {
Map m = new HashMap();
if (! StringUtils.hasText(s)) {
return m;
}
String[] tokens = s.split(", ");
for (String token : tokens) {
String[] kv = token.split(K_V_SEPARATOR);
String k = kv[0];
k = k.trim().substring(1, k.length() - 1);
String v = kv[1];
v = v.trim().substring(1, v.length() - 1);
m.put(k, v);
}
return m;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy