Skip to content

Commit fb5a969

Browse files
author
Tomáš Kraus
committed
Data MicroProfile example.
Signed-off-by: Tomáš Kraus <[email protected]>
1 parent becda59 commit fb5a969

File tree

30 files changed

+492
-1309
lines changed

30 files changed

+492
-1309
lines changed

etc/checkstyle-suppressions.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@
4545
files="examples/openapi-tools/"/>
4646
<suppress checks="MethodName"
4747
files="examples/data/"/>
48+
<suppress checks="MethodName"
49+
files="examples/declarative/data/"/>
50+
<suppress checks="MethodName"
51+
files="examples/microprofile/data/"/>
4852
</suppressions>

examples/data/mysql/src/main/java/io/helidon/examples/data/mysql/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
/**
17-
* This package contains the main application classes for demonstrating the usage of Helidon Data
17+
* This package contains an application classes for demonstrating the usage of Helidon Data
1818
* with a MySQL database.
1919
* <p>
2020
* The application provides REST services for managing pokémon data.

examples/data/oracle/src/main/java/io/helidon/examples/data/oracle/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
/**
17-
* This package contains the main application classes for demonstrating the usage of Helidon Data
17+
* This package contains an application classes for demonstrating the usage of Helidon Data
1818
* with an Oracle database.
1919
* <p>
2020
* The application provides REST services for managing pokémon data.

examples/declarative/data/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ Helidon Data SE Declarative Example
44
This example demonstrates a Java SE declarative application that utilizes Helidon Data, WebServer,
55
and a MySQL database.
66

7-
There are 3 repository interfaces in the example:
8-
97
There are 2 repository interfaces in the example:
108

119
- `PokemonRepository`
@@ -32,8 +30,8 @@ docker run --name mysql \
3230
### Database Schema and Content
3331

3432
The application's Jakarta Persistence API implementation automatically creates the database schema
35-
using the `resources/init.sql` script. The schema consists of three main entities: `Pet`, `Owner`
36-
and `Breed`. The initialization script populates the database with a basic set of records.
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.
3735

3836
## Build and Run
3937

examples/declarative/data/src/main/java/io/helidon/examples/declarative/data/package-info.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
*/
1616

1717
/**
18-
* This package contains the main application classes for demonstrating the usage of Helidon Data
18+
* This package contains an application classes for demonstrating the usage of Helidon Data
1919
* in a Java SE declarative application.
2020
* <p>
21-
* The application provides REST services for managing pet, owner, and breed data.
21+
* The application provides REST services for managing pokémon data.
2222
* <p>
2323
* The main entry point for the application is the {@link io.helidon.examples.declarative.data.Main} class.
2424
*
25-
* @see io.helidon.examples.declarative.data.Main
26-
* @see io.helidon.examples.declarative.data.service
25+
* @see io.helidon.examples.declarative.data.PokemonService
26+
* @see io.helidon.examples.declarative.data.PokemonRepository
27+
* @see io.helidon.examples.declarative.data.TypeRepository
2728
*/
2829
package io.helidon.examples.declarative.data;
Lines changed: 19 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
Helidon Data MP Example
22
----
33

4-
This example demonstrates a Java MP application that utilizes Helidon Data, WebServer, Hikari
4+
This example demonstrates a Java MicroProfile application that utilizes Helidon Data, WebServer, Hikari
55
connection pool DataSource and MySQL database.
66

7-
There are 3 repository interfaces in the example:
7+
There are 2 repository interfaces in the example:
88

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.
9+
- `PokemonRepository`
10+
- `TypeRepository`
1811

1912
> **NOTE:** Database tables are initialized with ID auto increment to supply primary key values
2013
> by the database. MySQL database default String comparisons are case-insensitive.
@@ -27,7 +20,7 @@ configuration using the following Docker command:
2720
```shell
2821
docker run --name mysql \
2922
-p 3306:3306 \
30-
-e MYSQL_DATABASE='pets' \
23+
-e MYSQL_DATABASE='pokemons' \
3124
-e MYSQL_RANDOM_ROOT_PASSWORD='yes' \
3225
-e MYSQL_USER='user' \
3326
-e MYSQL_PASSWORD='changeit' \
@@ -36,10 +29,9 @@ docker run --name mysql \
3629

3730
### Database Schema and Content
3831

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.
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.
4335

4436
## Build and Run
4537

@@ -59,97 +51,29 @@ java -jar target/helidon-examples-microprofile-data.jar
5951
6052
## Test Example
6153

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-
```
54+
The application provides `http://localhost:8080/pokemon` endpoint.
9255

93-
**Retrieve a Pet by name (`Max`):**
56+
**List all pokémons:**
9457
```shell
95-
curl http://localhost:8080/pet/get/Max
58+
curl http://localhost:8080/pokemon/all
9659
```
9760

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:**
61+
**List all normal type pokémons:**
13862
```shell
139-
curl http://localhost:8080/breed
63+
curl http://localhost:8080/pokemon/type/Normal
14064
```
14165

142-
**List all breeds:**
66+
**Retrieve a pokémon by name (`Meowth`):**
14367
```shell
144-
curl http://localhost:8080/breed/all
68+
curl http://localhost:8080/pokemon/get/Meowth
14569
```
14670

147-
**Insert new breed:**
71+
**Insert new pokémon:**
14872
```shell
149-
curl -i -X POST http://localhost:8080/breed/Hamster
73+
curl -i -X POST -H 'Content-type: application/json' -d '{"name":"Charmander","type":"Fire"}' http://localhost:8080/pokemon
15074
```
15175

152-
**Delete existing breed by ID (`10`):**
76+
**Delete existing pokémon by ID (`20`):**
15377
```shell
154-
curl -i -X DELETE http://localhost:8080/breed/10
78+
curl -i -X DELETE http://localhost:8080/pokemon/20
15579
```

examples/microprofile/data/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
<artifactId>helidon-logging-jul</artifactId>
6868
<scope>runtime</scope>
6969
</dependency>
70-
<!-- TODO: Replace with helidon support -->
7170
<dependency>
7271
<groupId>org.hibernate.orm</groupId>
7372
<artifactId>hibernate-core</artifactId>
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.microprofile.data;
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)