diff --git a/hibernate4/.factorypath b/hibernate4/.factorypath new file mode 100644 index 0000000000..16e422aead --- /dev/null +++ b/hibernate4/.factorypath @@ -0,0 +1,5 @@ + + + + + diff --git a/hibernate4/pom.xml b/hibernate4/pom.xml new file mode 100644 index 0000000000..2365e92ba2 --- /dev/null +++ b/hibernate4/pom.xml @@ -0,0 +1,340 @@ + + + 4.0.0 + + org.jboss.as.quickstart + jboss-as-hibernate4 + 7.0.2-SNAPSHOT + war + Java EE 6 webapp project + A starter Java EE 6 webapp project for use on JBoss AS 7 / EAP 6, generated from the jboss-javaee6-webapp archetype + + + + + UTF-8 + + 3.0.0.Beta1 + + + + + + + + + + org.jboss.spec + jboss-javaee-web-6.0 + ${javaee6.spec.version} + pom + import + + + + + + + + + + + javax.enterprise + cdi-api + provided + + + + + org.jboss.spec.javax.annotation + jboss-annotations-api_1.1_spec + provided + + + + + + + + org.jboss.spec.javax.ws.rs + jboss-jaxrs-api_1.1_spec + provided + + + + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + provided + + + + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.1_spec + provided + + + + + + + org.hibernate + hibernate-validator + 4.2.0.Final + provided + + + org.slf4j + slf4j-api + + + + + + + + + + + + + org.hibernate + hibernate-core + 4.0.0.Final + provided + + + + + junit + junit + 4.10 + test + + + + + + org.jboss.arquillian.junit + arquillian-junit-container + 1.0.0.CR7 + test + + + + org.jboss.arquillian.protocol + arquillian-protocol-servlet + 1.0.0.CR7 + test + + + + + + + ${project.artifactId} + + + + maven-compiler-plugin + 2.3.1 + + 1.6 + 1.6 + + + + maven-war-plugin + 2.1.1 + + + false + + + + + + org.jboss.as.plugins + jboss-as-maven-plugin + 7.1.0.CR1 + + + + + + + + + default + + true + + + + + maven-surefire-plugin + 2.4.3 + + true + + + + + + + + + + + + + jboss-public-repository + JBoss Repository + http://repository.jboss.org/nexus/content/groups/public + + + + + false + + + + + + + jboss-public-repository + JBoss Repository + http://repository.jboss.org/nexus/content/groups/public + + + + false + + + + + + + + arq-jbossas-managed + + + org.jboss.as + jboss-as-arquillian-container-managed + 7.1.0.CR1 + test + + + + + + + + + + + jboss-public-repository + JBoss Repository + http://repository.jboss.org/nexus/content/groups/public + + + + + false + + + + + + + jboss-public-repository + JBoss Repository + http://repository.jboss.org/nexus/content/groups/public + + + + false + + + + + + + arq-jbossas-remote + + + org.jboss.as + jboss-as-arquillian-container-remote + 7.1.0.CR1 + test + + + + + + + + + + openshift + + + + maven-war-plugin + 2.1.1 + + deployments + ROOT + + + + + + + + diff --git a/hibernate4/readme.html b/hibernate4/readme.html new file mode 100644 index 0000000000..ea6380a704 --- /dev/null +++ b/hibernate4/readme.html @@ -0,0 +1,63 @@ +

jboss-as-kitchensink

+

What is it?

+

This is a quickstart built upon kitchensink additionally demonstrating how to use Hibernate4 as ORM over JPA in AS7(or EAP6)! +It's a sample, deployable Maven 3 project to help you +get your foot in the door developing with Java EE 6 and Hibernate 4 on JBoss AS 7 or EAP 6. This +project is setup to allow you to create a compliant Java EE 6 application +using JSF 2.0, CDI 1.0, EJB 3.1, JPA 2.0 , Hibernate-Core and Hibernate Bean Validation. +It includes a persistence unit associated with Hibernate session and some sample persistence and transaction code +to help you get your feet wet with database access in enterprise Java.

+

System requirements

+

All you need to build this project is Java 6.0 (Java SDK 1.6) or better, Maven +3.0 or better.

+

The application this project produces is designed to be run on a JBoss AS 7 or EAP 6. +The following instructions target JBoss AS 7, but they also apply to JBoss EAP 6.

+

With the prerequisites out of the way, you're ready to build and deploy.

+

Adding correct Dependencies

+

JBoss AS7 (or EAP 6) provides both JPA as well as Hibernate4(as ORM) and also supports Hibernate3 etc. +If you choose to use Hibernate4 packaged within JBoss AS7(or EAP6) you will need to first import the JPA API. +This quickstart demonstrates usage of Hibernate Session and Hibernate Validators. +You will also need to add dependencies to the required Hibernate modules for using these features in pom.xml with scope as provided. +for eg . check dependency for hibernate-validator in pom.xml

+

Please note that if you are working with Hibernate 3 the process is different as you will need to bundle the jars since JBoss AS7(or EAP6) +does not ship with Hibernate 3. +Please refer to the quickstart demonstrating Hibernate3 for details on how to bundle the jars in such cases.

+

Deploying the application

+

First you need to start JBoss AS 7 (or EAP 6). To do this, run

+
$JBOSS_HOME/bin/standalone.sh
+
+

or if you are using windows

+
$JBOSS_HOME/bin/standalone.bat
+
+

To deploy the application, you first need to produce the archive to deploy using +the following Maven goal:

+
mvn package
+
+

You can now deploy the artifact to JBoss AS by executing the following command:

+
mvn jboss-as:deploy
+
+

This will deploy target/jboss-as-hibernate4.war.

+

The application will be running at the following URL http://localhost:8080/jboss-as-hibernate4/.

+

To undeploy from JBoss AS, run this command:

+
mvn jboss-as:undeploy
+
+

You can also start JBoss AS 7 and deploy the project using Eclipse. See the JBoss AS 7 +Getting Started Guide for Developers for more information.

+

Importing the project into an IDE

+

If you created the project using the Maven archetype wizard in your IDE +(Eclipse, NetBeans or IntelliJ IDEA), then there is nothing to do. You should +already have an IDE project.

+

Detailed instructions for using Eclipse with JBoss AS 7 are provided in the +JBoss AS 7 Getting Started Guide for Developers.

+

If you created the project from the commandline using archetype:generate, then +you need to import the project into your IDE. If you are using NetBeans 6.8 or +IntelliJ IDEA 9, then all you have to do is open the project as an existing +project. Both of these IDEs recognize Maven projects natively.

+

Downloading the sources and Javadocs

+

If you want to be able to debug into the source code or look at the Javadocs +of any library in the project, you can run either of the following two +commands to pull them into your local repository. The IDE should then detect +them.

+
mvn dependency:sources
+mvn dependency:resolve -Dclassifier=javadoc
+
diff --git a/hibernate4/readme.md b/hibernate4/readme.md new file mode 100644 index 0000000000..dc96b97e05 --- /dev/null +++ b/hibernate4/readme.md @@ -0,0 +1,111 @@ +hibernate4 +======================== + +What is it? +----------- + +This is a quickstart built upon kitchensink additionally demonstrating how to use Hibernate4 as ORM over JPA in AS7(or EAP6)! +It's a sample, deployable Maven 3 project to help you +get your foot in the door developing with Java EE 6 and Hibernate 4 on JBoss AS 7 or EAP 6. This +project is setup to allow you to create a compliant Java EE 6 application +using JSF 2.0, CDI 1.0, EJB 3.1, JPA 2.0 , Hibernate-Core and Hibernate Bean Validation. +It includes a persistence unit associated with Hibernate session and some sample persistence and transaction code +to help you get your feet wet with database access in enterprise Java. + +System requirements +------------------- + +All you need to build this project is Java 6.0 (Java SDK 1.6) or better, Maven +3.0 or better. + +The application this project produces is designed to be run on a JBoss AS 7 or EAP 6. +The following instructions target JBoss AS 7, but they also apply to JBoss EAP 6. + +With the prerequisites out of the way, you're ready to build and deploy. + + +Adding correct Dependencies +--------------------------- + +JBoss AS7 (or EAP 6) provides both JPA as well as Hibernate4(as ORM) and also supports Hibernate3 etc. +If you choose to use Hibernate4 packaged within JBoss AS7(or EAP6) you will need to first import the JPA API. +This quickstart demonstrates usage of Hibernate Session and Hibernate Validators. +You will also need to add dependencies to the required Hibernate modules for using these features in pom.xml with scope as provided. +for eg . + + + org.hibernate + hibernate-validator + 4.2.0.Final + provided + + + org.slf4j + slf4j-api + + + + +You may refer "Adding a new external dependency" located at http://community.jboss.org/wiki/HackingOnAS7 for further help on adding dependencies. + +Please note that if you are working with Hibernate 3 the process is different as you will need to bundle the jars since JBoss AS7(or EAP6) +does not ship with Hibernate 3. +Please refer to the quickstart demonstrating Hibernate3 for details on how to bundle the jars in such cases. + + +Deploying the application +------------------------- + +First you need to start JBoss AS 7 (or EAP 6). To do this, run + + $JBOSS_HOME/bin/standalone.sh + +or if you are using windows + + $JBOSS_HOME/bin/standalone.bat + +To deploy the application, you first need to produce the archive to deploy using +the following Maven goal: + + mvn package + +You can now deploy the artifact to JBoss AS by executing the following command: + + mvn jboss-as:deploy + +This will deploy `target/jboss-as-hibernate4.war`. + +The application will be running at the following URL . + +To undeploy from JBoss AS, run this command: + + mvn jboss-as:undeploy + +You can also start JBoss AS 7 and deploy the project using Eclipse. See the JBoss AS 7 +Getting Started Guide for Developers for more information. + +Importing the project into an IDE +================================= + +If you created the project using the Maven archetype wizard in your IDE +(Eclipse, NetBeans or IntelliJ IDEA), then there is nothing to do. You should +already have an IDE project. + +Detailed instructions for using Eclipse with JBoss AS 7 are provided in the +JBoss AS 7 Getting Started Guide for Developers. + +If you created the project from the commandline using archetype:generate, then +you need to import the project into your IDE. If you are using NetBeans 6.8 or +IntelliJ IDEA 9, then all you have to do is open the project as an existing +project. Both of these IDEs recognize Maven projects natively. + +Downloading the sources and Javadocs +==================================== + +If you want to be able to debug into the source code or look at the Javadocs +of any library in the project, you can run either of the following two +commands to pull them into your local repository. The IDE should then detect +them. + + mvn dependency:sources + mvn dependency:resolve -Dclassifier=javadoc diff --git a/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/controller/MemberRegistration.java b/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/controller/MemberRegistration.java new file mode 100644 index 0000000000..e616ce4282 --- /dev/null +++ b/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/controller/MemberRegistration.java @@ -0,0 +1,73 @@ +package org.jboss.as.quickstart.hibernate4.controller; + +import java.util.logging.Logger; + +import javax.annotation.PostConstruct; +import javax.ejb.Stateful; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Model; +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.inject.Named; +import javax.persistence.EntityManager; +import org.hibernate.Session; + +import org.jboss.as.quickstart.hibernate4.model.Member; + +/** + * @author Madhumita Sadhukhan + */ +// The @Stateful annotation eliminates the need for manual transaction demarcation +@Stateful +// The @Model stereotype is a convenience mechanism to make this a request-scoped bean that has an +// EL name +// Read more about the @Model stereotype in this FAQ: +// http://sfwk.org/Documentation/WhatIsThePurposeOfTheModelAnnotation +@Model +public class MemberRegistration { + + @Inject + private Logger log; + + @Inject + private EntityManager em; + + @Inject + private Event memberEventSrc; + + private Member newMember; + + @Produces + @Named + public Member getNewMember() { + return newMember; + } + + @Produces + public void register() throws Exception { + log.info("Registering " + newMember.getName()); + + //using Hibernate session(Native API) and JPA entitymanager + Session session = (Session)em.getDelegate(); + session.persist(newMember); + + try + { + memberEventSrc.fire(newMember); + } + catch(Exception e) + { + log.info("Registration Failed!You have provided duplicate details for Member!!!"); + e.printStackTrace(); + } + + + + initNewMember(); + } + + @PostConstruct + public void initNewMember() { + newMember = new Member(); + } +} diff --git a/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/data/MemberListProducer.java b/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/data/MemberListProducer.java new file mode 100644 index 0000000000..ebbbfe8495 --- /dev/null +++ b/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/data/MemberListProducer.java @@ -0,0 +1,51 @@ +package org.jboss.as.quickstart.hibernate4.data; + +import org.hibernate.Criteria; +import org.hibernate.Session; +import org.hibernate.criterion.Order; +import org.jboss.as.quickstart.hibernate4.model.Member; +import java.util.List; +import javax.annotation.PostConstruct; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Observes; +import javax.enterprise.event.Reception; + +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.inject.Named; +import javax.persistence.EntityManager; + +/** + * @author Madhumita Sadhukhan + */ + +@RequestScoped +public class MemberListProducer { + @Inject + private EntityManager em; + + private List members; + + // @Named provides access the return value via the EL variable name "members" in the UI (e.g., + // Facelets or JSP view) + @Produces + @Named + public List getMembers() { + return members; + } + + public void onMemberListChanged(@Observes(notifyObserver = Reception.IF_EXISTS) final Member member) { + retrieveAllMembersOrderedByName(); + } + + @PostConstruct + public void retrieveAllMembersOrderedByName() { + + //using Hibernate Session and Criteria Query via Hibernate Native API + Session session = (Session) em.getDelegate(); + Criteria cb = session.createCriteria(Member.class); + cb.addOrder(Order.asc("name")); + members = (List)cb.list(); + + } +} diff --git a/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/model/Member.java b/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/model/Member.java new file mode 100644 index 0000000000..f84828940e --- /dev/null +++ b/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/model/Member.java @@ -0,0 +1,97 @@ +package org.jboss.as.quickstart.hibernate4.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; +import javax.validation.constraints.Digits; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; +import javax.xml.bind.annotation.XmlRootElement; + +/*use hibernate4 specific validators + *please note for hibernate3 these imports will differ + *for eg import org.hibernate.validator.Email +*/ +import org.hibernate.validator.constraints.Email; +import org.hibernate.validator.constraints.NotEmpty; + +/*The Model uses JPA Entity as well as Hibernate Validators + * + */ + +@Entity +@XmlRootElement +@Table(name = "MemberHibernate4Demo",uniqueConstraints = @UniqueConstraint(columnNames = "id")) +public class Member implements Serializable { + /** Default value included to remove warning. Remove or modify at will. **/ + private static final long serialVersionUID = 1L; + + @Id + private Long id; + + @NotNull + @Size(min = 1, max = 25) + @Pattern(regexp = "[A-Za-z ]*", message = "must contain only letters and spaces") + private String name; + + /**using hibernate4 validators**/ + @NotNull + @NotEmpty + @Email + private String email; + + @NotNull + @Size(min = 9, max = 12) + @Digits(fraction = 0, integer = 12) + @Column(name = "phone_number") + private String phoneNumber; + + private String address; + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/util/Resources.java b/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/util/Resources.java new file mode 100644 index 0000000000..1626a2b71b --- /dev/null +++ b/hibernate4/src/main/java/org/jboss/as/quickstart/hibernate4/util/Resources.java @@ -0,0 +1,33 @@ +package org.jboss.as.quickstart.hibernate4.util; + +import java.util.logging.Logger; + +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +/** + * This class uses CDI to alias Java EE resources, such as the persistence context, to CDI beans + * + *

+ * Example injection on a managed bean field: + *

+ * + *
+ * @Inject
+ * private EntityManager em;
+ * 
+ */ +public class Resources { + // use @SuppressWarnings to tell IDE to ignore warnings about field not being referenced directly + @SuppressWarnings("unused") + @Produces + @PersistenceContext + private EntityManager em; + + @Produces + public Logger produceLog(InjectionPoint injectionPoint) { + return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName()); + } +} diff --git a/hibernate4/src/main/resources/META-INF/persistence.xml b/hibernate4/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..ea3fca0971 --- /dev/null +++ b/hibernate4/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,17 @@ + + + + + java:jboss/datasources/ExampleDS + + + + + + + \ No newline at end of file diff --git a/hibernate4/src/main/resources/import.sql b/hibernate4/src/main/resources/import.sql new file mode 100644 index 0000000000..95be160cd7 --- /dev/null +++ b/hibernate4/src/main/resources/import.sql @@ -0,0 +1,3 @@ +-- You can use this file to load seed data into the database using SQL statements +insert into MemberHibernate4Demo (id, name, email, phone_number, address) values (0, 'John Smith', 'john.smith@mailinator.com', '2125551212', 'Boston NY') +insert into MemberHibernate4Demo (id, name, email, phone_number, address) values (1, 'Madhumita Sadhukhan', 'msadhukh@gmail.com', '2135551214', 'Brno CZ') diff --git a/hibernate4/src/main/webapp/WEB-INF/beans.xml b/hibernate4/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000000..c782f570a2 --- /dev/null +++ b/hibernate4/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/hibernate4/src/main/webapp/WEB-INF/faces-config.xml b/hibernate4/src/main/webapp/WEB-INF/faces-config.xml new file mode 100644 index 0000000000..705006634f --- /dev/null +++ b/hibernate4/src/main/webapp/WEB-INF/faces-config.xml @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/hibernate4/src/main/webapp/WEB-INF/templates/default.xhtml b/hibernate4/src/main/webapp/WEB-INF/templates/default.xhtml new file mode 100644 index 0000000000..c505541265 --- /dev/null +++ b/hibernate4/src/main/webapp/WEB-INF/templates/default.xhtml @@ -0,0 +1,84 @@ + + + + Java EE 6 Starter Application + + + + +
+
+ + + [Template content will be inserted here] + +
+ +
+
+ diff --git a/hibernate4/src/main/webapp/index.html b/hibernate4/src/main/webapp/index.html new file mode 100644 index 0000000000..0c8f2aa8ba --- /dev/null +++ b/hibernate4/src/main/webapp/index.html @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/hibernate4/src/main/webapp/index.xhtml b/hibernate4/src/main/webapp/index.xhtml new file mode 100644 index 0000000000..f9847b1f71 --- /dev/null +++ b/hibernate4/src/main/webapp/index.xhtml @@ -0,0 +1,88 @@ + + + +

Welcome to JBoss AS 7!

+ +

You have successfully deployed a Java EE 6 web application + on JBoss AS 7.

+ + +

Register

+

Demonstrates quickstart application for Hibernate using Jboss AS7.

+ + + + + + + + + + + + + + + + + + + + + +
+
+
+

+ + +

+
+

Members

+ + No registered members. + + + + Id + #{_member.id} + + + Name + #{_member.name} + + + Email + #{_member.email} + + + Phone # + #{_member.phoneNumber} + + + Address # + #{_member.address} + + + +
+
diff --git a/hibernate4/src/main/webapp/resources/css/screen.css b/hibernate4/src/main/webapp/resources/css/screen.css new file mode 100644 index 0000000000..5e0fed859d --- /dev/null +++ b/hibernate4/src/main/webapp/resources/css/screen.css @@ -0,0 +1,40 @@ +body { + margin: 0; + padding: 0; + background-color: #EAECEE; + font-family: Verdana, sans-serif; + font-size: 0.9em; +} +#container { + margin: 0 auto; + padding: 0 20px 10px 20px; + border: 1px solid #666666; + width: 865px; /* subtract 40px from banner width for padding */ + background: #FFFFFF url(#{request.contextPath}/resources/gfx/banner.png) no-repeat; + padding-top: 110px; +} +#sidebar { + font-size: 0.9em; + width: 225px; + float: right; + border: 1px solid #666666; + background: #EAECEE; + padding: 0 15px 5px 15px; +} +#sidebar ul { + padding-left: 30px; +} +#footer { + clear: both; + text-align: center; + color: #666666; + font-size: 0.85em; + padding: 3em 0 0 0; +} +code { + font-size: 1.1em; +} +span.invalid { + padding-left: 3px; + color: red; +} diff --git a/hibernate4/src/main/webapp/resources/gfx/banner.png b/hibernate4/src/main/webapp/resources/gfx/banner.png new file mode 100644 index 0000000000..0b178ae699 Binary files /dev/null and b/hibernate4/src/main/webapp/resources/gfx/banner.png differ diff --git a/hibernate4/src/main/webapp/resources/gfx/logo.png b/hibernate4/src/main/webapp/resources/gfx/logo.png new file mode 100644 index 0000000000..b64d397a0d Binary files /dev/null and b/hibernate4/src/main/webapp/resources/gfx/logo.png differ