Using the new InterSystems IRIS Hibernate 6 Dialect for a Springboot Project
Hibernate is the most popular framework to do ORM (Object Relational Mapping) projects. With Hibernate a software can use the main DBMS in the market, including the capability to change the database vendor any time, without source code impact. This is possible because the Hibernate supports dialects. Each database product has a different dialect that can be assigned into a configuration file. So, if a software is using Oracle and is looking to evolve to InterSystems IRIS, just change the configuration file with connection and dialect information. If your software needs to be prepared to use database indicated by your client, the Hibernate is the solution to you.
Is there any InterSystems IRIS dialect for the new Hibernate 6?
Currently is not any official dialect to use IRIS with the new Hibernate 6. To resolve this issue, Dmitry Maslennikov proposed an idea in the excellent Ideas Portal (https://ideas.intersystems.com/ideas/DPI-I-372) and I implemented it.
If you do this tutorial, you will see the idea and this new dialect of IRIS in action and working.
What you need to do this tutorial
To do this tutorial you need:
1. An IRIS instance running (if you don’t have one, you can get it on https://openexchange.intersystems.com/package/ObjectScript).
2. Spring Tools installed (download it on https://spring.io/tools). Choose Eclipse version for this tutorial.
3. Java JDK version 17 (download it on https://jdk.java.net/archive/). Choose Java 17 for this tutorial.
4. All source code for this tutorial: https://github.com/yurimarx/iris-java-tools/tree/main/springboot-sample.
Tutorial steps
1. Open the Spring Tool Suite (STS) and choose a valid workspace path (any folder) and click Launch:
.png)
2. Click the Create new Spring Starter Project link:
.png)
3. This wizard will create a new Spring project. Fill the fields with these values:
• Service URL: https://start.spring.io
• Type: Maven (it is a package manager like the NPM, ZPM or IPM)
• Packaging: Jar (type of executable for the compiled project)
• Java Version: 17 or 20 (for this tutorial I selected version 17)
• Language: Java
• Group: com.tutorial (domain of the project for Maven)
• Artifact: iris-tutorial (name of the project for Maven)
• Version: 0.0.1-SNAPSHOT (version of the project for Maven)
• Description: IRIS Tutorial
• Package: com.tutorial.iris (root package for the project)
.png)
4. Click Next.
5. Choose the following dependencies for your project:
.png)
6. Click Finish to create your project.
7. Open your pom.xml file and include 2 new dependencies (for IRIS dialect and for IRIS JDBC driver) and 1 repository (necessary because the InterSystems IRIS JDBC driver is not published into a public maven repository).
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.1.1</version><relativePath /><!-- lookup parent from repository --></parent><groupId>com.tutorial</groupId><artifactId>tutorial-dialect</artifactId><version>0.0.1-SNAPSHOT</version><name>tutorial-dialect</name><description>Tutorial for IRIS Hibernate 6 Dialect</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-rest</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-hateoas</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-rest-hal-explorer</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>io.github.yurimarx</groupId><artifactId>hibernateirisdialect</artifactId><version>1.1.0</version></dependency><dependency><groupId>com.intersystems</groupId><artifactId>intersystems-jdbc</artifactId><version>3.7.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>InterSystems IRIS DC Git Repository</id><url>
https://github.com/intersystems-community/iris-driver-distribution/blob/main/JDBC/JDK18</url><snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots></repository></repositories></project>8. Go to the application.properties file (src > main > java > resources folder) and set connection and dialect properties with these values:
spring.datasource.username=_SYSTEMspring.datasource.url=jdbc:IRIS://localhost:1972/USERspring.datasource.password=SYSspring.jpa.properties.hibernate.default_schema=Examplespring.jpa.hibernate.ddl-auto=updatespring.datasource.driver-class-name=com.intersystems.jdbc.IRISDriverspring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=falsespring.jpa.database-platform=io.github.yurimarx.hibernateirisdialect.InterSystemsIRISDialectspring.jpa.show-sql=truespring.jpa.properties.hibernate.format_sql=true.png)
9. Create a new persistent class (click right button on project > New > Class):
.png)
10. Fill the following fields to create the class:
• Package: com.tutorial.iris.model
• Name: Product
.png)
11. Click Finish to create the class.
12. Develop the Product persistent class (class with values persisted in a SQL table) with this source code:
package com.tutorial.dialect.model;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
@Entity@Table(name = "Product")
publicclassProduct{
@Id@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private Double height;
private Double width;
private Double weight;
@Column(name="releasedate")
@Temporal(TemporalType.DATE)
@JsonFormat(pattern = "yyyy-MM-dd")
private Date releaseDate;
public Long getId(){
return id;
}
publicvoidsetId(Long id){
this.id = id;
}
public String getName(){
return name;
}
publicvoidsetName(String name){
this.name = name;
}
public String getDescription(){
return description;
}
publicvoidsetDescription(String description){
this.description = description;
}
public Double getHeight(){
return height;
}
publicvoidsetHeight(Double height){
this.height = height;
}
public Double getWidth(){
return width;
}
publicvoidsetWidth(Double width){
this.width = width;
}
public Double getWeight(){
return weight;
}
publicvoidsetWeight(Double weight){
this.weight = weight;
}
public Date getReleaseDate(){
return releaseDate;
}
publicvoidsetReleaseDate(Date releaseDate){
this.releaseDate = releaseDate;
}
}
13. Create an Interface repository for CRUD operations to the Product class (click right the project > New > Interface):
.png)
14. Fill the values for the interface and click Finish:
• Package: com.tutorial.iris.repository
• Name: ProductRepository
.png)
15. Click Finish to create the interface.
16. Develop the ProductRepository (a CRUD repository implements save, delete, find, find one and update functions) Interface (src > main > java > com > tutorial > dialect > repository folder ) with this source code:
package com.tutorial.dialect.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.tutorial.dialect.model.Product;
@RepositorypublicinterfaceProductRepositoryextendsCrudRepository<Product, Long> {
}17. Now, using the HAL browser from Springboot, is possible test CRUD functions in a web screen.
18. Be sure to run an IRIS instance on localhost, port 1972 with user _SYSTEM and password SYS (or change the application.properties for other connections values).
19. Run the application (click right the project > Run As > Spring boot app).
20. On the console you can see the log indicating the application start:
.png)
21. Go to your browser and type http://localhost:8080. See the HAL browser:
.png)
22. Click plus button for products endpoint to create a new product:
.png)
23. Fill the following values to create a new product and click Go button to confirm:
.png)
24. A new product was persisted:
.png)
Check the new row on IRIS (table product on USER namespace)
.png)
Test other operations, check in the database and enjoy!
I would like thanks @Dmitry Maslennikov for your advanced support and for help me to improve this new Dialect
Comments
Great initiative, @Yuri Marx ! Could you please publish the repo on OEX too?
I published on OEX: https://openexchange.intersystems.com/package/IRIS-Hibernate-Dialect
thank you, @Yuri Marx !
Thank you @Yuri Marx
for this great job!
Thanks guys
Hi @Yuri Marx,
Your video is available on InterSystems Developers YouTube channel:
⏯️ Using the new InterSystems IRIS Hibernate 6 Dialect for a Springboot Project
Please enjoy!
Great! Thanks