use of org.hibernate.type.StringType in project hibernate-orm by hibernate.
the class FumTest method testCompositeIDQuery.
@Test
public void testCompositeIDQuery() throws Exception {
Session s = openSession();
s.beginTransaction();
Fum fee = new Fum(fumKey("fee", true));
fee.setFum("fee");
s.save(fee);
Fum fi = new Fum(fumKey("fi", true));
fi.setFum("fi");
short fiShort = fi.getId().getShort();
s.save(fi);
Fum fo = new Fum(fumKey("fo", true));
fo.setFum("fo");
s.save(fo);
Fum fum = new Fum(fumKey("fum", true));
fum.setFum("fum");
s.save(fum);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
// Try to find the Fum object "fo" that we inserted searching by the string in the id
List vList = s.createQuery("from Fum fum where fum.id.string='fo'").list();
assertTrue("find by composite key query (find fo object)", vList.size() == 1);
fum = (Fum) vList.get(0);
assertTrue("find by composite key query (check fo object)", fum.getId().getString().equals("fo"));
// Try to find the Fum object "fi" that we inserted
vList = s.createQuery("from Fum fum where fum.id.short = ?").setParameter(0, new Short(fiShort), StandardBasicTypes.SHORT).list();
assertEquals("find by composite key query (find fi object)", 1, vList.size());
fi = (Fum) vList.get(0);
assertEquals("find by composite key query (check fi object)", "fi", fi.getId().getString());
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
assertTrue(s.createQuery("select fum.id.short, fum.id.string from Fum fum").iterate().hasNext());
assertTrue(s.createQuery("select fum.id from Fum fum").iterate().hasNext());
Query qu = s.createQuery("select fum.fum, fum , fum.fum from Fum fum");
Type[] types = qu.getReturnTypes();
assertTrue(types.length == 3);
for (int k = 0; k < types.length; k++) {
assertTrue(types[k] != null);
}
assertTrue(types[0] instanceof StringType);
assertTrue(types[1] instanceof EntityType);
assertTrue(types[2] instanceof StringType);
Iterator iter = qu.iterate();
int j = 0;
while (iter.hasNext()) {
j++;
assertTrue(((Object[]) iter.next())[1] instanceof Fum);
}
assertTrue("iterate on composite key", j == 8);
fum = (Fum) s.load(Fum.class, fum.getId());
s.createFilter(fum.getQuxArray(), "where this.foo is null").list();
s.createFilter(fum.getQuxArray(), "where this.foo.id = ?").setParameter(0, "fooid", StandardBasicTypes.STRING).list();
Query f = s.createFilter(fum.getQuxArray(), "where this.foo.id = :fooId");
f.setString("fooId", "abc");
assertFalse(f.iterate().hasNext());
iter = s.createQuery("from Fum fum where not fum.fum='FRIEND'").iterate();
int i = 0;
while (iter.hasNext()) {
fum = (Fum) iter.next();
s.delete(fum);
i++;
}
assertTrue("iterate on composite key", i == 4);
s.flush();
s.createQuery("from Fum fu, Fum fo where fu.fo.id.string = fo.id.string and fo.fum is not null").iterate();
s.createQuery("from Fumm f1 inner join f1.fum f2").list();
s.getTransaction().commit();
s.close();
}
use of org.hibernate.type.StringType in project openmrs-core by openmrs.
the class HibernatePersonDAO method getRelationshipTypes.
/**
* @see org.openmrs.api.PersonService#getRelationshipTypes(java.lang.String, java.lang.Boolean)
* @see org.openmrs.api.db.PersonDAO#getRelationshipTypes(java.lang.String, java.lang.Boolean)
*/
@Override
@SuppressWarnings("unchecked")
public List<RelationshipType> getRelationshipTypes(String relationshipTypeName, Boolean preferred) throws DAOException {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(RelationshipType.class);
criteria.add(Restrictions.sqlRestriction("CONCAT(a_Is_To_B, CONCAT('/', b_Is_To_A)) like (?)", relationshipTypeName, new StringType()));
if (preferred != null) {
criteria.add(Restrictions.eq("preferred", preferred));
}
return criteria.list();
}
use of org.hibernate.type.StringType in project BroadleafCommerce by BroadleafCommerce.
the class MapStructurePersistenceModule method procureSandBoxMapValue.
protected Serializable procureSandBoxMapValue(MapStructure mapStructure, Entity entity) {
try {
Serializable valueInstance = null;
// this is probably a sync from another sandbox where they've updated a map item for which we've updated the key in our own sandbox
// (i.e. the map entry key was changed by us in our sandbox, so our map does not have the requested key)
Class<?> valueClass = Class.forName(mapStructure.getValueClassName());
Map<String, Object> idMetadata = getPersistenceManager().getDynamicEntityDao().getIdMetadata(valueClass);
String idProperty = (String) idMetadata.get("name");
Property prop = entity.findProperty(idProperty);
if (prop != null) {
Serializable identifier;
if (!(((Type) idMetadata.get("type")) instanceof StringType)) {
identifier = Long.parseLong(prop.getValue());
} else {
identifier = prop.getValue();
}
valueInstance = (Serializable) getPersistenceManager().getDynamicEntityDao().find(valueClass, identifier);
BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
if (sandBoxHelper.isSandBoxable(valueInstance.getClass().getName()) && context != null && !context.isProductionSandBox()) {
if (sandBoxHelper.isPromote() && !sandBoxHelper.isReject()) {
// if this is a prod record (i.e. the destination map has deleted our record), then duplicate our value
// so it's available in this sandbox
valueInstance = getPersistenceManager().getDynamicEntityDao().merge(valueInstance);
} else {
valueInstance = null;
}
}
}
return valueInstance;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
use of org.hibernate.type.StringType in project BroadleafCommerce by BroadleafCommerce.
the class TranslationDaoImpl method getEntityId.
@Override
public String getEntityId(TranslatedEntity entityType, Object entity) {
Map<String, Object> idMetadata = getIdPropertyMetadata(entityType);
String idProperty = (String) idMetadata.get("name");
Type idType = (Type) idMetadata.get("type");
if (!(idType instanceof LongType || idType instanceof StringType)) {
throw new UnsupportedOperationException("Only ID types of String and Long are currently supported");
}
Object idValue;
try {
idValue = PropertyUtils.getProperty(entity, idProperty);
} catch (Exception e) {
throw new RuntimeException("Error reading id property", e);
}
if (idType instanceof StringType) {
return (String) idValue;
} else if (idType instanceof LongType) {
return getUpdatedEntityId(entityType, (Long) idValue);
}
throw new IllegalArgumentException(String.format("Could not retrieve value for id property. Object: [%s], " + "ID Property: [%s], ID Type: [%s]", entity, idProperty, idType));
}
use of org.hibernate.type.StringType in project opennms by OpenNMS.
the class DefaultNodeListService method addCriteriaForSiteStatusView.
private void addCriteriaForSiteStatusView(OnmsCriteria criteria, String statusViewName, String statusSite, String rowLabel) {
View view = m_siteStatusViewConfigDao.getView(statusViewName);
RowDef rowDef = getRowDef(view, rowLabel);
Set<String> categoryNames = getCategoryNamesForRowDef(rowDef);
addCriteriaForCategories(criteria, categoryNames.toArray(new String[categoryNames.size()]));
String sql = "{alias}.nodeId in (select nodeId from assets where " + view.getColumnName() + " = ?)";
criteria.add(Restrictions.sqlRestriction(sql, statusSite, new StringType()));
}
Aggregations