001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.query;
020:
021: import java.util.List;
022: import javax.persistence.EntityManager;
023: import javax.persistence.Query;
024:
025: import org.apache.openjpa.persistence.query.common.apps.Entity1;
026:
027: public class TestSimple extends BaseQueryTest {
028:
029: private Query q = null;
030:
031: public TestSimple(String test) {
032: super (test);
033: }
034:
035: public void setUp() {
036: deleteAll(Entity1.class);
037: }
038:
039: public void testSimple() throws java.io.IOException {
040: // test create
041: {
042: EntityManager em = currentEntityManager();
043: startTx(em);
044: em.persist(new Entity1(0, "testSimple", 12));
045: endTx(em);
046: endEm(em);
047: }
048:
049: // test Query
050: {
051: EntityManager em = currentEntityManager();
052: startTx(em);
053: List l = em.createQuery(
054: "SELECT o FROM Entity1 o "
055: + "WHERE o.stringField = 'testSimple'")
056: .getResultList();
057: assertSize(1, l);
058: endTx(em);
059: endEm(em);
060: }
061:
062: // test Update
063: {
064: EntityManager em = currentEntityManager();
065: startTx(em);
066: ((Entity1) em.createQuery(
067: "SELECT o FROM Entity1 o "
068: + "WHERE o.stringField = 'testSimple'")
069: .getSingleResult()).setStringField("testSimple2");
070: endTx(em);
071: endEm(em);
072:
073: em = currentEntityManager();
074: startTx(em);
075: q = em.createQuery("SELECT o FROM Entity1 o "
076: + "WHERE o.stringField = 'testSimple'");
077: assertSize(0, q);
078: q = em.createQuery("SELECT o FROM Entity1 o "
079: + "WHERE o.stringField = 'testSimple2'");
080: assertSize(1, q);
081: endTx(em);
082: endEm(em);
083: }
084:
085: // test delete
086: {
087: EntityManager em = currentEntityManager();
088: startTx(em);
089: em.remove(em.createQuery(
090: "SELECT o FROM Entity1 o "
091: + "WHERE o.stringField = 'testSimple2'")
092: .getSingleResult());
093: endTx(em);
094: endEm(em);
095:
096: em = currentEntityManager();
097: startTx(em);
098:
099: q = em.createQuery("SELECT o FROM Entity1 o "
100: + "WHERE o.stringField = 'testSimple2'");
101: assertSize(0, q);
102: endTx(em);
103: endEm(em);
104: }
105: }
106: }
|