Пример #1
0
  @Ignore("alias property not supported yet")
  public void testBCollection() throws Exception {
    CompassSession session = openSession();

    A a = new A();
    a.id = 1;
    a.value = "value";
    a.bs = new ArrayList<B>();
    B b = new B();
    b.value = "bvalue11";
    b.value2 = "bvalue12";
    a.bs.add(b);
    b = new B();
    b.value = "bvalue21";
    b.value = "bvalue22";
    a.bs.add(b);
    session.save(a);

    Resource resource = session.loadResource(A.class, 1);
    assertNotNull(resource);
    assertEquals(7, resource.getProperties().length);
    assertEquals("A", resource.getAlias());
    assertEquals(4, resource.getProperties("value").length);

    session.close();
  }
Пример #2
0
  @Ignore("alias property not supported yet")
  public void testBwoValues() throws Exception {
    CompassSession session = openSession();

    A a = new A();
    a.id = 1;
    a.value = "value";
    B b = new B();
    b.value = "bvalue";
    b.value2 = "bvalue2";
    a.b = b;
    session.save(a);

    Resource resource = session.loadResource(A.class, 1);
    assertNotNull(resource);
    assertEquals(6, resource.getProperties().length);
    assertEquals("A", resource.getAlias());
    assertEquals(3, resource.getProperties("value").length);

    a = (A) session.load(A.class, 1);
    assertEquals(1, a.id.longValue());
    assertNull(a.value);
    assertNull(a.b);

    session.close();
  }
  public void testPolyQuery() throws Exception {
    CompassSession session = openSession();
    CompassTransaction tr = session.beginTransaction();

    Long id = new Long(1);
    ExtendsA extendsA = new ExtendsA();
    extendsA.setId(id);
    extendsA.setValue("value");
    extendsA.setExtendsValue("evalue");

    session.save("extends", extendsA);

    extendsA = (ExtendsA) session.load("extends", id);
    assertEquals("value", extendsA.getValue());

    id = new Long(1);
    extendsA = new ExtendsA();
    extendsA.setId(id);
    extendsA.setValue("value");
    extendsA.setExtendsValue("evalue");

    session.save("override", extendsA);

    extendsA = (ExtendsA) session.load("override", id);
    assertEquals("value", extendsA.getValue());

    CompassHits hits = session.queryBuilder().polyAlias("base").hits();
    assertEquals(2, hits.length());

    hits = session.find("base");
    assertEquals(2, hits.length());

    tr.commit();
    session.close();
  }
  public void testErrorDuringUpdateOwneeDoesNotLeakMemory() throws Exception {
    Session hibSession = sessionFactory.openSession();
    Transaction hibTr = hibSession.beginTransaction();

    CompassSession session = compass.openSession();
    // (AGR_OSEM) ... CompassTransaction tr = session.beginTransaction();

    Roof r1 = new Roof();
    r1.setName("the roof");

    House h1 = new House();
    h1.setName("my house");
    h1.setRoof(r1);
    r1.setHouse(h1);

    hibSession.save(h1);

    // (AGR_OSEM) ... tr.commit();
    session.close();

    hibTr.commit();
    hibSession.close();

    hibSession = sessionFactory.openSession();
    hibTr = hibSession.beginTransaction();

    session = compass.openSession();
    // (AGR_OSEM) ... tr = session.beginTransaction();

    //        h1 = (House) hibSession.get(House.class, h1.getId());
    Roof r2 = new Roof();
    r2.setName("the new roof");
    h1.setRoof(r2);
    r2.setHouse(h1);
    r2.throwError = true;

    try {
      hibSession.update(h1);
      fail("should throw error");
    } catch (RuntimeException ex) {
      // good
      // (AGR_OSEM) ... tr.rollback();
      session.close();

      hibTr.rollback();
      hibSession.close();
    }

    Map pendingCreate = (Map) getProperty(hibernateEventListener, "pendingCreate");
    assertTrue(pendingCreate.isEmpty());
    Map pendingSave = (Map) getProperty(hibernateEventListener, "pendingSave");
    assertTrue(pendingSave.isEmpty());
  }
Пример #5
0
  public Pager search(Pager pager) {
    Compass compass = compassTemplate.getCompass();
    CompassSession compassSession = compass.openSession();
    CompassQueryBuilder compassQueryBuilder = compassSession.queryBuilder();
    CompassBooleanQueryBuilder compassBooleanQueryBuilder = compassQueryBuilder.bool();

    CompassQuery compassQuery =
        compassBooleanQueryBuilder
            .addMust(compassQueryBuilder.term("isMarketable", true))
            .addMust(compassQueryBuilder.queryString("name:*" + pager.getKeyword() + "*").toQuery())
            .toQuery();

    if (StringUtils.isEmpty(pager.getOrderBy()) || pager.getOrderType() == null) {
      compassQuery
          .addSort("isBest", SortPropertyType.STRING, SortDirection.REVERSE)
          .addSort("isNew", SortPropertyType.STRING, SortDirection.REVERSE)
          .addSort("isHot", SortPropertyType.STRING, SortDirection.REVERSE);
    } else {
      if (pager.getOrderType() == OrderType.asc) {
        if (StringUtils.equalsIgnoreCase(pager.getOrderBy(), "price")) {
          compassQuery.addSort("price", SortPropertyType.FLOAT);
        }
      } else {
        if (StringUtils.equalsIgnoreCase(pager.getOrderBy(), "price")) {
          compassQuery.addSort("price", SortPropertyType.FLOAT, SortDirection.REVERSE);
        }
      }
    }

    CompassHits compassHits = compassQuery.hits();

    List<Product> productList = new ArrayList<Product>();

    int firstResult = (pager.getPageNumber() - 1) * pager.getPageSize();
    int maxReasults = pager.getPageSize();
    int totalCount = compassHits.length();

    int end = Math.min(totalCount, firstResult + maxReasults);
    for (int i = firstResult; i < end; i++) {
      Product product = (Product) compassHits.data(i);
      productList.add(product);
    }
    compassSession.close();
    pager.setList(productList);
    pager.setTotalCount(totalCount);
    return pager;
  }
  public void testSimpleExtends() throws Exception {
    CompassSession session = openSession();
    CompassTransaction tr = session.beginTransaction();

    Long id = new Long(1);
    ExtendsA extendsA = new ExtendsA();
    extendsA.setId(id);
    extendsA.setValue("value");
    extendsA.setExtendsValue("evalue");

    session.save("extends", extendsA);

    extendsA = (ExtendsA) session.load("extends", id);
    assertEquals("value", extendsA.getValue());

    tr.commit();
    session.close();
  }
  public void testSave() throws Exception {
    CompassSession session = compass.openSession();
    // (AGR_OSEM) ... CompassTransaction tr = session.beginTransaction();

    Session hibSession = sessionFactory.openSession();
    Transaction hibTr = hibSession.beginTransaction();

    Roof r1 = new Roof();
    r1.setName("the roof");

    Foundations f1 = new Foundations();
    f1.setName("foundations");

    House h1 = new House();
    h1.setName("my house");
    h1.setRoof(r1);
    r1.setHouse(h1);
    h1.setFoundations(f1);

    // (AGR_OSEM) ... assertEquals(0,
    // session.queryBuilder().matchAll().setTypes(Roof.class).hits().length());
    // (AGR_OSEM) ... assertEquals(0,
    // session.queryBuilder().matchAll().setTypes(House.class).hits().length());

    hibSession.save(h1);

    // objects are present in index after save, before commit

    // (AGR_OSEM) ... assertEquals(1,
    // session.queryBuilder().matchAll().setTypes(Roof.class).hits().length());
    // (AGR_OSEM) ... assertEquals(1,
    // session.queryBuilder().matchAll().setTypes(House.class).hits().length());

    // (AGR_OSEM) ... tr.commit();
    session.close();

    hibTr.commit();
    hibSession.close();
  }
  public void testSimpleExtendsWithNoOverride() throws Exception {
    CompassSession session = openSession();
    CompassTransaction tr = session.beginTransaction();

    Long id = new Long(1);
    ExtendsA extendsA = new ExtendsA();
    extendsA.setId(id);
    extendsA.setValue("value");
    extendsA.setExtendsValue("evalue");

    session.save("override1", extendsA);

    extendsA = (ExtendsA) session.load("override1", id);
    assertEquals("value", extendsA.getValue());

    Resource r = session.loadResource("override1", id);
    assertNotNull(r.getProperty("mvalue"));
    assertNotNull(r.getProperty("mvalue1"));

    tr.commit();
    session.close();
  }