@Test
  public void test() throws InterruptedException {

    System.setProperty("ebean.ignoreExtraDdl", "true");

    ServerConfig config = new ServerConfig();
    config.setName("h2");
    config.loadFromProperties();
    config.setName("h2other");
    config.setH2ProductionMode(true);
    config.setDdlGenerate(false);
    config.setDdlRun(false);
    config.setDefaultServer(false);
    config.setRegister(false);

    config.addClass(UTDetail.class);
    config.addClass(OnStartupViaClass.class);

    // act - register an instance
    OnStartup onStartup = new OnStartup();
    config.addServerConfigStartup(onStartup);

    EbeanServer ebeanServer = EbeanServerFactory.create(config);

    assertThat(onStartup.calledWithConfig).isSameAs(config);
    assertThat(OnStartupViaClass.calledWithConfig).isSameAs(config);

    assertThat(ebeanServer).isNotNull();

    // test server shutdown and restart using the same ServerConfig
    ebeanServer.shutdown(true, false);

    EbeanServer restartedServer = EbeanServerFactory.create(config);
    restartedServer.shutdown(true, false);
  }
示例#2
0
  public void deleteAll() {
    Ebean.execute(
        () -> {

          // orm update use bean name and bean properties
          server.createSqlUpdate("delete from o_cached_bean_child").execute();
          server.createSqlUpdate("delete from o_cached_bean_country").execute();
          server.createSqlUpdate("delete from o_cached_bean").execute();

          server.createUpdate(OrderShipment.class, "delete from orderShipment").execute();

          server.createUpdate(OrderDetail.class, "delete from orderDetail").execute();

          server.createUpdate(Order.class, "delete from order").execute();

          server.createUpdate(Contact.class, "delete from contact").execute();

          server.createUpdate(Customer.class, "delete from Customer").execute();

          server.createUpdate(Address.class, "delete from address").execute();

          // sql update uses table and column names
          server.createSqlUpdate("delete from o_country").execute();
          server.createSqlUpdate("delete from o_product").execute();
        });
  }
示例#3
0
  public void insertCountries() {

    if (server.find(Country.class).findCount() > 0) {
      return;
    }

    server.execute(
        () -> {
          Country c = new Country();
          c.setCode("NZ");
          c.setName("New Zealand");
          server.save(c);

          Country au = new Country();
          au.setCode("AU");
          au.setName("Australia");
          server.save(au);
        });
  }
示例#4
0
  public void insertProducts() {

    if (server.find(Product.class).findCount() > 0) {
      return;
    }
    server.execute(
        () -> {
          Product p = new Product();
          p.setName("Chair");
          p.setSku("C001");
          server.save(p);

          p = new Product();
          p.setName("Desk");
          p.setSku("DSK1");
          server.save(p);

          p = new Product();
          p.setName("Computer");
          p.setSku("C002");
          server.save(p);

          p = new Product();
          p.setName("Printer");
          p.setSku("C003");
          server.save(p);
        });
  }
示例#5
0
  public static synchronized void reset() {

    if (runOnce) {
      return;
    }

    final ResetBasicData me = new ResetBasicData();

    server.execute(
        () -> {
          if (server.find(Product.class).findCount() > 0) {
            // we can't really delete this base data as
            // the test rely on the products being in there
            return;
          }
          // me.deleteAll();
          me.insertCountries();
          me.insertProducts();
          me.insertTestCustAndOrders();
        });
    runOnce = true;
  }
示例#6
0
  public SpiQuery<?> createQuery(EbeanServer server, int batchSize) {

    BeanPropertyAssocMany<?> many = getMany();

    SpiQuery<?> query = (SpiQuery<?>) server.createQuery(many.getTargetType());
    String orderBy = many.getLazyFetchOrderBy();
    if (orderBy != null) {
      query.orderBy(orderBy);
    }

    String extraWhere = many.getExtraWhere();
    if (extraWhere != null) {
      // replace special ${ta} placeholder with the base table alias
      // which is always t0 and add the extra where clause
      String ew = StringHelper.replaceString(extraWhere, "${ta}", "t0");
      query.where().raw(ew);
    }

    query.setLazyLoadForParents(many);

    List<Object> idList = getParentIdList(batchSize);
    many.addWhereParentIdIn(query, idList, loadContext.isUseDocStore());

    query.setPersistenceContext(loadContext.getPersistenceContext());

    String mode = isLazy() ? "+lazy" : "+query";
    query.setLoadDescription(mode, getDescription());

    if (isLazy()) {
      // cascade the batch size (if set) for further lazy loading
      query.setLazyLoadBatchSize(getBatchSize());
    }

    // potentially changes the joins and selected properties
    loadContext.configureQuery(query);

    if (isOnlyIds()) {
      // override to just select the Id values
      query.select(many.getTargetIdProperty());
    }

    return query;
  }
 public SqlRow findUnique() {
   return server.findUnique(this, null);
 }
 public List<SqlRow> findList() {
   return server.findList(this, null);
 }
 @Override
 public void findEachWhile(Predicate<SqlRow> consumer) {
   server.findEachWhile(this, consumer, null);
 }
示例#10
0
 @Override
 public void findEach(Consumer<SqlRow> consumer) {
   server.findEach(this, consumer, null);
 }
示例#11
0
 public void execute() {
   server.execute(this, null);
 }
  @Test
  public void test() throws SQLException {

    Properties properties = PropertyMap.defaultProperties();

    DataSourceConfig dsConfig = new DataSourceConfig();
    dsConfig.loadSettings(properties, "h2autocommit"); // "h2autocommit","pg"
    dsConfig.setAutoCommit(true);

    DataSourcePool pool = new ConnectionPool("h2autocommit", dsConfig);

    Connection connection = pool.getConnection();
    assertTrue(connection.getAutoCommit());
    connection.close();

    System.setProperty("ebean.ignoreExtraDdl", "true");

    ServerConfig config = new ServerConfig();
    config.setName("h2autocommit");
    config.loadFromProperties();
    config.setDataSource(pool);
    config.setDefaultServer(false);
    config.setRegister(false);
    config.setExplicitTransactionBeginMode(true);

    config.addClass(UTMaster.class);
    config.addClass(UTDetail.class);
    config.setDdlGenerate(true);
    config.setDdlRun(true);

    EbeanServer ebeanServer = EbeanServerFactory.create(config);

    Query<UTMaster> query = ebeanServer.find(UTMaster.class);
    List<UTMaster> details = ebeanServer.findList(query, null);
    assertEquals(0, details.size());

    UTMaster bean0 = new UTMaster("one0");
    Transaction txn0 = ebeanServer.beginTransaction();
    try {
      ebeanServer.save(bean0);
      txn0.rollback();
    } finally {
      txn0.end();
    }

    // rollback as expected
    assertEquals(0, ebeanServer.find(UTMaster.class).findCount());

    UTMaster bean1 = new UTMaster("one1");
    UTMaster bean2 = new UTMaster("two2");
    UTMaster bean3 = new UTMaster("three3");

    // use a different transaction to do final query check
    Transaction otherTxn = ebeanServer.createTransaction();
    Transaction txn = ebeanServer.beginTransaction();

    try {
      ebeanServer.save(bean1);
      ebeanServer.save(bean2);

      // not visible in other transaction
      Query<UTMaster> query2 = ebeanServer.find(UTMaster.class);
      details = ebeanServer.findList(query2, otherTxn);
      assertEquals(0, details.size());

      ebeanServer.save(bean3);

      txn.commit();

    } finally {
      txn.end();
    }

    // commit as expected
    Query<UTMaster> query3 = ebeanServer.find(UTMaster.class);
    details = ebeanServer.findList(query3, otherTxn);
    assertEquals(3, details.size());
  }