コード例 #1
0
  public static void createTestData(EntityManager em) {
    em.getTransaction().begin();

    // Create customer 1
    Customer customer1 = new Customer();
    customer1.setName("Customer1");
    customer1.setId(1);

    // Create customer 2
    Customer customer2 = new Customer();
    customer2.setName("Customer2");
    customer2.setId(2);

    // Create customer 3
    Customer customer3 = new Customer();
    customer3.setName("Customer3");
    customer3.setId(3);

    // Create 3 orders
    Order order1 = new Order();
    order1.setAddress("Address of order #1, Brazil");
    order1.setId(1);
    Order order2 = new Order();
    order2.setAddress("Address of order #2, USA");
    order2.setId(2);
    Order order3 = new Order();
    order3.setAddress("Address of order #3, Korea");
    order3.setId(3);

    // Since we are not using Cascade, we need to manaully persist orders
    em.persist(order1);
    em.persist(order2);
    em.persist(order3);

    // The first 2 orders belong to customer 1
    customer1.getOrders().add(order1);
    order1.setCustomer(customer1);
    customer1.getOrders().add(order2);
    order2.setCustomer(customer1);

    // The 3rd order belongs to customer 2
    customer2.getOrders().add(order3);
    order3.setCustomer(customer2);

    em.persist(customer1);
    em.persist(customer2);
    em.persist(customer3);

    em.getTransaction().commit();
  }
コード例 #2
0
  public void testCreateAndLoadCar() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Order order = new Order();
    order.setId(11);
    order.setName("order 11");
    database.create(order);
    database.commit();

    database.begin();
    ProductDetail detail = new ProductDetail();
    detail.setId(11);
    detail.setCategory("category 11");
    detail.setLocation("location 11");
    database.create(detail);
    database.commit();

    database.begin();
    Truck truck = new Truck();
    truck.setId(11);
    truck.setName("truck 11");
    truck.setKw(112);
    truck.setMake("Fiat");
    truck.setMaxWeight(3750);
    truck.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(11)));
    database.create(truck);
    Collection orders = new LinkedList();
    orders.add(database.load(Order.class, new Integer(11)));
    truck.setOrders(orders);
    database.commit();

    database.begin();
    Object object = database.load(Car.class, new Integer(11));
    assertNotNull(object);
    assertEquals("ctf.jdo.tc9x.Truck", object.getClass().getName());
    Truck loadedTruck = (Truck) object;
    database.commit();

    assertNotNull(loadedTruck);
    assertEquals(11, loadedTruck.getId());

    database.begin();
    database.remove(database.load(Car.class, new Integer(11)));
    database.remove(database.load(ProductDetail.class, new Integer(11)));
    database.remove(database.load(Order.class, new Integer(11)));
    database.commit();

    database.close();
  }
コード例 #3
0
  public void testCreateAndLoadComputer() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Order order = new Order();
    order.setId(12);
    order.setName("order 12");
    database.create(order);
    database.commit();

    database.begin();
    ProductDetail detail = new ProductDetail();
    detail.setId(12);
    detail.setCategory("category 12");
    detail.setLocation("location 12");
    database.create(detail);
    database.commit();

    database.begin();
    Laptop laptop = new Laptop();
    laptop.setId(12);
    laptop.setName("laptop 12");
    laptop.setCpu("centrino");
    laptop.setResolution("1600");
    laptop.setWeight(2450);
    laptop.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(12)));
    database.create(laptop);
    Collection orders = new LinkedList();
    orders.add(database.load(Order.class, new Integer(12)));
    laptop.setOrders(orders);
    database.commit();

    database.begin();
    Computer computer = (Computer) database.load(Computer.class, new Integer(12));
    database.commit();

    assertNotNull(computer);
    assertEquals("ctf.jdo.tc9x.Laptop", computer.getClass().getName());
    assertEquals(12, computer.getId());
    assertEquals("laptop 12", computer.getName());

    database.begin();
    computer = (Computer) database.load(Computer.class, new Integer(12));
    database.remove(computer);
    database.remove(database.load(ProductDetail.class, new Integer(12)));
    database.remove(database.load(Order.class, new Integer(12)));
    database.commit();

    database.close();
  }
コード例 #4
0
 /** @inheritdoc */
 @Override
 public void readEntity(Cursor cursor, Order entity, int offset) {
   entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
   entity.setType(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
   entity.setTopic(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
   entity.setStartTime(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
   entity.setEndTime(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
   entity.setPrice(cursor.isNull(offset + 5) ? null : cursor.getDouble(offset + 5));
   entity.setRealtime(cursor.isNull(offset + 6) ? null : cursor.getShort(offset + 6) != 0);
   entity.setContactName(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
   entity.setContactGender(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
   entity.setContactPhone(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
   entity.setExpertId(cursor.isNull(offset + 10) ? null : cursor.getLong(offset + 10));
   entity.setUserId(cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11));
 }
コード例 #5
0
  final void init() {
    Customer c = new Customer();
    c.setName("John");
    c.setId(123);
    customers.put(c.getId(), c);

    c = new Customer();
    c.setName("Dan");
    c.setId(113);
    customers.put(c.getId(), c);

    Order o = new Order();
    o.setDescription("order 223");
    o.setId(223);
    orders.put(o.getId(), o);
  }
コード例 #6
0
 /** @inheritdoc */
 @Override
 protected Long updateKeyAfterInsert(Order entity, long rowId) {
   entity.setId(rowId);
   return rowId;
 }