Example usage for org.hibernate Session getNamedQuery

List of usage examples for org.hibernate Session getNamedQuery

Introduction

In this page you can find the example usage for org.hibernate Session getNamedQuery.

Prototype

org.hibernate.Query getNamedQuery(String queryName);

Source Link

Document

Create a Query instance for the named query.

Usage

From source file:com.googlecode.hibernate.audit.HibernateAudit.java

License:Open Source License

public static List<AuditTypeField> getModifiedAuditTypeFields(Session session,
        List<AuditTypeField> auditTypeFieldsToCheck, String targetEntityId, Long afterAuditTransactionId) {
    Query query = session.getNamedQuery(SELECT_MODIFIED_AUDIT_TYPE_FIELDS);
    query.setParameterList("auditTypeFields", auditTypeFieldsToCheck);
    query.setParameter("targetEntityId", targetEntityId);
    query.setParameter("afterAuditTransactionId", afterAuditTransactionId);

    List<AuditTypeField> result = (List<AuditTypeField>) query.list();
    return result;
}

From source file:com.googlecode.hibernate.audit.HibernateAudit.java

License:Open Source License

public static AuditTransaction getAuditTransaction(Session session, Long transactionId) {
    Query query = session.getNamedQuery(SELECT_AUDIT_TRANSACTION_BY_TRANSACTION_ID);

    query.setParameter("transactionId", transactionId);
    AuditTransaction auditTransaction = (AuditTransaction) query.uniqueResult();
    return auditTransaction;
}

From source file:com.googlecode.hibernate.audit.HibernateAudit.java

License:Open Source License

public static List<AuditTransaction> getAllAuditTransactionsAfterTransactionId(Session session,
        Long transactionId) {/*from   w w  w . j  ava 2 s .  c om*/
    Query query = session.getNamedQuery(SELECT_ALL_AUDIT_TRANSACTIONS_AFTER_TRANSACTION_ID);

    query.setParameter("transactionId", transactionId);
    List<AuditTransaction> auditTransactions = (List<AuditTransaction>) query.list();
    return auditTransactions;
}

From source file:com.googlecode.hibernate.audit.HibernateAudit.java

License:Open Source License

/**
 * Return all audit transactions that have the entity modified in any way.
 * The result is sorted in reverse, e.g. the most recent transactions first.
 * /* w ww.jav a  2s  .c  o m*/
 * @param session
 * @param auditType
 * @param externalId
 * @return
 */
public static List<AuditTransaction> getAllAuditTransactionsForEntity(Session session, AuditType auditType,
        String externalId) {
    Query query = session.getNamedQuery(SELECT_AUDIT_TRANSACTIONS_FOR_ENTITY);
    query.setParameter("auditType", auditType);
    query.setParameter("externalId", externalId);
    List<AuditTransaction> auditTransactions = (List<AuditTransaction>) query.list();
    return auditTransactions;
}

From source file:com.googlecode.hibernate.audit.HibernateAudit.java

License:Open Source License

/**
 * Return all audit events that have the entity modified in any way. The
 * result is sorted in asc order, e.g. the oldest entries first.
 * //  ww w .j a v a2s .  c  om
 * @param session
 * @param auditType
 * @param externalId
 * @return
 */
public static List<AuditEvent> getAllAuditEventsForEntity(Session session, AuditType auditType,
        String externalId) {
    Query query = session.getNamedQuery(SELECT_AUDIT_EVENTS_FOR_ENTITY);
    query.setParameter("auditType", auditType);
    query.setParameter("externalId", externalId);
    List<AuditEvent> auditEvents = (List<AuditEvent>) query.list();
    return auditEvents;
}

From source file:com.googlecode.hibernate.audit.HibernateAudit.java

License:Open Source License

/**
 * Return all audit events that have the entity modified in any way until
 * specified transactionId (The transactionId is included). The result is
 * sorted in asc order, e.g. the oldest entries first.
 * // w w w.jav  a  2s  . c  o m
 * @param session
 * @param auditType
 * @param externalId
 * @return
 */
public static List<AuditEvent> getAllAuditEventsForEntityUntilTransactionId(Session session,
        AuditType auditType, String externalId, Long transactionId) {
    Query query = session.getNamedQuery(SELECT_AUDIT_EVENTS_FOR_ENTITY_UNTIL_TRANSACTION_ID);
    query.setParameter("auditType", auditType);
    query.setParameter("externalId", externalId);
    query.setParameter("transactionId", transactionId);
    List<AuditEvent> auditEvents = (List<AuditEvent>) query.list();
    return auditEvents;
}

From source file:com.googlecode.hibernate.audit.HibernateAudit.java

License:Open Source License

public static AuditLogicalGroup getAuditLogicalGroup(Session session, AuditType auditType, String externalId) {
    Query query = session.getNamedQuery(SELECT_AUDIT_LOCAL_GROUP_BY_AUDIT_TYPE_AND_EXTERNAL_ID);

    query.setParameter("auditType", auditType);
    query.setParameter("externalId", externalId);

    //query.setCacheable(true);
    //query.setCacheRegion(AUDIT_LOGICAL_GROUP_QUERY_CACHE_REGION);

    AuditLogicalGroup storedAuditLogicalGroup = (AuditLogicalGroup) query.uniqueResult();
    return storedAuditLogicalGroup;
}

From source file:com.googlecode.hibernate.audit.HibernateAudit.java

License:Open Source License

public static AuditType getAuditType(Session session, String className) {
    AuditType result = AUDIT_TYPE_CACHE.get(className);
    if (result != null) {
        return result;
    } else {/*from   www.j a  va 2  s  .c o  m*/
        Query query = session.getNamedQuery(SELECT_AUDIT_TYPE_BY_CLASS_NAME);
        query.setParameter("className", className);

        //query.setCacheable(true);
        //query.setCacheRegion(AUDIT_META_DATA_QUERY_CACHE_REGION);

        result = (AuditType) query.uniqueResult();

        if (result != null) {
            // eager load all collections because of the internal cache
            for (AuditTypeField f : result.getAuditFields()) {
                if (!Hibernate.isInitialized(f.getAuditTypeFieldAttributes())) {
                    Hibernate.initialize(f.getAuditTypeFieldAttributes());
                }
                AUDIT_TYPE_FIELD_CACHE.put(className + ":" + f.getName(), f);
            }
            if (!Hibernate.isInitialized(result.getAuditTypeAttributes())) {
                Hibernate.initialize(result.getAuditTypeAttributes());
            }
            AUDIT_TYPE_CACHE.put(className, result);
        }
        return result;
    }
}

From source file:com.googlecode.hibernate.audit.HibernateAudit.java

License:Open Source License

public static AuditTypeField getAuditField(Session session, String className, String propertyName) {
    AuditTypeField result = AUDIT_TYPE_FIELD_CACHE.get(className + ":" + propertyName);
    if (result != null) {
        return result;
    } else {/*from  w  ww  .  j  a v a  2 s . c  o  m*/
        Query query = session.getNamedQuery(SELECT_AUDIT_TYPE_FIELD_BY_CLASS_NAME_AND_PROPERTY_NAME);

        query.setParameter("className", className);
        query.setParameter("name", propertyName);

        //query.setCacheable(true);
        //query.setCacheRegion(AUDIT_META_DATA_QUERY_CACHE_REGION);

        result = (AuditTypeField) query.uniqueResult();
        if (result != null) {
            // eager load all collections because of the internal cache
            if (!Hibernate.isInitialized(result.getAuditTypeFieldAttributes())) {
                Hibernate.initialize(result.getAuditTypeFieldAttributes());
            }
            AUDIT_TYPE_FIELD_CACHE.put(className + ":" + propertyName, result);
        }
        return result;
    }
}

From source file:com.hibernate.app.Program.java

/**
 * @param args the command line arguments
 *///w  ww . ja  v a  2 s .co  m
public static void main(String[] args) {
    Configuration configuration = new Configuration().configure("resources/hibernate.cfg.xml");
    StandardServiceRegistryBuilder registry = new StandardServiceRegistryBuilder();
    registry.applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = registry.build();
    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    Session session = sessionFactory.openSession();
    //Transaction trans=session.beginTransaction();
    //session.save(new Discount(0,"50% OFF",50,null,true));
    //trans.commit();
    List<Discount> discounts = session.getNamedQuery("Discount.findAll").list();
    discounts.forEach(d -> {
        System.out.println(d.getDiscountTitle());
    });
    session.close();
    sessionFactory.close();

    System.out.println("Finish");
    System.exit(0);
}