Commit b4872a11383fc929eb6a0caabfab769fce004284
Exists in
master
Merge branch 'master' of git@github.com:demoiselle/framework.git
Showing
5 changed files
with
77 additions
and
1 deletions
Show diff stats
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.Entity; | |
| 50 | 51 | import javax.persistence.EntityExistsException; |
| 51 | 52 | import javax.persistence.EntityManager; |
| 52 | 53 | import javax.persistence.Enumerated; |
| ... | ... | @@ -195,7 +196,17 @@ public class JPACrud<T, I> implements Crud<T, I> { |
| 195 | 196 | |
| 196 | 197 | @Override |
| 197 | 198 | public List<T> findAll() { |
| 198 | - return findByJPQL("select this from " + getBeanClass().getSimpleName() + " this"); | |
| 199 | + Entity entityAnnotation = getBeanClass().getAnnotation(Entity.class); | |
| 200 | + String entityName = null; | |
| 201 | + if (entityAnnotation!=null | |
| 202 | + && entityAnnotation.name()!=null | |
| 203 | + && !entityAnnotation.name().trim().equals("")) { | |
| 204 | + entityName = entityAnnotation.name(); | |
| 205 | + } | |
| 206 | + else { | |
| 207 | + entityName = getBeanClass().getSimpleName(); | |
| 208 | + } | |
| 209 | + return findByJPQL("select this from " + entityName + " this"); | |
| 199 | 210 | } |
| 200 | 211 | |
| 201 | 212 | /** | ... | ... |
impl/extension/jpa/src/test/java/template/JPACrudTest.java
| ... | ... | @@ -24,6 +24,9 @@ public class JPACrudTest { |
| 24 | 24 | |
| 25 | 25 | @Inject |
| 26 | 26 | private MyCrud crud; |
| 27 | + | |
| 28 | + @Inject | |
| 29 | + private MyNamedCrud namedCrud; | |
| 27 | 30 | |
| 28 | 31 | @Deployment(name = "1") |
| 29 | 32 | public static WebArchive createDeployment() { |
| ... | ... | @@ -80,6 +83,17 @@ public class JPACrudTest { |
| 80 | 83 | |
| 81 | 84 | assertEquals(list.size(), 4); |
| 82 | 85 | } |
| 86 | + | |
| 87 | + @Test | |
| 88 | + public void findAllNamedEntity() { | |
| 89 | + populateNamedEntity(4, 0); | |
| 90 | + | |
| 91 | + List<MyNamedEntity> list; | |
| 92 | + list = namedCrud.findAll(); | |
| 93 | + | |
| 94 | + assertEquals(list.size(), 4); | |
| 95 | + } | |
| 96 | + | |
| 83 | 97 | |
| 84 | 98 | private void populate(int size, int offset) { |
| 85 | 99 | MyEntity entity; |
| ... | ... | @@ -93,6 +107,18 @@ public class JPACrudTest { |
| 93 | 107 | } |
| 94 | 108 | } |
| 95 | 109 | |
| 110 | + private void populateNamedEntity(int size, int offset) { | |
| 111 | + MyNamedEntity entity; | |
| 112 | + | |
| 113 | + for (int i = 0; i < size; i++) { | |
| 114 | + entity = new MyNamedEntity(); | |
| 115 | + entity.setId(createId("id-" + (i + 1 + offset))); | |
| 116 | + entity.setDescription("desc-" + (i + 1 + offset)); | |
| 117 | + | |
| 118 | + namedCrud.insert(entity); | |
| 119 | + } | |
| 120 | + } | |
| 121 | + | |
| 96 | 122 | private String createId(String id) { |
| 97 | 123 | return this.getClass().getName() + "_" + id; |
| 98 | 124 | } | ... | ... |
impl/extension/jpa/src/test/java/template/MyNamedCrud.java
0 → 100644
impl/extension/jpa/src/test/java/template/MyNamedEntity.java
0 → 100644
| ... | ... | @@ -0,0 +1,29 @@ |
| 1 | +package template; | |
| 2 | + | |
| 3 | +import javax.persistence.Entity; | |
| 4 | +import javax.persistence.Id; | |
| 5 | + | |
| 6 | +@Entity(name = "namedEntity") | |
| 7 | +public class MyNamedEntity { | |
| 8 | + | |
| 9 | + @Id | |
| 10 | + private String id; | |
| 11 | + | |
| 12 | + private String description; | |
| 13 | + | |
| 14 | + public String getId() { | |
| 15 | + return id; | |
| 16 | + } | |
| 17 | + | |
| 18 | + public void setId(String id) { | |
| 19 | + this.id = id; | |
| 20 | + } | |
| 21 | + | |
| 22 | + public String getDescription() { | |
| 23 | + return description; | |
| 24 | + } | |
| 25 | + | |
| 26 | + public void setDescription(String description) { | |
| 27 | + this.description = description; | |
| 28 | + } | |
| 29 | +} | ... | ... |
impl/extension/jpa/src/test/resources/template/persistence.xml