From 9cd33317c128109efb53e83196d5d41d19ff68fd Mon Sep 17 00:00:00 2001 From: subaochen Date: Sun, 11 Sep 2016 21:54:07 +0800 Subject: [PATCH 1/2] DELTASPIKE-1203:jpa example --- deltaspike/examples/jpa-examples/README.md | 25 ++++ deltaspike/examples/jpa-examples/pom.xml | 114 ++++++++++++++++++ .../apache/deltaspike/example/Article.java | 108 +++++++++++++++++ .../deltaspike/example/ArticleController.java | 103 ++++++++++++++++ .../apache/deltaspike/example/HttpParam.java | 41 +++++++ .../apache/deltaspike/example/HttpParams.java | 41 +++++++ .../deltaspike/example/LogProducer.java | 37 ++++++ .../apache/deltaspike/example/Resources.java | 63 ++++++++++ .../main/resources/META-INF/persistence.xml | 15 +++ .../src/main/webapp/WEB-INF/beans.xml | 23 ++++ .../src/main/webapp/WEB-INF/faces-config.xml | 40 ++++++ .../src/main/webapp/WEB-INF/web.xml | 43 +++++++ .../jpa-examples/src/main/webapp/add.xhtml | 52 ++++++++ .../jpa-examples/src/main/webapp/index.xhtml | 39 ++++++ .../jpa-examples/src/main/webapp/list.xhtml | 64 ++++++++++ deltaspike/examples/pom.xml | 9 +- 16 files changed, 816 insertions(+), 1 deletion(-) create mode 100644 deltaspike/examples/jpa-examples/README.md create mode 100644 deltaspike/examples/jpa-examples/pom.xml create mode 100644 deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/Article.java create mode 100644 deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/ArticleController.java create mode 100644 deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/HttpParam.java create mode 100644 deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/HttpParams.java create mode 100644 deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/LogProducer.java create mode 100644 deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/Resources.java create mode 100644 deltaspike/examples/jpa-examples/src/main/resources/META-INF/persistence.xml create mode 100644 deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/beans.xml create mode 100644 deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/faces-config.xml create mode 100644 deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/web.xml create mode 100644 deltaspike/examples/jpa-examples/src/main/webapp/add.xhtml create mode 100644 deltaspike/examples/jpa-examples/src/main/webapp/index.xhtml create mode 100644 deltaspike/examples/jpa-examples/src/main/webapp/list.xhtml diff --git a/deltaspike/examples/jpa-examples/README.md b/deltaspike/examples/jpa-examples/README.md new file mode 100644 index 000000000..f1c23e019 --- /dev/null +++ b/deltaspike/examples/jpa-examples/README.md @@ -0,0 +1,25 @@ +Notice: 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. + +JPA support in deltaspike +========================= +This example shows: + +1, how to use JPA in deltaspike; +2, the function of @Transactional, how @Transactional save entity automatically. + +BUT, this example is not involved in deltaspike Data module, please see data-playground example for deltaspike data module. diff --git a/deltaspike/examples/jpa-examples/pom.xml b/deltaspike/examples/jpa-examples/pom.xml new file mode 100644 index 000000000..a17e2f5e1 --- /dev/null +++ b/deltaspike/examples/jpa-examples/pom.xml @@ -0,0 +1,114 @@ + + + + + + 4.0.0 + + + org.apache.deltaspike.examples + examples-project + 1.7.2-SNAPSHOT + + + deltaspike-jpa-examples + + Apache DeltaSpike JPA Examples + + war + + + true + + + + + + org.apache.deltaspike.core + deltaspike-core-api + + + + org.apache.deltaspike.core + deltaspike-core-impl + + + + org.apache.deltaspike.modules + deltaspike-jsf-module-api + + + + org.apache.deltaspike.modules + deltaspike-jsf-module-impl + + + + org.apache.deltaspike.modules + deltaspike-jpa-module-api + + + + org.apache.deltaspike.modules + deltaspike-jpa-module-impl + + + + + + + + + javax + javaee-api + 7.0 + provided + + + + + ${project.artifactId} + + + diff --git a/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/Article.java b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/Article.java new file mode 100644 index 000000000..10505b6f0 --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/Article.java @@ -0,0 +1,108 @@ +/* + * 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.example; + +import java.io.Serializable; +import java.util.Date; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +@Entity +@Table(name = "articles") +public class Article implements Serializable +{ + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue + private Long id; + + private String title; + private String content; + + @Temporal(TemporalType.TIMESTAMP) + @Column(name = "time_created") + private Date date; + + // A must have! + public Article() + { + // this form used by Hibernate + } + + public Article(String title, Date date) + { + // for application use, to create new articles + this.title = title; + this.date = date; + } + + public Long getId() + { + return id; + } + + private void setId(Long id) + { + this.id = id; + } + + public Date getDate() + { + return date; + } + + public void setDate(Date date) + { + this.date = date; + } + + public String getTitle() + { + return title; + } + + public void setTitle(String title) + { + this.title = title; + } + + public String getContent() + { + return content; + } + + public void setContent(String content) + { + this.content = content; + } + + @Override + public String toString() + { + return "Article{" + "id=" + id + ", title=" + title + ", content=" + content + ", date=" + date + '}'; + } + +} diff --git a/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/ArticleController.java b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/ArticleController.java new file mode 100644 index 000000000..56c17163e --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/ArticleController.java @@ -0,0 +1,103 @@ +/* + * 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.example; + +import java.io.Serializable; +import java.util.Date; +import java.util.List; +import java.util.logging.Logger; +import javax.faces.application.FacesMessage; +import javax.faces.bean.ViewScoped; +import javax.faces.context.FacesContext; +import javax.inject.Inject; +import javax.inject.Named; +import javax.persistence.EntityManager; +import javax.transaction.Transactional; + +/** + * + * @author Su Baochen + */ +@Named +@ViewScoped +@Transactional +public class ArticleController implements Serializable +{ + + private Article article = new Article(); + + @Inject + private Logger log; + + @Inject + private EntityManager em; + + @Inject + private FacesContext facesContext; + + @HttpParam("aid") + @Inject + private String aid; // article id + + public Article getArticle() + { + return article; + } + + public void setArticle(Article article) + { + this.article = article; + } + + public Article findArticleById(Long id) + { + article = em.find(Article.class, id); + return article; + } + + + public String persist() + { + article.setDate(new Date()); + em.merge(this.article); + facesContext.addMessage(null, new FacesMessage("article:" + article.getTitle() + " persisted")); + return "persisted"; + } + + public String delete(Article article) + { + em.remove(article); + facesContext.addMessage(null, new FacesMessage("article:" + article.getTitle() + " deleted")); + return "deleted"; + } + + public List
getAllArticles() + { + return em.createQuery("from Article a order by a.id desc").getResultList(); + } + + public void loadArticle() + { + if (aid != null) + { + this.article = findArticleById(Long.valueOf(aid)); + } + } + +} diff --git a/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/HttpParam.java b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/HttpParam.java new file mode 100644 index 000000000..6ce615c8f --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/HttpParam.java @@ -0,0 +1,41 @@ +/* + * 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.example; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import javax.enterprise.util.Nonbinding; +import javax.inject.Qualifier; + +/** + * + * @author subaochen + */ +@Qualifier +@Retention(RUNTIME) +@Target({ METHOD, FIELD, PARAMETER, TYPE }) +public @interface HttpParam +{ + @Nonbinding String value(); +} diff --git a/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/HttpParams.java b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/HttpParams.java new file mode 100644 index 000000000..91938c56c --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/HttpParams.java @@ -0,0 +1,41 @@ +/* + * 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.example; + +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.faces.context.FacesContext; +import javax.servlet.ServletRequest; + +/** + * + * @author subaochen + */ +public class HttpParams +{ + + @Produces + @HttpParam("") + String getParamValue(InjectionPoint ip) + { + ServletRequest request = (ServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); + return request.getParameter(ip.getAnnotated().getAnnotation(HttpParam.class).value()); + } + +} diff --git a/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/LogProducer.java b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/LogProducer.java new file mode 100644 index 000000000..6467a2075 --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/LogProducer.java @@ -0,0 +1,37 @@ +/* + * 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.example; + +import java.util.logging.Logger; +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; + +/** + * + * @author Su Baochen + */ +public class LogProducer +{ + + @Produces + Logger createLogger(final InjectionPoint ip) + { + return Logger.getLogger(ip.getMember().getDeclaringClass().getName()); + } +} diff --git a/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/Resources.java b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/Resources.java new file mode 100644 index 000000000..1d622586f --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/java/org/apache/deltaspike/example/Resources.java @@ -0,0 +1,63 @@ +/* + * 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.example; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.inject.Default; +import javax.enterprise.inject.Disposes; +import javax.enterprise.inject.Produces; +import javax.faces.context.FacesContext; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; + +// @TODO why ApplicatonScoped? +@ApplicationScoped +public class Resources +{ + + @PersistenceUnit + private EntityManagerFactory entityManagerFactory; + + @Produces + @Default + @RequestScoped + public EntityManager create() + { + return this.entityManagerFactory.createEntityManager(); + } + + public void dispose(@Disposes @Default EntityManager entityManager) + { + if (entityManager.isOpen()) + { + entityManager.close(); + } + } + + @Produces + @RequestScoped + public FacesContext produceFacesContext() + { + return FacesContext.getCurrentInstance(); + } + +} diff --git a/deltaspike/examples/jpa-examples/src/main/resources/META-INF/persistence.xml b/deltaspike/examples/jpa-examples/src/main/resources/META-INF/persistence.xml new file mode 100644 index 000000000..654ed562a --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,15 @@ + + + + java:jboss/datasources/ExampleDS + org.apache.deltaspike.example.Article + + + + + + + diff --git a/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/beans.xml b/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 000000000..e9f29b3d7 --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,23 @@ + + + + \ No newline at end of file diff --git a/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/faces-config.xml b/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/faces-config.xml new file mode 100644 index 000000000..8b8e36503 --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,40 @@ + + + + + /add.xhtml + + persisted + /list.xhtml + + + + + /list.xhtml + + deleted + /list.xhtml + + + + \ No newline at end of file diff --git a/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/web.xml b/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 000000000..76f7ba6a5 --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,43 @@ + + + + + + javax.faces.DEFAULT_SUFFIX + .xhtml + + + + Faces Servlet + javax.faces.webapp.FacesServlet + 1 + + + + Faces Servlet + *.xhtml + + + + index.xhtml + + \ No newline at end of file diff --git a/deltaspike/examples/jpa-examples/src/main/webapp/add.xhtml b/deltaspike/examples/jpa-examples/src/main/webapp/add.xhtml new file mode 100644 index 000000000..968a6ffc8 --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/webapp/add.xhtml @@ -0,0 +1,52 @@ + + + + + + + + + + + Simple JPA page + + + +

#{empty(articleController.article.id) ? 'Add New ' : 'Edit '}Article

+ + + + + + + + + + + + +
+ + \ No newline at end of file diff --git a/deltaspike/examples/jpa-examples/src/main/webapp/index.xhtml b/deltaspike/examples/jpa-examples/src/main/webapp/index.xhtml new file mode 100644 index 000000000..43da4e3b1 --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/webapp/index.xhtml @@ -0,0 +1,39 @@ + + + + + + + + Simple JPA page + + + +

Deltaspike JPA module examples

+ +
+ +
+
+ + \ No newline at end of file diff --git a/deltaspike/examples/jpa-examples/src/main/webapp/list.xhtml b/deltaspike/examples/jpa-examples/src/main/webapp/list.xhtml new file mode 100644 index 000000000..bdfd666f7 --- /dev/null +++ b/deltaspike/examples/jpa-examples/src/main/webapp/list.xhtml @@ -0,0 +1,64 @@ + + + + + + + + Simple JPA page + + + +

Article List

+ + + + + + ID + + + + title + + + + date + + + + operation + + + + #{' '} + + + + + + +
+ + \ No newline at end of file diff --git a/deltaspike/examples/pom.xml b/deltaspike/examples/pom.xml index cfbe3d171..677963c08 100644 --- a/deltaspike/examples/pom.xml +++ b/deltaspike/examples/pom.xml @@ -59,6 +59,7 @@ security-requested-page-after-login-cdi security-requested-page-after-login-picketlink scheduler-playground + jpa-examples @@ -94,6 +95,12 @@ ${deploy.skip} + + + org.wildfly.plugins + wildfly-maven-plugin + 1.0.1.Final + - \ No newline at end of file + From 6103b67c104616a493e6d447081aa33f163e3412 Mon Sep 17 00:00:00 2001 From: subaochen Date: Fri, 16 Sep 2016 16:39:27 +0800 Subject: [PATCH 2/2] redirect to list.xhtml when article saved --- .../jpa-examples/src/main/webapp/WEB-INF/faces-config.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/faces-config.xml b/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/faces-config.xml index 8b8e36503..ca5f7c230 100644 --- a/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/faces-config.xml +++ b/deltaspike/examples/jpa-examples/src/main/webapp/WEB-INF/faces-config.xml @@ -26,6 +26,7 @@ persisted /list.xhtml +