Skip to content

Commit 70607ad

Browse files
author
Tomáš Kraus
authored
Helidon Data examples. (#24)
* Helidon Data examples. Signed-off-by: Tomáš Kraus <[email protected]> * Java SE declarative example with Oracle DB and UCP DataSource. application.yaml contains properties to generate DDL scripts with EclipseLink 4.x Signed-off-by: Tomáš Kraus <[email protected]> * Removed unused files and properties. Signed-off-by: Tomáš Kraus <[email protected]> * MicroProfile example verified. Signed-off-by: Tomáš Kraus <[email protected]> * Java SE examples verified. Signed-off-by: Tomáš Kraus <[email protected]> * Fixed checkstyle failure: added suppresion for QbMN method name with '_' character. Signed-off-by: Tomáš Kraus <[email protected]> * README update. Signed-off-by: Tomáš Kraus <[email protected]> --------- Signed-off-by: Tomáš Kraus <[email protected]>
1 parent d860b30 commit 70607ad

Some content is hidden

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

71 files changed

+5011
-1
lines changed

apps/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
These are examples of Helidon applications that are more complex than what is typically found in the [Helidon Examples Repository](https://github.com/helidon-io/helidon-examples).
44

55
* [Chat Room using SSE](chatroom-sse/README.md)
6+
* [Helidon Data examples](data/README.md)
67
* [Todo app - Helidon + Coherence](../apps/todo)

apps/data/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Helidon Data Examples
2+
----
3+
4+
* [Java SE imperative application](se-imperative/README.md)
5+
* [Java SE declarative application](se-declarative/README.md)
6+
* [Java Microprofile application](mp/README.md)

apps/data/mp/README.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
Helidon Data MP Example
2+
----
3+
4+
This example demonstrates a Java MicroProfile application that utilizes Helidon Data, Hibernate,
5+
WebServer, Hikari connection pool DataSource and MySQL database.
6+
7+
There are 3 repository interfaces in the example:
8+
9+
- `OwnerRepository`
10+
- `BreedRepository`
11+
- `PetRepository`
12+
13+
All methods in `OwnerRepository` are defined as methods with queries defined by the @Data.Query
14+
annotation. There is no specific limitation on the names of those methods.
15+
16+
All methods in `BreedRepository` and `PetRepository` are defined as methods with queries defined
17+
by the method name. Method names must follow the _Query by Method Name_ grammar.
18+
19+
> **NOTE:** Database tables are initialized with ID auto increment to supply primary key values
20+
> by the database. MySQL database default String comparisons are case-insensitive.
21+
22+
## Start the Database
23+
24+
To run the application, a MySQL database is required. You can start the database with the necessary
25+
configuration using the following Docker command:
26+
27+
```shell
28+
docker run --name mysql \
29+
-p 3306:3306 \
30+
-e MYSQL_DATABASE='pets' \
31+
-e MYSQL_RANDOM_ROOT_PASSWORD='yes' \
32+
-e MYSQL_USER='user' \
33+
-e MYSQL_PASSWORD='changeit' \
34+
-d mysql
35+
```
36+
37+
### Database Schema and Content
38+
39+
The application's Jakarta Persistence API implementation automatically drops and creates the database
40+
schema using the `resources/drop.sql` and `resources/init.sql` scripts. The schema consists of three
41+
main entities: `Pet`, `Owner` and `Breed`. The initialization script populates the database with a basic
42+
set of records.
43+
44+
## Build and Run
45+
46+
1. Build the application using Maven:
47+
48+
```shell
49+
mvn package
50+
```
51+
52+
2. Run the application:
53+
54+
```shell
55+
java -jar target/helidon-labs-apps-data-mp.jar
56+
```
57+
58+
> **NOTE:** The default username and password from this example should never be used in a production environment!
59+
60+
## Test Example
61+
62+
The application provides the following endpoints:
63+
- http://localhost:8080/pet - Pet entity endpoint
64+
- http://localhost:8080/owner - Owner entity endpoint
65+
- http://localhost:8080/breed - Breed entity endpoint
66+
67+
### Pet Endpoint
68+
69+
**Retrieve Pet entity endpoint information:**
70+
```shell
71+
curl http://localhost:8080/pet
72+
```
73+
74+
**List all pets:**
75+
```shell
76+
curl http://localhost:8080/pet/all
77+
```
78+
79+
**List all pets as pages:**
80+
```shell
81+
curl http://localhost:8080/pet/all/0
82+
curl http://localhost:8080/pet/all/1
83+
curl http://localhost:8080/pet/all/2
84+
```
85+
86+
The last page will be empty with the initial set of `Pet` records. Additional records would fill it.
87+
88+
**List all dogs:**
89+
```shell
90+
curl http://localhost:8080/pet/breed/Dog
91+
```
92+
93+
**Retrieve a Pet by name (`Max`):**
94+
```shell
95+
curl http://localhost:8080/pet/get/Max
96+
```
97+
98+
**Insert new pet:**
99+
```shell
100+
curl -i -X POST -H 'Content-type: application/json' -d '{"name":"Ken","weight":3.5,"birth":"2023-05-10","owner":"Betty","breed":"Dog"}' http://localhost:8080/pet
101+
```
102+
103+
**Delete existing pet by ID (`20`):**
104+
```shell
105+
curl -i -X DELETE http://localhost:8080/pet/20
106+
```
107+
108+
### Owner Endpoint
109+
110+
**Retrieve Owner entity endpoint information:**
111+
```shell
112+
curl http://localhost:8080/owner
113+
```
114+
115+
**List all owners:**
116+
```shell
117+
curl http://localhost:8080/owner/all
118+
```
119+
120+
**List all names of owners who own a cat:**
121+
```shell
122+
curl http://localhost:8080/owner/names/Cat
123+
```
124+
125+
**Insert new owner:**
126+
```shell
127+
curl -i -X POST http://localhost:8080/owner/Alice
128+
```
129+
130+
**Delete existing owner by ID (`10`):**
131+
```shell
132+
curl -i -X DELETE http://localhost:8080/owner/10
133+
```
134+
135+
### Breed Endpoint
136+
137+
**Retrieve Breed entity endpoint information:**
138+
```shell
139+
curl http://localhost:8080/breed
140+
```
141+
142+
**List all breeds:**
143+
```shell
144+
curl http://localhost:8080/breed/all
145+
```
146+
147+
**Insert new breed:**
148+
```shell
149+
curl -i -X POST http://localhost:8080/breed/Hamster
150+
```
151+
152+
**Delete existing breed by ID (`10`):**
153+
```shell
154+
curl -i -X DELETE http://localhost:8080/breed/10
155+
```

apps/data/mp/pom.xml

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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-mp</artifactId>
26+
<version>4.3.0</version>
27+
<relativePath/>
28+
</parent>
29+
30+
<groupId>io.helidon.labs.apps.data</groupId>
31+
<artifactId>helidon-labs-apps-data-mp</artifactId>
32+
<version>1.0.0-SNAPSHOT</version>
33+
<name>Helidon Labs Apps Data Repository MP</name>
34+
35+
<description>
36+
The example shows how to use Helidon Data in MP application.
37+
</description>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>io.helidon.data</groupId>
42+
<artifactId>helidon-data</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>io.helidon.data.sql.datasource</groupId>
46+
<artifactId>helidon-data-sql-datasource-hikari</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.helidon.data.jakarta.persistence</groupId>
50+
<artifactId>helidon-data-jakarta-persistence</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>io.helidon.microprofile.bundles</groupId>
54+
<artifactId>helidon-microprofile</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>jakarta.transaction</groupId>
58+
<artifactId>jakarta.transaction-api</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>io.helidon.transaction</groupId>
62+
<artifactId>helidon-transaction-narayana</artifactId>
63+
<scope>runtime</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>io.helidon.logging</groupId>
67+
<artifactId>helidon-logging-jul</artifactId>
68+
<scope>runtime</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.hibernate.orm</groupId>
72+
<artifactId>hibernate-core</artifactId>
73+
<scope>runtime</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>com.mysql</groupId>
77+
<artifactId>mysql-connector-j</artifactId>
78+
<scope>runtime</scope>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.slf4j</groupId>
82+
<artifactId>slf4j-jdk14</artifactId>
83+
<scope>runtime</scope>
84+
</dependency>
85+
</dependencies>
86+
87+
<build>
88+
<plugins>
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-compiler-plugin</artifactId>
92+
<configuration>
93+
<annotationProcessorPaths>
94+
<path>
95+
<groupId>io.helidon.bundles</groupId>
96+
<artifactId>helidon-bundles-apt</artifactId>
97+
<version>${helidon.version}</version>
98+
</path>
99+
<path>
100+
<groupId>io.helidon.data.jakarta.persistence</groupId>
101+
<artifactId>helidon-data-jakarta-persistence-codegen</artifactId>
102+
<version>${helidon.version}</version>
103+
</path>
104+
</annotationProcessorPaths>
105+
</configuration>
106+
</plugin>
107+
<plugin>
108+
<groupId>org.apache.maven.plugins</groupId>
109+
<artifactId>maven-dependency-plugin</artifactId>
110+
<executions>
111+
<execution>
112+
<id>copy-libs</id>
113+
</execution>
114+
</executions>
115+
</plugin>
116+
<plugin>
117+
<groupId>io.smallrye</groupId>
118+
<artifactId>jandex-maven-plugin</artifactId>
119+
<executions>
120+
<execution>
121+
<id>make-index</id>
122+
</execution>
123+
</executions>
124+
</plugin>
125+
</plugins>
126+
</build>
127+
128+
</project>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.labs.apps.data.mp.model;
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.Table;
24+
25+
/**
26+
* A breed entity.
27+
*/
28+
@Entity
29+
@Table(name = "BREED")
30+
public class Breed {
31+
32+
@Id
33+
@Column(name = "ID")
34+
@GeneratedValue(strategy = GenerationType.IDENTITY)
35+
private Integer id;
36+
37+
@Column(name = "NAME", unique = true, nullable = false)
38+
private String name;
39+
40+
/**
41+
* Constructs a new {@link Breed} instance with the specified values.
42+
*
43+
* @param name the name of the breed
44+
*/
45+
public Breed(String name) {
46+
this.id = null;
47+
this.name = name;
48+
}
49+
50+
/**
51+
* Constructs a new default {@link Breed} instance with default values.
52+
*/
53+
public Breed() {
54+
this(null);
55+
}
56+
57+
/**
58+
* Returns the unique identifier of this breed.
59+
*
60+
* @return the id of the breed
61+
*/
62+
public Integer getId() {
63+
return id;
64+
}
65+
66+
/**
67+
* Sets the unique identifier of this breed.
68+
*
69+
* @param id the new id of the breed
70+
*/
71+
public void setId(Integer id) {
72+
this.id = id;
73+
}
74+
75+
/**
76+
* Returns the name of this breed.
77+
*
78+
* @return the name of the breed
79+
*/
80+
public String getName() {
81+
return name;
82+
}
83+
84+
/**
85+
* Sets the name of this breed.
86+
*
87+
* @param name the new name of the breed
88+
*/
89+
public void setName(String name) {
90+
this.name = name;
91+
}
92+
93+
}

0 commit comments

Comments
 (0)