Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
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).

* [Chat Room using SSE](chatroom-sse/README.md)
* [Helidon Data examples](data/README.md)
* [Todo app - Helidon + Coherence](../apps/todo)
6 changes: 6 additions & 0 deletions apps/data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Helidon Data Examples
----

* [Java SE imperative application](se-imperative/README.md)
* [Java SE declarative application](se-declarative/README.md)
* [Java Microprofile application](mp/README.md)
155 changes: 155 additions & 0 deletions apps/data/mp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
Helidon Data MP Example
----

This example demonstrates a Java MicroProfile application that utilizes Helidon Data, Hibernate,
WebServer, Hikari connection pool DataSource and MySQL database.

There are 3 repository interfaces in the example:

- `OwnerRepository`
- `BreedRepository`
- `PetRepository`

All methods in `OwnerRepository` are defined as methods with queries defined by the @Data.Query
annotation. There is no specific limitation on the names of those methods.

All methods in `BreedRepository` and `PetRepository` are defined as methods with queries defined
by the method name. Method names must follow the _Query by Method Name_ grammar.

> **NOTE:** Database tables are initialized with ID auto increment to supply primary key values
> by the database. MySQL database default String comparisons are case-insensitive.

## Start the Database

To run the application, a MySQL database is required. You can start the database with the necessary
configuration using the following Docker command:

```shell
docker run --name mysql \
-p 3306:3306 \
-e MYSQL_DATABASE='pets' \
-e MYSQL_RANDOM_ROOT_PASSWORD='yes' \
-e MYSQL_USER='user' \
-e MYSQL_PASSWORD='changeit' \
-d mysql
```

### Database Schema and Content

The application's Jakarta Persistence API implementation automatically drops and creates the database
schema using the `resources/drop.sql` and `resources/init.sql` scripts. The schema consists of three
main entities: `Pet`, `Owner` and `Breed`. The initialization script populates the database with a basic
set of records.

## Build and Run

1. Build the application using Maven:

```shell
mvn package
```

2. Run the application:

```shell
java -jar target/helidon-labs-apps-data-mp.jar
```

> **NOTE:** The default username and password from this example should never be used in a production environment!

## Test Example

The application provides the following endpoints:
- http://localhost:8080/pet - Pet entity endpoint
- http://localhost:8080/owner - Owner entity endpoint
- http://localhost:8080/breed - Breed entity endpoint

### Pet Endpoint

**Retrieve Pet entity endpoint information:**
```shell
curl http://localhost:8080/pet
```

**List all pets:**
```shell
curl http://localhost:8080/pet/all
```

**List all pets as pages:**
```shell
curl http://localhost:8080/pet/all/0
curl http://localhost:8080/pet/all/1
curl http://localhost:8080/pet/all/2
```

The last page will be empty with the initial set of `Pet` records. Additional records would fill it.

**List all dogs:**
```shell
curl http://localhost:8080/pet/breed/Dog
```

**Retrieve a Pet by name (`Max`):**
```shell
curl http://localhost:8080/pet/get/Max
```

**Insert new pet:**
```shell
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
```

**Delete existing pet by ID (`20`):**
```shell
curl -i -X DELETE http://localhost:8080/pet/20
```

### Owner Endpoint

**Retrieve Owner entity endpoint information:**
```shell
curl http://localhost:8080/owner
```

**List all owners:**
```shell
curl http://localhost:8080/owner/all
```

**List all names of owners who own a cat:**
```shell
curl http://localhost:8080/owner/names/Cat
```

**Insert new owner:**
```shell
curl -i -X POST http://localhost:8080/owner/Alice
```

**Delete existing owner by ID (`10`):**
```shell
curl -i -X DELETE http://localhost:8080/owner/10
```

### Breed Endpoint

**Retrieve Breed entity endpoint information:**
```shell
curl http://localhost:8080/breed
```

**List all breeds:**
```shell
curl http://localhost:8080/breed/all
```

**Insert new breed:**
```shell
curl -i -X POST http://localhost:8080/breed/Hamster
```

**Delete existing breed by ID (`10`):**
```shell
curl -i -X DELETE http://localhost:8080/breed/10
```
128 changes: 128 additions & 0 deletions apps/data/mp/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2025 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.applications</groupId>
<artifactId>helidon-mp</artifactId>
<version>4.3.0</version>
<relativePath/>
</parent>

<groupId>io.helidon.labs.apps.data</groupId>
<artifactId>helidon-labs-apps-data-mp</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Helidon Labs Apps Data Repository MP</name>

<description>
The example shows how to use Helidon Data in MP application.
</description>

<dependencies>
<dependency>
<groupId>io.helidon.data</groupId>
<artifactId>helidon-data</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.data.sql.datasource</groupId>
<artifactId>helidon-data-sql-datasource-hikari</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.data.jakarta.persistence</groupId>
<artifactId>helidon-data-jakarta-persistence</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.bundles</groupId>
<artifactId>helidon-microprofile</artifactId>
</dependency>
<dependency>
<groupId>jakarta.transaction</groupId>
<artifactId>jakarta.transaction-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.transaction</groupId>
<artifactId>helidon-transaction-narayana</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.helidon.logging</groupId>
<artifactId>helidon-logging-jul</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.helidon.bundles</groupId>
<artifactId>helidon-bundles-apt</artifactId>
<version>${helidon.version}</version>
</path>
<path>
<groupId>io.helidon.data.jakarta.persistence</groupId>
<artifactId>helidon-data-jakarta-persistence-codegen</artifactId>
<version>${helidon.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-libs</id>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.smallrye</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<executions>
<execution>
<id>make-index</id>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.labs.apps.data.mp.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

/**
* A breed entity.
*/
@Entity
@Table(name = "BREED")
public class Breed {

@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "NAME", unique = true, nullable = false)
private String name;

/**
* Constructs a new {@link Breed} instance with the specified values.
*
* @param name the name of the breed
*/
public Breed(String name) {
this.id = null;
this.name = name;
}

/**
* Constructs a new default {@link Breed} instance with default values.
*/
public Breed() {
this(null);
}

/**
* Returns the unique identifier of this breed.
*
* @return the id of the breed
*/
public Integer getId() {
return id;
}

/**
* Sets the unique identifier of this breed.
*
* @param id the new id of the breed
*/
public void setId(Integer id) {
this.id = id;
}

/**
* Returns the name of this breed.
*
* @return the name of the breed
*/
public String getName() {
return name;
}

/**
* Sets the name of this breed.
*
* @param name the new name of the breed
*/
public void setName(String name) {
this.name = name;
}

}
Loading
Loading