You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by hw...@apache.org on 2015/12/19 14:17:41 UTC
deltaspike git commit: DELTASPIKE-1045 new API classes exposing all
EntityManager methods
Repository: deltaspike
Updated Branches:
refs/heads/master d0923ec55 -> 944d13aef
DELTASPIKE-1045 new API classes exposing all EntityManager methods
FullEntityRepository and AbstractFullEntityRepository also extend or
implement EntityManagerDelegate
Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/944d13ae
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/944d13ae
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/944d13ae
Branch: refs/heads/master
Commit: 944d13aef4fbbe62a5d8e08e11dc99917e3e6810
Parents: d0923ec
Author: Harald Wellmann <hw...@apache.org>
Authored: Sat Dec 19 14:17:33 2015 +0100
Committer: Harald Wellmann <hw...@apache.org>
Committed: Sat Dec 19 14:17:33 2015 +0100
----------------------------------------------------------------------
.../data/api/AbstractFullEntityRepository.java | 36 ++++
.../data/api/FullEntityRepository.java | 33 +++
.../data/impl/RepositoryExtension.java | 5 +-
.../impl/handler/FullEntityRepositoryTest.java | 206 +++++++++++++++++++
.../test/service/FullRepositoryAbstract.java | 38 ++++
.../test/service/FullRepositoryInterface.java | 42 ++++
6 files changed, 359 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/944d13ae/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/AbstractFullEntityRepository.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/AbstractFullEntityRepository.java b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/AbstractFullEntityRepository.java
new file mode 100644
index 0000000..f3d23b2
--- /dev/null
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/AbstractFullEntityRepository.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.api;
+
+import java.io.Serializable;
+
+
+/**
+ * Full Repository base class to be extended by concrete implementations.
+ * Unlike {@link AbstractEntityRepository}, this class exposes all methods
+ * of the underlying EntityManager.
+ *
+ * @param <E> Entity type.
+ * @param <PK> Primary key type.
+ */
+public abstract class AbstractFullEntityRepository<E, PK extends Serializable>
+ extends AbstractEntityRepository<E, PK>
+ implements EntityManagerDelegate<E>
+{
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/944d13ae/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/FullEntityRepository.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/FullEntityRepository.java b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/FullEntityRepository.java
new file mode 100644
index 0000000..11910ad
--- /dev/null
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/FullEntityRepository.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.api;
+
+import java.io.Serializable;
+
+/**
+ * Full repository interface, including all methods not exposed by the base repository
+ * interface.
+ *
+ * @param <E> Entity type.
+ * @param <PK> Primary key type.
+ */
+public interface FullEntityRepository<E, PK extends Serializable>
+ extends EntityRepository<E, PK>, EntityManagerDelegate<E>
+{
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/944d13ae/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/RepositoryExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/RepositoryExtension.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/RepositoryExtension.java
index e9bee96..f33545d 100755
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/RepositoryExtension.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/RepositoryExtension.java
@@ -36,6 +36,7 @@ import javax.enterprise.inject.spi.ProcessAnnotatedType;
import org.apache.deltaspike.core.spi.activation.Deactivatable;
import org.apache.deltaspike.core.util.ClassDeactivationUtils;
import org.apache.deltaspike.data.api.AbstractEntityRepository;
+import org.apache.deltaspike.data.api.AbstractFullEntityRepository;
import org.apache.deltaspike.data.api.Repository;
import org.apache.deltaspike.data.impl.meta.RepositoryComponents;
import org.apache.deltaspike.data.impl.meta.unit.PersistenceUnits;
@@ -141,7 +142,9 @@ public class RepositoryExtension implements Extension, Deactivatable
private <X> boolean isVetoed(AnnotatedType<X> annotated)
{
- return annotated.getJavaClass().equals(AbstractEntityRepository.class);
+ Class<X> javaClass = annotated.getJavaClass();
+ return javaClass.equals(AbstractEntityRepository.class) ||
+ javaClass.equals(AbstractFullEntityRepository.class);
}
public RepositoryComponents getComponents()
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/944d13ae/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/FullEntityRepositoryTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/FullEntityRepositoryTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/FullEntityRepositoryTest.java
new file mode 100644
index 0000000..6d20c7e
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/FullEntityRepositoryTest.java
@@ -0,0 +1,206 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.impl.handler;
+
+import static org.apache.deltaspike.data.test.util.TestDeployments.initDeployment;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import javax.enterprise.inject.Produces;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+import org.apache.deltaspike.data.test.TransactionalTestCase;
+import org.apache.deltaspike.data.test.domain.Simple;
+import org.apache.deltaspike.data.test.service.FullRepositoryAbstract;
+import org.apache.deltaspike.data.test.service.FullRepositoryInterface;
+import org.apache.deltaspike.test.category.WebProfileCategory;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.shrinkwrap.api.Archive;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category(WebProfileCategory.class)
+public class FullEntityRepositoryTest extends TransactionalTestCase
+{
+
+ @Deployment
+ public static Archive<?> deployment()
+ {
+ return initDeployment()
+ .addClasses(FullRepositoryInterface.class)
+ .addClasses(FullRepositoryAbstract.class)
+ .addPackage(Simple.class.getPackage());
+ }
+
+ @Inject
+ private FullRepositoryInterface repo;
+
+ @Inject
+ private FullRepositoryAbstract repoAbstract;
+
+ @Produces
+ @PersistenceContext
+ private EntityManager entityManager;
+
+ @Test
+ public void should_save() throws Exception
+ {
+ // given
+ Simple simple = new Simple("test");
+
+ // when
+ simple = repo.save(simple);
+
+ // then
+ assertNotNull(simple.getId());
+ }
+
+ @Test
+ public void should_save_abstract() throws Exception
+ {
+ // given
+ Simple simple = new Simple("test");
+
+ // when
+ simple = repoAbstract.save(simple);
+
+ // then
+ assertNotNull(simple.getId());
+ }
+
+ @Test
+ public void should_persist() throws Exception
+ {
+ // given
+ Simple simple = new Simple("test");
+
+ // when
+ repo.persist(simple);
+
+ // then
+ assertNotNull(simple.getId());
+ }
+
+ @Test
+ public void should_persist_abstract() throws Exception
+ {
+ // given
+ Simple simple = new Simple("test");
+
+ // when
+ repoAbstract.persist(simple);
+
+ // then
+ assertNotNull(simple.getId());
+ }
+
+ @Test
+ public void should_save_with_merge() throws Exception
+ {
+ // given
+ Simple simple = testData.createSimple("testMerge");
+ Long id = simple.getId();
+
+ // when
+ final String newName = "testMergeUpdated";
+ simple.setName(newName);
+ simple = repo.save(simple);
+
+ // then
+ assertEquals(id, simple.getId());
+ assertEquals(newName, simple.getName());
+ }
+
+ @Test
+ public void should_save_with_merge_abstract() throws Exception
+ {
+ // given
+ Simple simple = testData.createSimple("testMerge");
+ Long id = simple.getId();
+
+ // when
+ final String newName = "testMergeUpdated";
+ simple.setName(newName);
+ simple = repoAbstract.save(simple);
+
+ // then
+ assertEquals(id, simple.getId());
+ assertEquals(newName, simple.getName());
+ }
+
+ @Test
+ public void should_merge() throws Exception
+ {
+ // given
+ Simple simple = testData.createSimple("testMerge");
+ Long id = simple.getId();
+
+ // when
+ final String newName = "testMergeUpdated";
+ simple.setName(newName);
+ simple = repo.merge(simple);
+
+ // then
+ assertEquals(id, simple.getId());
+ assertEquals(newName, simple.getName());
+ }
+
+ @Test
+ public void should_merge_abstract() throws Exception
+ {
+ // given
+ Simple simple = testData.createSimple("testMerge");
+ Long id = simple.getId();
+
+ // when
+ final String newName = "testMergeUpdated";
+ simple.setName(newName);
+ simple = repoAbstract.merge(simple);
+
+ // then
+ assertEquals(id, simple.getId());
+ assertEquals(newName, simple.getName());
+ }
+
+ @Test
+ public void should_save_and_flush() throws Exception
+ {
+ // given
+ Simple simple = new Simple("test");
+
+ // when
+ simple = repo.saveAndFlush(simple);
+ Simple fetch = (Simple) entityManager
+ .createNativeQuery("select * from SIMPLE_TABLE where id = ?", Simple.class)
+ .setParameter(1, simple.getId())
+ .getSingleResult();
+
+ // then
+ assertEquals(simple.getId(), fetch.getId());
+ }
+
+ @Override
+ protected EntityManager getEntityManager()
+ {
+ return entityManager;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/944d13ae/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/FullRepositoryAbstract.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/FullRepositoryAbstract.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/FullRepositoryAbstract.java
new file mode 100644
index 0000000..b5f31af
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/FullRepositoryAbstract.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.test.service;
+
+import org.apache.deltaspike.data.api.AbstractFullEntityRepository;
+import org.apache.deltaspike.data.api.Repository;
+import org.apache.deltaspike.data.test.domain.Simple;
+
+@Repository
+public abstract class FullRepositoryAbstract
+ extends AbstractFullEntityRepository<Simple, Long>
+{
+ public String getTableName()
+ {
+ return tableName();
+ }
+
+ public String getEntityName()
+ {
+ return entityName();
+ }
+}
http://git-wip-us.apache.org/repos/asf/deltaspike/blob/944d13ae/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/FullRepositoryInterface.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/FullRepositoryInterface.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/FullRepositoryInterface.java
new file mode 100644
index 0000000..05a7d6e
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/FullRepositoryInterface.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.test.service;
+
+import static javax.persistence.LockModeType.PESSIMISTIC_READ;
+
+import org.apache.deltaspike.data.api.FullEntityRepository;
+import org.apache.deltaspike.data.api.Modifying;
+import org.apache.deltaspike.data.api.Query;
+import org.apache.deltaspike.data.api.Repository;
+import org.apache.deltaspike.data.test.domain.Simple;
+
+@Repository
+public interface FullRepositoryInterface extends FullEntityRepository<Simple, Long>
+{
+
+ @Query(lock = PESSIMISTIC_READ)
+ Simple findByName(String name);
+
+ @Query(named = Simple.BY_NAME_LIKE)
+ Simple findByNameNoLock(String name);
+
+ @Modifying @Query("delete from Simple")
+ int deleteAll();
+
+}