Skip to content

Commit d459209

Browse files
dreab8fax4ever
authored andcommitted
Message Board Hibernate Demo
1 parent 3b8dc33 commit d459209

File tree

32 files changed

+5567
-61
lines changed

32 files changed

+5567
-61
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Micro Service: Account
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.hibernate.demos.messageboard</groupId>
9+
<artifactId>message-board</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
<artifactId>account-service</artifactId>
13+
<packaging>war</packaging>
14+
15+
<properties>
16+
<version.hibernate.orm>5.2.13.Final</version.hibernate.orm>
17+
18+
<classifier.wildfly>wildfly-11-dist</classifier.wildfly>
19+
20+
<name.wildfly>wildfly-${version.wildfly}</name.wildfly>
21+
22+
<home.wildfly>${project.build.directory}/wildfly-${version.wildfly}</home.wildfly>
23+
</properties>
24+
25+
<dependencies>
26+
27+
<!-- Hibernate && JEE -->
28+
29+
<dependency>
30+
<groupId>org.hibernate</groupId>
31+
<artifactId>hibernate-core</artifactId>
32+
<version>${version.hibernate.orm}</version>
33+
<scope>provided</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.hibernate</groupId>
38+
<artifactId>hibernate-validator</artifactId>
39+
<scope>provided</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>javax.enterprise</groupId>
44+
<artifactId>cdi-api</artifactId>
45+
<scope>provided</scope>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.jboss.spec.javax.ejb</groupId>
50+
<artifactId>jboss-ejb-api_3.2_spec</artifactId>
51+
<scope>provided</scope>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.jboss.spec.javax.transaction</groupId>
56+
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
57+
<scope>provided</scope>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>org.jboss.spec.javax.ws.rs</groupId>
62+
<artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
63+
<scope>provided</scope>
64+
</dependency>
65+
66+
<!-- Log -->
67+
<dependency>
68+
<groupId>org.slf4j</groupId>
69+
<artifactId>slf4j-api</artifactId>
70+
<scope>provided</scope>
71+
</dependency>
72+
73+
<!-- Test -->
74+
<dependency>
75+
<groupId>org.hibernate.demos.messageboard</groupId>
76+
<artifactId>test-util</artifactId>
77+
<version>${project.version}</version>
78+
<scope>test</scope>
79+
</dependency>
80+
81+
<dependency>
82+
<groupId>junit</groupId>
83+
<artifactId>junit</artifactId>
84+
<scope>test</scope>
85+
</dependency>
86+
87+
<dependency>
88+
<groupId>org.jboss.arquillian.junit</groupId>
89+
<artifactId>arquillian-junit-container</artifactId>
90+
<scope>test</scope>
91+
</dependency>
92+
93+
<dependency>
94+
<groupId>org.wildfly.arquillian</groupId>
95+
<artifactId>wildfly-arquillian-container-managed</artifactId>
96+
<scope>test</scope>
97+
</dependency>
98+
99+
<dependency>
100+
<groupId>org.jboss.shrinkwrap</groupId>
101+
<artifactId>shrinkwrap-api</artifactId>
102+
<scope>test</scope>
103+
</dependency>
104+
105+
<dependency>
106+
<groupId>org.jboss.shrinkwrap.descriptors</groupId>
107+
<artifactId>shrinkwrap-descriptors-api-javaee</artifactId>
108+
<scope>test</scope>
109+
</dependency>
110+
111+
<dependency>
112+
<groupId>org.jboss.shrinkwrap.descriptors</groupId>
113+
<artifactId>shrinkwrap-descriptors-impl-javaee</artifactId>
114+
<scope>test</scope>
115+
</dependency>
116+
117+
<dependency>
118+
<groupId>com.h2database</groupId>
119+
<artifactId>h2</artifactId>
120+
<version>1.4.196</version>
121+
<scope>test</scope>
122+
</dependency>
123+
124+
</dependencies>
125+
126+
<build>
127+
<testResources>
128+
<testResource>
129+
<directory>src/test/resources</directory>
130+
<filtering>true</filtering>
131+
</testResource>
132+
</testResources>
133+
134+
<plugins>
135+
<plugin>
136+
<groupId>org.wildfly.build</groupId>
137+
<artifactId>wildfly-server-provisioning-maven-plugin</artifactId>
138+
<version>1.2.9.Final</version>
139+
<executions>
140+
<execution>
141+
<id>wildfly-server</id>
142+
<goals>
143+
<goal>build</goal>
144+
</goals>
145+
<phase>compile</phase>
146+
<configuration>
147+
<config-file>provisioning-wildfly.xml</config-file>
148+
<server-name>${name.wildfly}</server-name>
149+
</configuration>
150+
</execution>
151+
</executions>
152+
</plugin>
153+
<plugin>
154+
<artifactId>maven-dependency-plugin</artifactId>
155+
<version>3.0.1</version>
156+
<executions>
157+
<execution>
158+
<id>unpack</id>
159+
<phase>pre-integration-test</phase>
160+
<goals>
161+
<goal>unpack</goal>
162+
</goals>
163+
<configuration>
164+
<artifactItems>
165+
166+
<!-- Wildfly Application Server -->
167+
<artifactItem>
168+
<groupId>org.wildfly</groupId>
169+
<artifactId>wildfly-dist</artifactId>
170+
<version>${version.wildfly}</version>
171+
<type>zip</type>
172+
<overWrite>false</overWrite>
173+
<outputDirectory>${project.build.directory}</outputDirectory>
174+
</artifactItem>
175+
176+
<!-- Hibernate ORM Wildfly modules -->
177+
<artifactItem>
178+
<groupId>org.hibernate</groupId>
179+
<artifactId>hibernate-orm-modules</artifactId>
180+
<version>${version.hibernate.orm}</version>
181+
<classifier>${classifier.wildfly}</classifier>
182+
<type>zip</type>
183+
<overWrite>false</overWrite>
184+
<outputDirectory>${home.wildfly}/modules</outputDirectory>
185+
</artifactItem>
186+
187+
</artifactItems>
188+
</configuration>
189+
</execution>
190+
</executions>
191+
</plugin>
192+
193+
<plugin>
194+
<artifactId>maven-failsafe-plugin</artifactId>
195+
<version>2.20</version>
196+
<executions>
197+
<!-- Run arquillian integration test -->
198+
<execution>
199+
<id>run-embedded-wildfly-tests</id>
200+
<goals>
201+
<goal>integration-test</goal>
202+
<goal>verify</goal>
203+
</goals>
204+
<configuration>
205+
<redirectTestOutputToFile>false</redirectTestOutputToFile>
206+
<includes>
207+
<include>**/*IT.java</include>
208+
</includes>
209+
</configuration>
210+
</execution>
211+
</executions>
212+
</plugin>
213+
</plugins>
214+
</build>
215+
216+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<server-provisioning xmlns="urn:wildfly:server-provisioning:1.1">
2+
<feature-packs>
3+
4+
<feature-pack groupId="org.wildfly" artifactId="wildfly-feature-pack" version="${version.wildfly}" />
5+
6+
</feature-packs>
7+
</server-provisioning>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Hibernate OGM, Domain model persistence for NoSQL datastores
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.demo.message.account.core.cdi;
8+
9+
import javax.enterprise.context.ApplicationScoped;
10+
import javax.enterprise.inject.Produces;
11+
import javax.enterprise.inject.spi.InjectionPoint;
12+
import javax.persistence.EntityManager;
13+
import javax.persistence.PersistenceContext;
14+
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
18+
/**
19+
* Simple resource producer for default {@link EntityManager} and slf4j {@link Logger}.
20+
*
21+
* @author Fabio Massimo Ercoli
22+
*/
23+
@ApplicationScoped
24+
public class ResourcesProducer {
25+
26+
@Produces
27+
@PersistenceContext
28+
private EntityManager em;
29+
30+
@Produces
31+
private Logger produceLog(InjectionPoint injectionPoint) {
32+
return LoggerFactory.getLogger( injectionPoint.getMember().getDeclaringClass().getName());
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.demo.message.account.core.entity;
8+
9+
import javax.persistence.Column;
10+
import javax.persistence.Entity;
11+
import javax.persistence.GeneratedValue;
12+
import javax.persistence.GenerationType;
13+
import javax.persistence.Id;
14+
import javax.persistence.Table;
15+
16+
import org.hibernate.validator.constraints.NotEmpty;
17+
18+
/**
19+
* @author Andrea Boriero
20+
*/
21+
@Entity
22+
@Table(name = "USERS")
23+
public class User {
24+
25+
@Id
26+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
27+
private Long id;
28+
29+
@NotEmpty
30+
@Column(unique = true)
31+
private String userName;
32+
33+
/*
34+
JPA requires a default constructor, it can be private
35+
*/
36+
private User(){}
37+
38+
public User(String userName) {
39+
this.userName = userName;
40+
}
41+
42+
public Long getId() {
43+
return id;
44+
}
45+
46+
public String getUserName() {
47+
return userName;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.demo.message.account.core.repo;
8+
9+
import java.util.List;
10+
import javax.enterprise.context.ApplicationScoped;
11+
import javax.inject.Inject;
12+
import javax.persistence.EntityManager;
13+
import javax.persistence.Query;
14+
import javax.validation.Valid;
15+
16+
import org.hibernate.demo.message.account.core.entity.User;
17+
18+
/**
19+
* @author Andrea Boriero
20+
*/
21+
@ApplicationScoped
22+
public class UserRepo {
23+
24+
@Inject
25+
private EntityManager em;
26+
27+
public UserRepo() {
28+
}
29+
30+
public UserRepo(EntityManager em) {
31+
this.em = em;
32+
}
33+
34+
public void add(@Valid User user) {
35+
em.persist( user );
36+
}
37+
38+
public User findByUserName(String userName) {
39+
Query query = em.createQuery( "from User u where u.userName = :userName" );
40+
query.setParameter( "userName", userName );
41+
return (User) query.getSingleResult();
42+
}
43+
44+
public void delete(String userName) {
45+
Query query = em.createQuery( "delete from User u where u.userName = :userName" );
46+
query.setParameter( "userName", userName );
47+
query.executeUpdate();
48+
}
49+
50+
public void deleteAll() {
51+
Query query = em.createQuery( "delete from User" );
52+
query.executeUpdate();
53+
}
54+
55+
public List<User> findAll() {
56+
return em.createQuery( "from User u order by u.id" ).getResultList();
57+
}
58+
}

0 commit comments

Comments
 (0)