Repository: deltaspike Updated Branches: refs/heads/master ec5ce5dd8 -> 1e660a251
DELTASPIKE-1169 Create separate interfaces for the main concerns of EntityRepository. Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/1e660a25 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/1e660a25 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/1e660a25 Branch: refs/heads/master Commit: 1e660a25131f249ab8e82b3c478a2953eceb670b Parents: ec5ce5d Author: John D. Ament <[email protected]> Authored: Wed Jun 8 22:00:21 2016 -0400 Committer: John D. Ament <[email protected]> Committed: Wed Jun 8 22:00:32 2016 -0400 ---------------------------------------------------------------------- .../data/api/EntityCountRepository.java | 51 +++++++++++ .../data/api/EntityPersistenceRepository.java | 91 +++++++++++++++++++ .../deltaspike/data/api/EntityRepository.java | 95 +------------------- 3 files changed, 144 insertions(+), 93 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1e660a25/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityCountRepository.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityCountRepository.java b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityCountRepository.java new file mode 100644 index 0000000..b7d0870 --- /dev/null +++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityCountRepository.java @@ -0,0 +1,51 @@ +/* + * 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 javax.persistence.metamodel.SingularAttribute; + +public interface EntityCountRepository<E> +{ + /** + * Count all existing entities of entity class {@code <E>}. + * @return Counter. + */ + Long count(); + + /** + * Count existing entities of entity class {@code <E>} + * with for a given object and a specific set of properties.. + * @param example Sample entity. Query all like. + * @param attributes Which attributes to consider for the query. + * + * @return Counter. + */ + Long count(E example, SingularAttribute<E, ?>... attributes); + + /** + * Count existing entities of entity class using the like operator for String attributes {@code <E>} + * with for a given object and a specific set of properties.. + * @param example Sample entity. Query all like. + * @param attributes Which attributes to consider for the query. + * + * @return Counter. + */ + Long countLike(E example, SingularAttribute<E, ?>... attributes); +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1e660a25/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityPersistenceRepository.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityPersistenceRepository.java b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityPersistenceRepository.java new file mode 100644 index 0000000..4e3af76 --- /dev/null +++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityPersistenceRepository.java @@ -0,0 +1,91 @@ +/* + * 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 org.apache.deltaspike.core.spi.activation.Deactivatable; + +import java.io.Serializable; + +public interface EntityPersistenceRepository<E, PK extends Serializable> extends Deactivatable +{ + /** + * Persist (new entity) or merge the given entity. The distinction on calling either + * method is done based on the primary key field being null or not. + * If this results in wrong behavior for a specific case, consider using the + * {@link org.apache.deltaspike.data.api.EntityManagerDelegate} which offers both + * {@code persist} and {@code merge}. + * @param entity Entity to save. + * @return Returns the modified entity. + */ + E save(E entity); + + /** + * {@link #save(Object)}s the given entity and flushes the persistence context afterwards. + * @param entity Entity to save. + * @return Returns the modified entity. + */ + E saveAndFlush(E entity); + + /** + * {@link #save(Object)}s the given entity and flushes the persistence context afterwards, + * followed by a refresh (e.g. to load DB trigger modifications). + * @param entity Entity to save. + * @return Returns the modified entity. + */ + E saveAndFlushAndRefresh(E entity); + + /** + * Convenience access to {@link javax.persistence.EntityManager#remove(Object)}. + * @param entity Entity to remove. + */ + void remove(E entity); + + /** + * Convenience access to {@link javax.persistence.EntityManager#remove(Object)} + * with a following flush. + * @param entity Entity to remove. + */ + void removeAndFlush(E entity); + + /** + * Convenience access to {@link javax.persistence.EntityManager#remove(Object)} + * with an detached entity. + * @param entity Entity to remove. + */ + void attachAndRemove(E entity); + + /** + * Convenience access to {@link javax.persistence.EntityManager#refresh(Object)}. + * @param entity Entity to refresh. + */ + void refresh(E entity); + + /** + * Convenience access to {@link javax.persistence.EntityManager#flush()}. + */ + void flush(); + + /** + * Return the id of the entity. Returns null if the entity does not yet have an id. + * @param example Sample entity. + * @return id of the entity + */ + PK getPrimaryKey(E example); +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1e660a25/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityRepository.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityRepository.java b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityRepository.java index 647a96e..33243a9 100755 --- a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityRepository.java +++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityRepository.java @@ -23,75 +23,17 @@ import java.util.List; import javax.persistence.metamodel.SingularAttribute; -import org.apache.deltaspike.core.spi.activation.Deactivatable; - /** * Base Repository interface. All methods are implemented by the CDI extension. * * @param <E> Entity type. * @param <PK> Primary key type. */ -public interface EntityRepository<E, PK extends Serializable> extends Deactivatable +public interface EntityRepository<E, PK extends Serializable> extends EntityPersistenceRepository<E, PK>, + EntityCountRepository<E> { /** - * Persist (new entity) or merge the given entity. The distinction on calling either - * method is done based on the primary key field being null or not. - * If this results in wrong behavior for a specific case, consider using the - * {@link org.apache.deltaspike.data.api.EntityManagerDelegate} which offers both - * {@code persist} and {@code merge}. - * @param entity Entity to save. - * @return Returns the modified entity. - */ - E save(E entity); - - /** - * {@link #save(Object)}s the given entity and flushes the persistence context afterwards. - * @param entity Entity to save. - * @return Returns the modified entity. - */ - E saveAndFlush(E entity); - - /** - * {@link #save(Object)}s the given entity and flushes the persistence context afterwards, - * followed by a refresh (e.g. to load DB trigger modifications). - * @param entity Entity to save. - * @return Returns the modified entity. - */ - E saveAndFlushAndRefresh(E entity); - - /** - * Convenience access to {@link javax.persistence.EntityManager#remove(Object)}. - * @param entity Entity to remove. - */ - void remove(E entity); - - /** - * Convenience access to {@link javax.persistence.EntityManager#remove(Object)} - * with a following flush. - * @param entity Entity to remove. - */ - void removeAndFlush(E entity); - - /** - * Convenience access to {@link javax.persistence.EntityManager#remove(Object)} - * with an detached entity. - * @param entity Entity to remove. - */ - void attachAndRemove(E entity); - - /** - * Convenience access to {@link javax.persistence.EntityManager#refresh(Object)}. - * @param entity Entity to refresh. - */ - void refresh(E entity); - - /** - * Convenience access to {@link javax.persistence.EntityManager#flush()}. - */ - void flush(); - - /** * Entity lookup by primary key. Convenicence method around * {@link javax.persistence.EntityManager#find(Class, Object)}. * @param primaryKey DB primary key. @@ -149,37 +91,4 @@ public interface EntityRepository<E, PK extends Serializable> extends Deactivata * @return List of entities matching the example, or empty if none found. */ List<E> findByLike(E example, int start, int max, SingularAttribute<E, ?>... attributes); - - /** - * Count all existing entities of entity class {@code <E>}. - * @return Counter. - */ - Long count(); - - /** - * Count existing entities of entity class {@code <E>} - * with for a given object and a specific set of properties.. - * @param example Sample entity. Query all like. - * @param attributes Which attributes to consider for the query. - * - * @return Counter. - */ - Long count(E example, SingularAttribute<E, ?>... attributes); - - /** - * Count existing entities of entity class using the like operator for String attributes {@code <E>} - * with for a given object and a specific set of properties.. - * @param example Sample entity. Query all like. - * @param attributes Which attributes to consider for the query. - * - * @return Counter. - */ - Long countLike(E example, SingularAttribute<E, ?>... attributes); - - /** - * Return the id of the entity. Returns null if the entity does not yet have an id. - * @param example Sample entity. - * @return id of the entity - */ - PK getPrimaryKey(E example); }
