Package org.hibernate.criterion

Examples of org.hibernate.criterion.DetachedCriteria


  public static final String USER_EMAIL = "userEmail";
  public static final String USER_BIRTHDATE = "userBirthdate";
  public static final String DISABLE_FLAG = "disableFlag";
 
  public AppUser findByUserLoginName(String userLoginName) {
    DetachedCriteria criteria = DetachedCriteria.forClass(AppUser.class);
    criteria.add(Restrictions.eq(DELETE_FLAG, false));
    List<AppUser> users = super.findByCriteria(criteria);
    if (CollectionUtils.isEmpty(users)) {
      return users.get(0);
    } else {
      return null;
View Full Code Here


      return null;
    }
  }

  public void findPage(PaginationSupport<AppUser> pagination, String search) {
    DetachedCriteria criteria = DetachedCriteria.forClass(AppUser.class);
    criteria.add(Restrictions.eq(DELETE_FLAG, false));
    if (StringUtils.isNotBlank(search)) {
      criteria.add(Restrictions.or(
          Restrictions.ilike(USER_LOGIN_NAME, search, MatchMode.ANYWHERE),
          Restrictions.or(Restrictions.ilike(USER_NICK_NAME, search, MatchMode.ANYWHERE),
              Restrictions.ilike(USER_EMAIL, search, MatchMode.ANYWHERE))));
    }
    criteria.addOrder(Order.desc(CREATE_TIME));
    super.findPageByCriteria(pagination, criteria);
  }
View Full Code Here

                 (((OrOperation) criterion).getOperand1()),
                 convertFilterCriterion
                 (((OrOperation) criterion).getOperand2()));
        }
        if (criterion instanceof ContextVariableEquality) {
            DetachedCriteria sub
                = DetachedCriteria.forClass(ProcessDataStore.DAO.class);
            sub.setProjection(Projections.property("mapId"));
            Criterion valueCond = null;
            if (((ContextVariableEquality) criterion).getValue() == null) {
                valueCond = Expression.and
                    (Expression.isNull("svalue"), Expression.isNull("bvalue"));
            } else {
                valueCond = Expression.eq
                    ("svalue", ((ContextVariableEquality)criterion).getValue());
            }
            sub.add
                (Expression.and
                    (Expression.eq("item", ((ContextVariableEquality)criterion)
                                   .getContextVariable()),
                     valueCond));
            return Property.forName("dbId").in(sub);
View Full Code Here

    enrolment2.setStudent(gavin);
    enrolment2.setStudentNumber(gavin.getStudentNumber());
    gavin.getEnrolments().add(enrolment2);
    session.persist(enrolment2);
   
    DetachedCriteria dc = DetachedCriteria.forClass(Student.class)
      .add( Property.forName("studentNumber").eq( new Long(232) ) )
      .setProjection( Property.forName("name") );

    session.createCriteria(Student.class)
      .add( Subqueries.propertyEqAll("name", dc) )
      .list();
   
    session.createCriteria(Student.class)
      .add( Subqueries.exists(dc) )
      .list();
 
    session.createCriteria(Student.class)
      .add( Property.forName("name").eqAll(dc) )
      .list();
 
    session.createCriteria(Student.class)
      .add( Subqueries.in("Gavin King", dc) )
      .list();
   
    DetachedCriteria dc2 = DetachedCriteria.forClass(Student.class, "st")
      .add( Property.forName("st.studentNumber").eqProperty("e.studentNumber") )
      .setProjection( Property.forName("name") );
   
    session.createCriteria(Enrolment.class, "e")
      .add( Subqueries.eq("Gavin King", dc2) )
      .list();

    DetachedCriteria dc3 = DetachedCriteria.forClass(Student.class, "st")
      .createCriteria("enrolments")
        .createCriteria("course")
          .add( Property.forName("description").eq("Hibernate Training") )
          .setProjection( Property.forName("st.name") );
 
    session.createCriteria(Enrolment.class, "e")
      .add( Subqueries.eq("Gavin King", dc3) )
      .list();

    DetachedCriteria dc4 = DetachedCriteria.forClass(Student.class, "st")
      .setProjection( Property.forName("name").as( "stname" ) );

    dc4.getExecutableCriteria( session ).list();

    dc4.getExecutableCriteria( session ).addOrder( Order.asc( "stname" ) ).list();

    session.createCriteria(Enrolment.class, "e")
      .add( Subqueries.eq("Gavin King", dc4) )
      .list();
View Full Code Here

    enrolment2.setStudent(gavin);
    enrolment2.setStudentNumber(gavin.getStudentNumber());
    gavin.getEnrolments().add(enrolment2);
    session.persist(enrolment2);

    DetachedCriteria dc = DetachedCriteria.forClass(Student.class)
      .add( Property.forName("cityState").eq( odessaWa ) )
      .setProjection( Property.forName("cityState") );

    session.createCriteria(Student.class)
      .add( Subqueries.exists(dc) )
      .list();
    t.commit();
    session.close();

    session = openSession();
    t = session.beginTransaction();
    try {
      session.createCriteria(Student.class)
        .add( Subqueries.propertyEqAll("cityState", dc) )
        .list();
      fail( "should have failed because cannot compare subquery results with multiple columns" );
    }
    catch ( QueryException ex ) {
      // expected
    }
    finally {
      t.rollback();
      session.close();
    }

    session = openSession();
    t = session.beginTransaction();
    try {
      session.createCriteria(Student.class)
        .add( Property.forName("cityState").eqAll(dc) )
        .list();
      fail( "should have failed because cannot compare subquery results with multiple columns" );
    }
    catch ( QueryException ex ) {
      // expected
    }
    finally {
      t.rollback();
      session.close();
    }

    session = openSession();
    t = session.beginTransaction();
    try {
      session.createCriteria(Student.class)
        .add( Subqueries.in( odessaWa, dc) )
        .list();
      fail( "should have failed because cannot compare subquery results with multiple columns" );
    }
    catch ( JDBCException ex ) {
      // expected
    }
    finally {
      t.rollback();
      session.close();
    }

    session = openSession();
    t = session.beginTransaction();
    DetachedCriteria dc2 = DetachedCriteria.forClass(Student.class, "st1")
      .add( Property.forName("st1.cityState").eqProperty("st2.cityState") )
      .setProjection( Property.forName("cityState") );
    try {
      session.createCriteria(Student.class, "st2")
        .add( Subqueries.eq( odessaWa, dc2) )
        .list();
      fail( "should have failed because cannot compare subquery results with multiple columns" );
    }
    catch ( JDBCException ex ) {
      // expected
    }
    finally {
      t.rollback();
      session.close();
    }

    session = openSession();
    t = session.beginTransaction();
    DetachedCriteria dc3 = DetachedCriteria.forClass(Student.class, "st")
      .createCriteria("enrolments")
        .createCriteria("course")
          .add( Property.forName("description").eq("Hibernate Training") )
          .setProjection( Property.forName("st.cityState") );
    try {
View Full Code Here

  }

  public void testDetachedCriteria() {
   
    DetachedCriteria dc = DetachedCriteria.forClass(Student.class)
      .add( Property.forName("name").eq("Gavin King") )
      .addOrder( Order.asc("studentNumber") )
      .setProjection( Property.forName("studentNumber") );
   
    byte[] bytes = SerializationHelper.serialize(dc);
   
    dc = (DetachedCriteria) SerializationHelper.deserialize(bytes);
   
    Session session = openSession();
    Transaction t = session.beginTransaction();

    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    Student bizarroGavin = new Student();
    bizarroGavin.setName("Gavin King");
    bizarroGavin.setStudentNumber(666);
    session.persist(bizarroGavin);
    session.persist(gavin);
   
    List result = dc.getExecutableCriteria(session)
      .setMaxResults(3)
      .list();
   
    assertEquals( result.size(), 2 );
    assertEquals( result.get(0), new Long(232) );
View Full Code Here

    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    DetachedCriteria dc = DetachedCriteria.forClass(Student.class)
      .add( Property.forName("studentNumber").eq( new Long(232) ) )
      .setProjection( Property.forName("name") );
    gavin = ( Student ) s.createCriteria(Student.class)
      .add( Subqueries.exists(dc) )
      .setReadOnly( true )
      .uniqueResult();
    assertFalse( s.isDefaultReadOnly() );
    assertTrue( s.isReadOnly( gavin ) );
    assertFalse( Hibernate.isInitialized( gavin.getPreferredCourse() ) );
    checkProxyReadOnly( s, gavin.getPreferredCourse(), true );
    assertFalse( Hibernate.isInitialized( gavin.getPreferredCourse() ) );
    Hibernate.initialize( gavin.getPreferredCourse() );
    assertTrue( Hibernate.isInitialized( gavin.getPreferredCourse() ) );
    checkProxyReadOnly( s, gavin.getPreferredCourse(), true );
    assertFalse( Hibernate.isInitialized( gavin.getEnrolments() ) );
    Hibernate.initialize( gavin.getEnrolments() );
    assertTrue( Hibernate.isInitialized( gavin.getEnrolments() ) );
    assertEquals( 1, gavin.getEnrolments().size() );
    enrolment = ( Enrolment ) gavin.getEnrolments().iterator().next();
    assertFalse( s.isReadOnly( enrolment ) );
    assertFalse( Hibernate.isInitialized( enrolment.getCourse() ) );
    checkProxyReadOnly( s, enrolment.getCourse(), false );
    Hibernate.initialize( enrolment.getCourse() );
    checkProxyReadOnly( s, enrolment.getCourse(), false );
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    DetachedCriteria dc2 = DetachedCriteria.forClass(Student.class, "st")
      .add( Property.forName("st.studentNumber").eqProperty("e.studentNumber") )
      .setProjection( Property.forName("name") );
    enrolment = ( Enrolment ) s.createCriteria(Enrolment.class, "e")
      .add( Subqueries.eq("Gavin King", dc2) )
      .setReadOnly( true )
      .uniqueResult();
    assertTrue( s.isReadOnly( enrolment ) );
    assertFalse( Hibernate.isInitialized( enrolment.getCourse() ) );
    checkProxyReadOnly( s, enrolment.getCourse(), true );
    Hibernate.initialize( enrolment.getCourse() );
    assertTrue( Hibernate.isInitialized( enrolment.getCourse() ) );
    checkProxyReadOnly( s, enrolment.getCourse(), true );
    assertFalse( Hibernate.isInitialized( enrolment.getStudent() ) );
    checkProxyReadOnly( s, enrolment.getStudent(), true );
    Hibernate.initialize( enrolment.getStudent() );
    assertTrue( Hibernate.isInitialized( enrolment.getStudent() ) );
    checkProxyReadOnly( s, enrolment.getStudent(), true );
    assertFalse( Hibernate.isInitialized( enrolment.getStudent().getPreferredCourse() ) );
    checkProxyReadOnly( s, enrolment.getStudent().getPreferredCourse(), false );
    Hibernate.initialize( enrolment.getStudent().getPreferredCourse() );
    assertTrue( Hibernate.isInitialized( enrolment.getStudent().getPreferredCourse() ) );
    checkProxyReadOnly( s, enrolment.getStudent().getPreferredCourse(), false );
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    DetachedCriteria dc3 = DetachedCriteria.forClass(Student.class, "st")
      .createCriteria("enrolments")
        .createCriteria("course")
          .add( Property.forName("description").eq("Hibernate Training") )
          .setProjection( Property.forName("st.name") );
    enrolment = ( Enrolment ) s.createCriteria(Enrolment.class, "e")
View Full Code Here

    s.close();   
  }
 
  public void testDetachedCriteria() {
   
    DetachedCriteria dc = DetachedCriteria.forClass(Student.class)
      .add( Property.forName("name").eq("Gavin King") )
      .addOrder( Order.asc("studentNumber") );
   
    byte[] bytes = SerializationHelper.serialize(dc);
   
    dc = (DetachedCriteria) SerializationHelper.deserialize(bytes);
   
    Session session = openSession();
    Transaction t = session.beginTransaction();

    Student gavin = new Student();
    gavin.setName("Gavin King");
    gavin.setStudentNumber(232);
    Student bizarroGavin = new Student();
    bizarroGavin.setName("Gavin King");
    bizarroGavin.setStudentNumber(666);
    session.persist(bizarroGavin);
    session.persist(gavin);

    t.commit();
    session.close();

    session = openSession();
    t = session.beginTransaction();

    List result = dc.getExecutableCriteria(session)
      .setMaxResults(3)
      .setReadOnly( true )
      .list();

    assertEquals( result.size(), 2 );
View Full Code Here

  public void testCriteriaControl() {
    TestData testData = new TestData();
    testData.prepare();

    // the subquery...
    DetachedCriteria subquery = DetachedCriteria.forClass( Salesperson.class )
        .setProjection( Property.forName( "name" ) );

    Session session = openSession();
    session.beginTransaction();
    session.enableFilter( "fulfilledOrders" ).setParameter( "asOfDate", testData.lastMonth.getTime() );
View Full Code Here

    Session session = openSession();
    session.enableFilter("region").setParameter("region", "APAC");

    log.info("Criteria query against Department with a subquery on Salesperson in the APAC reqion...");
    DetachedCriteria salespersonSubquery = DetachedCriteria.forClass(Salesperson.class)
        .add(Restrictions.eq("name", "steve"))
        .setProjection(Property.forName("department"));

    Criteria departmentsQuery = session.createCriteria(Department.class).add(Subqueries.propertyIn("id", salespersonSubquery));
    List departments = departmentsQuery.list();

    assertEquals("Incorrect department count", 1, departments.size());

    log.info("Criteria query against Department with a subquery on Salesperson in the FooBar reqion...");

    session.enableFilter("region").setParameter("region", "Foobar");
    departments = departmentsQuery.list();

    assertEquals("Incorrect department count", 0, departments.size());

    log.info("Criteria query against Order with a subquery for line items with a subquery on product and sold by a given sales person...");
    session.enableFilter("region").setParameter("region", "APAC");

    DetachedCriteria lineItemSubquery = DetachedCriteria.forClass(LineItem.class)
        .add( Restrictions.ge( "quantity", 1L ) )
        .createCriteria( "product" )
        .add( Restrictions.eq( "name", "Acme Hair Gel" ) )
        .setProjection( Property.forName( "id" ) );

    List orders = session.createCriteria(Order.class)
        .add(Subqueries.exists(lineItemSubquery))
        .add(Restrictions.eq("buyer", "gavin"))
        .list();

    assertEquals("Incorrect orders count", 1, orders.size());

    log.info("query against Order with a subquery for line items with a subquery line items where the product name is Acme Hair Gel and the quantity is greater than 1 in a given region and the product is effective as of last month");
    session.enableFilter("region").setParameter("region", "APAC");
    session.enableFilter("effectiveDate").setParameter("asOfDate", testData.lastMonth.getTime());

    DetachedCriteria productSubquery = DetachedCriteria.forClass(Product.class)
        .add(Restrictions.eq("name", "Acme Hair Gel"))
        .setProjection(Property.forName("id"));

    lineItemSubquery = DetachedCriteria.forClass(LineItem.class)
        .add(Restrictions.ge("quantity", 1L ))
View Full Code Here

TOP

Related Classes of org.hibernate.criterion.DetachedCriteria

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.