Commit 669b81e1de37a1523e48dfc67026677c996f2325
1 parent
e82afb17
Exists in
master
Alterando a interface Crud para permitir a implementação de mais abstrações.
Showing
2 changed files
with
68 additions
and
7 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/template/Crud.java
| ... | ... | @@ -39,16 +39,53 @@ package br.gov.frameworkdemoiselle.template; |
| 39 | 39 | import java.io.Serializable; |
| 40 | 40 | import java.util.List; |
| 41 | 41 | |
| 42 | +/** | |
| 43 | + * Interface containing basic methods for creating, updating and deleting entities (the CRUD design pattern). | |
| 44 | + * | |
| 45 | + * @author serpro | |
| 46 | + * | |
| 47 | + * @param <T> Type of the entity | |
| 48 | + * @param <I> Type of the identification attribute of the entity | |
| 49 | + */ | |
| 42 | 50 | public interface Crud<T, I> extends Serializable { |
| 43 | 51 | |
| 52 | + /** | |
| 53 | + * Find an entity by it's identification attribute and make it transient. | |
| 54 | + * | |
| 55 | + * @param id The unique identification to find the entity to be deleted. | |
| 56 | + */ | |
| 44 | 57 | void delete(I id); |
| 45 | 58 | |
| 59 | + /** | |
| 60 | + * List all instances of the given entity. | |
| 61 | + * | |
| 62 | + * @return The (possibly empty) list of all instances of the given entity. | |
| 63 | + */ | |
| 46 | 64 | List<T> findAll(); |
| 47 | 65 | |
| 48 | - void insert(T bean); | |
| 66 | + /** | |
| 67 | + * Insert an entity and return the inserted instance of the entity | |
| 68 | + * | |
| 69 | + * @param bean The bean to be inserted | |
| 70 | + * @return The inserted instance of the entity | |
| 71 | + */ | |
| 72 | + T insert(T bean); | |
| 49 | 73 | |
| 74 | + /** | |
| 75 | + * Find an entity's instance by it's identification attribute and return it | |
| 76 | + * @param id Value of the identification attribute of the desired entity's instance | |
| 77 | + * @return The entity's instance whose identification attribute's value matches | |
| 78 | + * the one passed as argument. | |
| 79 | + */ | |
| 50 | 80 | T load(I id); |
| 51 | 81 | |
| 52 | - void update(T bean); | |
| 82 | + /** | |
| 83 | + * Update an instance of the entity with the bean's attributes as new values. | |
| 84 | + * | |
| 85 | + * @param bean The bean instance containing both the identification value to find the old | |
| 86 | + * instance and the new values for the instance's attributes. | |
| 87 | + * @return The updated entity's instance | |
| 88 | + */ | |
| 89 | + T update(T bean); | |
| 53 | 90 | |
| 54 | 91 | } | ... | ... |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/template/JPACrud.java
| ... | ... | @@ -47,6 +47,7 @@ import javax.enterprise.inject.Instance; |
| 47 | 47 | import javax.inject.Inject; |
| 48 | 48 | import javax.persistence.Basic; |
| 49 | 49 | import javax.persistence.Column; |
| 50 | +import javax.persistence.EntityExistsException; | |
| 50 | 51 | import javax.persistence.EntityManager; |
| 51 | 52 | import javax.persistence.Enumerated; |
| 52 | 53 | import javax.persistence.Query; |
| ... | ... | @@ -145,25 +146,48 @@ public class JPACrud<T, I> implements Crud<T, I> { |
| 145 | 146 | } |
| 146 | 147 | } |
| 147 | 148 | |
| 149 | + /** | |
| 150 | + * Insert a new instance of the entity in the database. After insertion the entity becomes "managed" | |
| 151 | + * as stated by the JPA specification. | |
| 152 | + * | |
| 153 | + * @param entity A non-detached instance of an entity. If this instance is not managed, it will be after this method returns. | |
| 154 | + * @return The same instance passed as argument. | |
| 155 | + * @throws EntityExistsException if <code>entity</code> is an unmanaged instance | |
| 156 | + * and the persistence provider already has a persisted instance that matches the <code>entity</code>'s primary key. | |
| 157 | + * @throws IllegalArgumentException if the instance is not an entity | |
| 158 | + * @see EntityManager#persist(Object entity) | |
| 159 | + */ | |
| 148 | 160 | @Override |
| 149 | 161 | @Transactional |
| 150 | - public void insert(final T entity) { | |
| 162 | + public T insert(final T entity) { | |
| 151 | 163 | getEntityManager().persist(entity); |
| 164 | + return entity; | |
| 152 | 165 | } |
| 153 | 166 | |
| 167 | + /** | |
| 168 | + * Finds an instance of this entity by it's primary ID and asks to the | |
| 169 | + * persistence provider to remove this entity instance from the persistence | |
| 170 | + * context. | |
| 171 | + * | |
| 172 | + * @see EntityManager#remove(Object entity) | |
| 173 | + */ | |
| 154 | 174 | @Override |
| 155 | 175 | @Transactional |
| 156 | 176 | public void delete(final I id) { |
| 157 | 177 | T entity = getEntityManager().getReference(getBeanClass(), id); |
| 158 | 178 | getEntityManager().remove(entity); |
| 159 | 179 | } |
| 160 | - | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * Merge all changes made to the passed entity to a managed entity. The passed instance is not | |
| 183 | + * modified nor becomes managed, instead the managed entity is returned by this method. | |
| 184 | + */ | |
| 161 | 185 | @Override |
| 162 | 186 | @Transactional |
| 163 | - public void update(final T entity) { | |
| 164 | - getEntityManager().merge(entity); | |
| 187 | + public T update(T entity) { | |
| 188 | + return getEntityManager().merge(entity); | |
| 165 | 189 | } |
| 166 | - | |
| 190 | + | |
| 167 | 191 | @Override |
| 168 | 192 | public T load(final I id) { |
| 169 | 193 | return getEntityManager().find(getBeanClass(), id); | ... | ... |