示例#1
0
  @Test
  public void testOrderedList() throws Exception {
    Session session = openSession();
    Transaction tx = session.beginTransaction();
    Child luke = new Child();
    luke.setName("Luke");
    Child leia = new Child();
    leia.setName("Leia");
    session.persist(luke);
    session.persist(leia);
    Father father = new Father();
    father.getOrderedChildren().add(luke);
    father.getOrderedChildren().add(null);
    father.getOrderedChildren().add(leia);
    session.persist(father);
    tx.commit();

    session.clear();

    tx = session.beginTransaction();
    father = (Father) session.get(Father.class, father.getId());
    assertThat(father.getOrderedChildren()).as("List should have 3 elements").hasSize(3);
    assertThat(father.getOrderedChildren().get(0).getName())
        .as("Luke should be first")
        .isEqualTo(luke.getName());
    assertThat(father.getOrderedChildren().get(1)).as("Second born should be null").isNull();
    assertThat(father.getOrderedChildren().get(2).getName())
        .as("Leia should be third")
        .isEqualTo(leia.getName());
    session.delete(father);
    session.delete(session.load(Child.class, luke.getId()));
    session.delete(session.load(Child.class, leia.getId()));
    tx.commit();

    session.close();

    checkCleanCache();
  }
  @Override
  protected void setUp() throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    InputStream schemaStream =
        Thread.currentThread().getContextClassLoader().getResourceAsStream(XSD_RESOURCE);
    Schema s = sf.newSchema(new StreamSource(schemaStream));

    marshaller = JAXBContextFactory.createContext(getClasses(), null).createMarshaller();
    marshaller.setSchema(s);

    child = new Child();
    child.setName("123456789");

    root = setupRootObject();
  }