Transaction tx = null;
Criteria crit;
Query q;
EnhancedOQLQuery oql;
Iterator it;
Article example;
//perform transaction
try
{
tx = _kit.getTransaction(_conn);
tx.begin();
example = (Article) _conn.getObjectByIdentity(
new Identity(Article.class, Article.class,
new Object[] {new Integer(77777)}));
if (example == null)
{
example = Article.createInstance();
example.setArticleId(new Integer(77777));
}
example.setProductGroupId(new Integer(7));
example.setStock(333);
example.setArticleName("333");
_conn.makePersistent(example);
tx.commit();
Identity oid = _conn.getIdentity(example);
// get from the cache
tx = _kit.getTransaction(_conn);
tx.begin();
example = (Article) _conn.getObjectByIdentity(oid);
assertEquals("should be equal", 7, example.getProductGroupId().intValue());
assertEquals("should be equal", 333, example.getStock());
assertEquals("should be equal", "333", example.getArticleName());
tx.commit();
// get from the database
tx = _kit.getTransaction(_conn);
tx.begin();
_conn.invalidate(oid);
example = (Article) _conn.getObjectByIdentity(oid);
assertEquals("should be equal", 7, example.getProductGroupId().intValue());
assertEquals("should be equal", "333", example.getArticleName());
example.setArticleName("334"); // test update
tx.commit();
// get from the database via Query
tx = _kit.getTransaction(_conn);
_conn.invalidate(oid);
tx.begin();
crit = new Criteria();
crit.addEqualTo("articleId", new Integer(77777));
crit.addEqualTo("articleName", "334");
q = QueryFactory.newQuery(Article.class, crit);
it = _conn.getIteratorByQuery(q);
if (it.hasNext())
{
InterfaceArticle article = (InterfaceArticle) it.next();
assertEquals("should be equal", 77777, article.getArticleId().intValue());
assertEquals("should be equal", "334", article.getArticleName());
article.setArticleName("335"); // test update
if (it.hasNext())
{
fail("Query returned more than 1 object");
}
}
else
{
fail("Query returned empty result set");
}
tx.commit();
// get from the database via OQLQuery Iterator
tx = _kit.getTransaction(_conn);
_conn.invalidate(oid);
tx.begin();
oql = _conn.newOQLQuery();
oql.create("select a from " + Article.class.getName()
+ " where articleId=$1 and articleName=$2");
oql.bind(new Integer(77777));
oql.bind("335");
it = _conn.getIteratorByOQLQuery(oql);
if (it.hasNext())
{
InterfaceArticle article = (InterfaceArticle) it.next();
assertEquals("should be equal", 77777, article.getArticleId().intValue());
assertEquals("should be equal", "335", article.getArticleName());
article.setArticleName("336"); // test update
if (it.hasNext())
{
fail("Query returned more than 1 object");
}
}
else
{
fail("Query returned empty result set");
}
tx.commit();
// get from the database via OQLQuery Collection
tx = _kit.getTransaction(_conn);
_conn.invalidate(oid);
tx.begin();
oql.bind(new Integer(77777));
oql.bind("336");
it = ((Collection) oql.execute()).iterator();
if (it.hasNext())
{
InterfaceArticle article = (InterfaceArticle) it.next();
assertEquals("should be equal", 77777, article.getArticleId().intValue());
assertEquals("should be equal", "336", article.getArticleName());
article.setArticleName("337"); // test update
if (it.hasNext())
{
fail("Query returned more than 1 object");
}
}
else
{
fail("Query returned empty result set");
}
tx.commit();
// get from the database
tx = _kit.getTransaction(_conn);
tx.begin();
_conn.invalidate(oid);
example = (Article) _conn.getObjectByIdentity(oid);
assertEquals("should be equal", "337", example.getArticleName());
tx.commit();
try
{
tx = _kit.getTransaction(_conn);