Search in sources :

Example 6 with StringType

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();
}
Also used : EntityType(org.hibernate.type.EntityType) EntityType(org.hibernate.type.EntityType) StringType(org.hibernate.type.StringType) CalendarType(org.hibernate.type.CalendarType) Type(org.hibernate.type.Type) Query(org.hibernate.Query) StringType(org.hibernate.type.StringType) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with StringType

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();
}
Also used : StringType(org.hibernate.type.StringType) Criteria(org.hibernate.Criteria)

Example 8 with StringType

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);
    }
}
Also used : Serializable(java.io.Serializable) MergedPropertyType(org.broadleafcommerce.openadmin.dto.MergedPropertyType) OperationType(org.broadleafcommerce.common.presentation.client.OperationType) PersistencePerspectiveItemType(org.broadleafcommerce.common.presentation.client.PersistencePerspectiveItemType) StringType(org.hibernate.type.StringType) Type(org.hibernate.type.Type) StringType(org.hibernate.type.StringType) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 9 with StringType

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));
}
Also used : StringType(org.hibernate.type.StringType) LongType(org.hibernate.type.LongType) ResultType(org.broadleafcommerce.common.extension.ResultType) Type(org.hibernate.type.Type) LongType(org.hibernate.type.LongType) StringType(org.hibernate.type.StringType)

Example 10 with StringType

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()));
}
Also used : StringType(org.hibernate.type.StringType) RowDef(org.opennms.netmgt.config.siteStatusViews.RowDef) View(org.opennms.netmgt.config.siteStatusViews.View)

Aggregations

StringType (org.hibernate.type.StringType)20 ClassType (org.hibernate.type.ClassType)11 IntegerType (org.hibernate.type.IntegerType)11 IType (org.jboss.tools.hibernate.runtime.spi.IType)11 Test (org.junit.jupiter.api.Test)10 Type (org.hibernate.type.Type)5 LongType (org.hibernate.type.LongType)4 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)2 PipelineId (com.thoughtworks.go.server.ui.PipelineId)2 CollectionUtil (com.thoughtworks.go.server.util.CollectionUtil)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ResultType (org.broadleafcommerce.common.extension.ResultType)2 Test (org.junit.Test)2 ModificationForPipeline (com.thoughtworks.go.server.ui.ModificationForPipeline)1 Serializable (java.io.Serializable)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1