Skip to content

Commit ce16623

Browse files
author
Tomáš Kraus
committed
Helidon Data basics example.
Signed-off-by: Tomáš Kraus <[email protected]>
1 parent 8aa90a4 commit ce16623

File tree

13 files changed

+766
-0
lines changed

13 files changed

+766
-0
lines changed

examples/data/basics/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Helidon Data Basic Example
2+
----
3+
4+
This example shows the basics of using Helidon Data.
5+
6+
7+
## Start the Database
8+
9+
Application requires MySQL database to be running. Following shell script command starts the database
10+
with required setup in Docker container:
11+
12+
```shell
13+
docker run --name mysql \
14+
-p 3306:3306 \
15+
-e MYSQL_DATABASE='pets' \
16+
-e MYSQL_ROOT_PASSWORD='R00tP4ssw0rd' \
17+
-e MYSQL_USER='user' \
18+
-e MYSQL_PASSWORD='P4ssw0rd' \
19+
-d mysql
20+
```
21+
22+
## Build and Run
23+
24+
```shell
25+
mvn package
26+
java -jar target/helidon-examples-data-basics.jar
27+
```
28+
29+
> **NOTE:** Default username and password from this example shall never be used in production environment!

examples/data/basics/pom.xml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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-basics</artifactId>
31+
<version>1.0.0-SNAPSHOT</version>
32+
<name>Helidon Examples Data Repository Basics</name>
33+
34+
<description>
35+
The simplest example shows how to use Data Repository.
36+
</description>
37+
38+
<properties>
39+
<mainClass>io.helidon.examples.data.basics.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.jakarta.persistence</groupId>
53+
<artifactId>helidon-data-jakarta-persistence</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>jakarta.persistence</groupId>
57+
<artifactId>jakarta.persistence-api</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.eclipse.persistence</groupId>
61+
<artifactId>org.eclipse.persistence.jpa</artifactId>
62+
<scope>runtime</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.eclipse.persistence</groupId>
66+
<artifactId>org.eclipse.persistence.core</artifactId>
67+
<scope>runtime</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.mysql</groupId>
71+
<artifactId>mysql-connector-j</artifactId>
72+
<scope>runtime</scope>
73+
</dependency>
74+
</dependencies>
75+
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-compiler-plugin</artifactId>
81+
<configuration>
82+
<annotationProcessorPaths>
83+
<path>
84+
<groupId>io.helidon.bundles</groupId>
85+
<artifactId>helidon-bundles-apt</artifactId>
86+
<version>${helidon.version}</version>
87+
</path>
88+
<path>
89+
<groupId>io.helidon.data.jakarta.persistence</groupId>
90+
<artifactId>helidon-data-jakarta-persistence-codegen</artifactId>
91+
<version>${helidon.version}</version>
92+
</path>
93+
</annotationProcessorPaths>
94+
</configuration>
95+
</plugin>
96+
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-dependency-plugin</artifactId>
100+
<executions>
101+
<execution>
102+
<id>copy-libs</id>
103+
</execution>
104+
</executions>
105+
</plugin>
106+
</plugins>
107+
</build>
108+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.helidon.examples.data.basics;
2+
3+
import io.helidon.examples.data.basics.repository.OwnerRepository;
4+
import io.helidon.examples.data.basics.repository.PetRepository;
5+
import io.helidon.service.registry.Services;
6+
7+
/**
8+
* The Main class serves as the entry point for the application.
9+
* It demonstrates the usage of OwnerRepository and PetRepository.
10+
*/
11+
public class Main {
12+
13+
private Main() {
14+
throw new UnsupportedOperationException("Instances of Main class are not allowed");
15+
}
16+
17+
/**
18+
* Entry point for the SE application.
19+
*
20+
* @param args command-line arguments
21+
*/
22+
public static void main(String... args) {
23+
OwnerRepository ownerRepository = Services.get(OwnerRepository.class);
24+
PetRepository petRepository = Services.get(PetRepository.class);
25+
26+
// OwnerRepository methods usage:
27+
// - print list of Owner names
28+
System.out.println("All owners:");
29+
ownerRepository.listNameOrderByName()
30+
.forEach(name -> System.out.printf(" - name: %s%n", name));
31+
// - print list of Dog Owner names
32+
System.out.println("Dog owners:");
33+
ownerRepository.queryNameByPetBreed("Dog")
34+
.forEach(name -> System.out.printf(" - name: %s%n", name));
35+
36+
// PetRepository methods usage:
37+
// - print list of Cats
38+
System.out.println("Cats:");
39+
petRepository.listByBreed_Name("Cat")
40+
.forEach(pet -> System.out.printf(" - name: %s%n", pet.getName()));
41+
// - print list of pets owned by Betty
42+
System.out.println("Betty's pets:");
43+
petRepository.streamByOwner_Name("Betty")
44+
.forEach(pet -> System.out.printf(" - name: %s%n", pet.getName()));
45+
// - print information about Freya
46+
System.out.println("Freya:");
47+
petRepository.findByName("Freya")
48+
.ifPresentOrElse(pet -> {
49+
System.out.printf(" - name: %s%n", pet.getName());
50+
System.out.printf(" - weight: %.1f%n", pet.getWeight());
51+
System.out.printf(" - date of birth: %s%n", pet.getBirth());
52+
System.out.printf(" - breed: %s%n", pet.getBreed().getName());
53+
System.out.printf(" - owner: %s%n", pet.getOwner().getName());
54+
},
55+
() -> System.out.println("Freya was not found"));
56+
57+
}
58+
59+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.basics.model;
17+
18+
import jakarta.persistence.Entity;
19+
import jakarta.persistence.Id;
20+
import jakarta.persistence.Table;
21+
22+
/**
23+
* A breed entity.
24+
*/
25+
@Entity
26+
@Table(name = "BREED")
27+
public class Breed {
28+
29+
@Id
30+
private int id;
31+
32+
private String name;
33+
34+
/**
35+
* Creates an instance of {@link Breed} class with the specified id and name.
36+
*
37+
* @param id the primary key of the breed
38+
* @param name the name of the breed
39+
*/
40+
public Breed(int id, String name) {
41+
this.id = id;
42+
this.name = name;
43+
}
44+
45+
/**
46+
* Creates a default instance of {@link Breed} class with id set to {@code -1}
47+
* and name set to {@code null}.
48+
*/
49+
public Breed() {
50+
this(-1, null);
51+
}
52+
53+
/**
54+
* Returns the primary key of this {@link Breed}.
55+
*
56+
* @return the primary key
57+
*/
58+
public int getId() {
59+
return id;
60+
}
61+
62+
/**
63+
* Sets the primary key of this {@link Breed}.
64+
*
65+
* @param id the new primary key
66+
*/
67+
public void setId(int id) {
68+
this.id = id;
69+
}
70+
71+
/**
72+
* Returns the name of this {@link Breed}.
73+
*
74+
* @return the name of the breed
75+
*/
76+
public String getName() {
77+
return name;
78+
}
79+
80+
/**
81+
* Sets the name of this {@link Breed}.
82+
*
83+
* @param name the new name of the breed
84+
*/
85+
public void setName(String name) {
86+
this.name = name;
87+
}
88+
89+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.basics.model;
17+
18+
import jakarta.persistence.Entity;
19+
import jakarta.persistence.Id;
20+
import jakarta.persistence.Table;
21+
22+
/**
23+
* An owner entity.
24+
*/
25+
@Entity
26+
@Table(name = "OWNER")
27+
public class Owner {
28+
29+
@Id
30+
private int id;
31+
32+
private String name;
33+
34+
/**
35+
* Creates an instance of {@link Owner} class with the specified id and name.
36+
*
37+
* @param id the primary key of the owner
38+
* @param name the name of the owner
39+
*/
40+
public Owner(int id, String name) {
41+
this.id = id;
42+
this.name = name;
43+
}
44+
45+
/**
46+
* Creates a default instance of {@link Owner} class with id set to {@code -1}
47+
* and name set to {@code null}.
48+
*/
49+
public Owner() {
50+
this(-1, null);
51+
}
52+
53+
/**
54+
* Returns the primary key of this {@link Owner}.
55+
*
56+
* @return the primary key
57+
*/
58+
public int getId() {
59+
return id;
60+
}
61+
62+
/**
63+
* Sets the primary key of this {@link Owner}.
64+
*
65+
* @param id the new primary key
66+
*/
67+
public void setId(int id) {
68+
this.id = id;
69+
}
70+
71+
/**
72+
* Returns the name of this {@link Owner}.
73+
*
74+
* @return the name of the owner
75+
*/
76+
public String getName() {
77+
return name;
78+
}
79+
80+
/**
81+
* Sets the name of this {@link Owner}.
82+
*
83+
* @param name the new name of the owner
84+
*/
85+
public void setName(String name) {
86+
this.name = name;
87+
}
88+
89+
}

0 commit comments

Comments
 (0)