Skip to content

Commit 2f99b52

Browse files
Tomáš Krausromain-grecourt
andauthored
[4.x] Helidon Data examples (#210)
* Data se Imperative example. Data se imperative example with Oracle DB and UCP DataSource. Data se declarative example. Data MicroProfile example. Used JSON-B in MicroProfile and SE declarative examples. * Remove oracle variation for examples/data to be aligned with examples/declarative/data and examples/microprofile/data Update dbclient pom files to set the groupId to be consistent with other examples * All data examples use the same setup. Signed-off-by: Tomáš Kraus <[email protected]> --------- Signed-off-by: Tomáš Kraus <[email protected]> Co-authored-by: Romain Grecourt <[email protected]>
1 parent 36b81da commit 2f99b52

File tree

48 files changed

+2434
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2434
-1
lines changed

etc/checkstyle-suppressions.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<!--
33
4-
Copyright (c) 2016, 2023 Oracle and/or its affiliates.
4+
Copyright (c) 2016, 2025 Oracle and/or its affiliates.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -43,4 +43,10 @@
4343
files="examples/openapi-tools/"/>
4444
<suppress checks="HideUtilityClassConstructor"
4545
files="examples/openapi-tools/"/>
46+
<suppress checks="MethodName"
47+
files="examples/data/"/>
48+
<suppress checks="MethodName"
49+
files="examples/declarative/data/"/>
50+
<suppress checks="MethodName"
51+
files="examples/microprofile/data/"/>
4652
</suppressions>

examples/data/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Helidon Data SE Imperative Example
2+
----
3+
4+
This example demonstrates a Java SE imperative application that utilizes Helidon Data, EclipseLink,
5+
WebServer, Hikari connection pool DataSource and MySQL database.
6+
7+
There are 2 repository interfaces in the example:
8+
9+
- `PokemonRepository`
10+
- `TypeRepository`
11+
12+
> **NOTE:** Database table `POKEMON` is initialized with ID auto increment to supply primary key values
13+
> by the database. MySQL database default String comparisons are case-insensitive.
14+
15+
## Start the Database
16+
17+
To run the application, a MySQL database is required. You can start the database with the necessary
18+
configuration using the following Docker command:
19+
20+
```shell
21+
docker run --name mysql \
22+
-p 3306:3306 \
23+
-e MYSQL_DATABASE='pokemons' \
24+
-e MYSQL_RANDOM_ROOT_PASSWORD='yes' \
25+
-e MYSQL_USER='user' \
26+
-e MYSQL_PASSWORD='changeit' \
27+
-d mysql
28+
```
29+
30+
### Database Schema and Content
31+
32+
The application's Jakarta Persistence API implementation automatically creates the database schema
33+
using the `resources/init.sql` script. The schema consists of two main entities: `Pokemon` and `Type`.
34+
The initialization script populates the database with a basic set of records.
35+
36+
## Build and Run
37+
38+
1. Build the application using Maven:
39+
40+
```shell
41+
mvn package
42+
```
43+
44+
2. Run the application:
45+
46+
```shell
47+
java -jar target/helidon-examples-data-mysql.jar
48+
```
49+
50+
> **NOTE:** The default username and password from this example should never be used in a production environment!
51+
52+
## Test Example
53+
54+
The application provides `http://localhost:8080/pokemon` endpoint.
55+
56+
**List all pokémons:**
57+
```shell
58+
curl http://localhost:8080/pokemon/all
59+
```
60+
61+
**List all normal type pokémons:**
62+
```shell
63+
curl http://localhost:8080/pokemon/type/Normal
64+
```
65+
66+
**Retrieve a pokémon by name (`Meowth`):**
67+
```shell
68+
curl http://localhost:8080/pokemon/get/Meowth
69+
```
70+
71+
**Insert new pokémon:**
72+
```shell
73+
curl -i -X POST -H 'Content-type: application/json' -d '{"name":"Charmander","type":"Fire"}' http://localhost:8080/pokemon
74+
```
75+
76+
**Delete existing pokémon by ID (`20`):**
77+
```shell
78+
curl -i -X DELETE http://localhost:8080/pokemon/20
79+
```

examples/data/pom.xml

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2025 Oracle and/or its affiliates.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>io.helidon.applications</groupId>
25+
<artifactId>helidon-se</artifactId>
26+
<version>4.3.0-SNAPSHOT</version>
27+
<relativePath/>
28+
</parent>
29+
<groupId>io.helidon.examples.data</groupId>
30+
<artifactId>helidon-examples-data-mysql</artifactId>
31+
<version>1.0.0-SNAPSHOT</version>
32+
<name>Helidon Examples Data</name>
33+
34+
<description>
35+
The example shows how to use Helidon Data with Helidon SE.
36+
</description>
37+
38+
<properties>
39+
<mainClass>io.helidon.examples.data.mysql.Main</mainClass>
40+
</properties>
41+
42+
<dependencies>
43+
<dependency>
44+
<groupId>io.helidon.config</groupId>
45+
<artifactId>helidon-config-yaml</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>io.helidon.data</groupId>
49+
<artifactId>helidon-data</artifactId>
50+
</dependency>
51+
<dependency>
52+
<groupId>io.helidon.data.sql.datasource</groupId>
53+
<artifactId>helidon-data-sql-datasource-hikari</artifactId>
54+
<scope>runtime</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>io.helidon.data.jakarta.persistence</groupId>
58+
<artifactId>helidon-data-jakarta-persistence</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>io.helidon.webserver</groupId>
62+
<artifactId>helidon-webserver</artifactId>
63+
</dependency>
64+
<dependency>
65+
<groupId>io.helidon.http.media</groupId>
66+
<artifactId>helidon-http-media-jsonp</artifactId>
67+
</dependency>
68+
<dependency>
69+
<groupId>io.helidon.logging</groupId>
70+
<artifactId>helidon-logging-jul</artifactId>
71+
<scope>runtime</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>jakarta.persistence</groupId>
75+
<artifactId>jakarta.persistence-api</artifactId>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.eclipse.persistence</groupId>
79+
<artifactId>org.eclipse.persistence.jpa</artifactId>
80+
<scope>runtime</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.eclipse.persistence</groupId>
84+
<artifactId>org.eclipse.persistence.core</artifactId>
85+
<scope>runtime</scope>
86+
</dependency>
87+
<dependency>
88+
<groupId>com.mysql</groupId>
89+
<artifactId>mysql-connector-j</artifactId>
90+
<scope>runtime</scope>
91+
</dependency>
92+
</dependencies>
93+
94+
<build>
95+
<plugins>
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<artifactId>maven-compiler-plugin</artifactId>
99+
<configuration>
100+
<annotationProcessorPaths>
101+
<path>
102+
<groupId>io.helidon.bundles</groupId>
103+
<artifactId>helidon-bundles-apt</artifactId>
104+
<version>${helidon.version}</version>
105+
</path>
106+
<path>
107+
<groupId>io.helidon.data.jakarta.persistence</groupId>
108+
<artifactId>helidon-data-jakarta-persistence-codegen</artifactId>
109+
<version>${helidon.version}</version>
110+
</path>
111+
</annotationProcessorPaths>
112+
</configuration>
113+
</plugin>
114+
<plugin>
115+
<groupId>org.apache.maven.plugins</groupId>
116+
<artifactId>maven-dependency-plugin</artifactId>
117+
<executions>
118+
<execution>
119+
<id>copy-libs</id>
120+
</execution>
121+
</executions>
122+
</plugin>
123+
</plugins>
124+
</build>
125+
126+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.examples.data.mysql;
17+
18+
import io.helidon.logging.common.LogConfig;
19+
import io.helidon.webserver.WebServer;
20+
21+
/**
22+
* The Main class serves as the entry point for the application.
23+
* It demonstrates the usage of Helidon Data in an SE imperative application.
24+
*/
25+
public class Main {
26+
27+
private Main() {
28+
throw new UnsupportedOperationException("Instances of Main class are not allowed");
29+
}
30+
31+
/**
32+
* Entry point for the SE application.
33+
*
34+
* @param args command-line arguments passed to the application
35+
*/
36+
public static void main(String... args) {
37+
LogConfig.configureRuntime();
38+
WebServer webServer = WebServer.builder()
39+
.port(8080)
40+
.routing(routing -> routing.register("/pokemon", new PokemonService()))
41+
.build();
42+
webServer.start();
43+
System.out.println("Server started on: http://localhost:" + webServer.port());
44+
}
45+
46+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.examples.data.mysql;
17+
18+
import jakarta.persistence.Column;
19+
import jakarta.persistence.Entity;
20+
import jakarta.persistence.GeneratedValue;
21+
import jakarta.persistence.GenerationType;
22+
import jakarta.persistence.Id;
23+
import jakarta.persistence.JoinColumn;
24+
import jakarta.persistence.ManyToOne;
25+
import jakarta.persistence.Table;
26+
27+
/**
28+
* Represents a pokémon entity with its associated properties.
29+
* This class is annotated with JPA annotations to map its properties to a database table.
30+
*/
31+
@Entity
32+
@Table(name = "POKEMON")
33+
public class Pokemon {
34+
35+
@Id
36+
@Column(name = "ID")
37+
@GeneratedValue(strategy = GenerationType.IDENTITY)
38+
private Integer id;
39+
@Column(name = "NAME", unique = true, nullable = false)
40+
private String name;
41+
@ManyToOne
42+
@JoinColumn(name = "TYPE_ID", nullable = false)
43+
private Type type;
44+
45+
/**
46+
* Constructs a new {@link Pokemon} instance with the specified values.
47+
*
48+
* @param name the name of the pokémon
49+
* @param type the type of the pokémon
50+
*/
51+
public Pokemon(String name, Type type) {
52+
this.id = null;
53+
this.name = name;
54+
this.type = type;
55+
}
56+
57+
/**
58+
* Constructs a new default {@link Pokemon} instance with default values.
59+
*/
60+
public Pokemon() {
61+
this(null, null);
62+
}
63+
64+
/**
65+
* Returns the unique identifier of the pokémon.
66+
*
67+
* @return the id of the pokémon
68+
*/
69+
public Integer getId() {
70+
return id;
71+
}
72+
73+
/**
74+
* Sets the unique identifier of the pokémon.
75+
*
76+
* @param id the new id of the pokémon
77+
*/
78+
public void setId(Integer id) {
79+
this.id = id;
80+
}
81+
82+
/**
83+
* Returns the name of the pokémon.
84+
*
85+
* @return the name of the pokémon
86+
*/
87+
public String getName() {
88+
return name;
89+
}
90+
91+
/**
92+
* Sets the name of the pokémon.
93+
*
94+
* @param name the new name of the pokémon
95+
*/
96+
public void setName(String name) {
97+
this.name = name;
98+
}
99+
100+
/**
101+
* Returns the type of the pokémon.
102+
*
103+
* @return the type of the pokémon
104+
*/
105+
public Type getType() {
106+
return type;
107+
}
108+
109+
/**
110+
* Sets the type of the pokémon.
111+
*
112+
* @param type the new type of the pokémon
113+
*/
114+
public void setType(Type type) {
115+
this.type = type;
116+
}
117+
118+
}

0 commit comments

Comments
 (0)