Tutorial - Develop IRIS using SSH
Tags: java, beginners, tutorial, jdbc
Published: 2023-04-16 09:15:52

SSH framework is a relatively representative framework of Java, which was popular many years ago. There are Struts+Spring+hibernate and Spring MVC+Spring+hibernate. Of course, I used the second one in college. If I can connect IRIS to Hibernate as a library, does it also mean that IRIS can be developed using SSH framework in theory?

*Tools and environment*

JDK 1.8

Maven

Hibernate 5.X.X

IRISHealth-2022.1.3

intellij idea

Windows 10 64

*Create database*

Create several tables in code mode


```
Class BKIP.SOA.MonitorScreen.CacheTable.logError Extends %Persistent
{
/// Service Overview Cache Table
Property SucNum As %String(MAXLEN = "");
Property failNum As %String(MAXLEN = "");
Property fdateTime As %String(MAXLEN = "");
}
```       


As shown in the figure:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asa7nemmki5u8zzf77sv.png)
 

Create a Spring project

The next step is to use IDEA to create the mapping of the library and create the entity class

File—New—Project....

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jn0gmz655le4kuo41r9g.png)
  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2u9vjyei3jwij9eduwv.png)


Name the project and select the jdk version，Click "Next"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/089azyiro0mmbo0q5hfv.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ga1l2xci4yxvyy126hub.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebt16eusrz5ulo9oc2u2.png)

Wait for maven to finish creating the project，As shown in the figure

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swzv85ohb3btpzm71zql.png)

Add required packages


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vntoqhzrzp2nkfs05yug.png)


```
<!--Introduce hibernate package-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.9.Final</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.1.2</version>
        </dependency>
```

File—Project Structure...

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qccxueszdkg2el9oolzy.png)


  Select Facets - add Hibernate

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4w25kb33t1mr62vy4jy.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/juxmei7tfv5b4oq2m9oc.png)

  Add the configuration file as shown in the figure


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1f5kbj1u6kfmu18a4vu.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49cscmp8gmxdgeb2ts61.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwcowa1bngnpv5m1al9u.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/74stjc81x5h3dkak5q1f.png)


  Jdbc package introducing iris


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4oivtqo838faiz4oyn1w.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9t6byzjygd9ie0wzfwh0.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cvfxtw3wdlutsrgrtnua.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ziz8qpq2sqpv2822uls.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fg2eyvrgr8gjlzswlvco.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4h449prm10ymcihhulm.png)

  Enter connection information in Hibernate configuration

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31ho9f5634mq26lxstlk.png)

  IDEA's database management tool connects to IRIS

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ifn8ryt9z7r2eqgog9k.png)


Introduction of drive package

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke18jco7jg8fnqetajzq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtdc0k8q42m3yx2j5c0z.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w69byht13j3bi99x4l1g.png)

  Connect to IRIS database

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xo96g6nmgh770xksnmzy.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ab5inalsu8jfp481vfj.png)

  Select the required database

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qcsu8t16kxcp8m2v096.png)


  Open Hibernate tool, View -- Persistence


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmupitepgk8xv11m9sbg.png)

  Open the mapping, as shown:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3qhlhadvxxzonjovjybs.png)

  Select Connect and enter the registration. Because the computer display is incomplete, select all.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/du38ho7abbu9qxo5h4fy.png)

  Next, click OK until you succeed:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqz67j5vwey4rikbmgl5.png)

  You get a bunch of entity classes:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hj5uy162oa7bgb0hjzt.png)


 Now that the entity class has been created, the rest of the logic of creating a session factory to add, delete, and check is not shown here. Share a tool class I used for reference only

```
package com.example.hibernate.utils;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

/**
 * <p>
 * Hibernate Tools
 * </p>
 *
 * @author wangzhe
 * @since 2017/3/9 14:42
 */
public class HibernateUtil {

   private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
   private static SessionFactory sessionFactory = null;

   static {
      try {
         Configuration cfg = new Configuration().configure();
         ServiceRegistry serviceRegistry = cfg.getStandardServiceRegistryBuilder().build();
         sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
      } catch (Exception e) {
         System.err.println("Failed to create session factory");
         e.printStackTrace();
      }
   }

   /**
    * Get Session
    *
    * @return Session
    * @throws HibernateException
    */
   public static Session getSession() throws HibernateException {
      Session session = (Session) threadLocal.get();
      if (session == null || !session.isOpen()) {
         if (sessionFactory == null) {
            rebuildSessionFactory();
         }
         session = (sessionFactory != null) ? sessionFactory.openSession() : null;
         threadLocal.set(session);
      }

      return session;
   }

   /**
    * Rebuild session factory
    */
   public static void rebuildSessionFactory() {
      try {
         Configuration cfg = new Configuration().configure();
         ServiceRegistry serviceRegistry = cfg.getStandardServiceRegistryBuilder().build();
         sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
      } catch (Exception e) {
         System.err.println("Failed to create session factory");
         e.printStackTrace();
      }
   }

   /**
    * Get SessionFactory object
    *
    * @return SessionFactory object
    */
   public static SessionFactory getSessionFactory() {
      return sessionFactory;
   }

   /**
    * Close Session
    *
    * @throws HibernateException
    */
   public static void closeSession() throws HibernateException {
      Session session = (Session) threadLocal.get();
      threadLocal.set(null);
      if (session != null) {
         session.close();
      }
   }
}
``` 

Conclusion:

The above is a step that I showed how to build an environment and continue to develop IRIS as a data source in combination with the SSH framework. Although the content shown is relatively simple, if I follow this way, in theory, I can use Java to develop and use IRIS, and I can use a series of native Java methods to make it more convenient for Java developers to use the powerful performance of IRIS and complete more businesses, At the same time, if you want to use SpringBoot or even SpringCloud to develop IRIS, it is not impossible. The key is the role of IRIS in this architecture.