Using Hibernate with InterSystems IRIS
Get the basics of using Hibernate with InterSystems IRIS® data platform in this video series.
Hibernate is one of many third-party tools that you can use to connect your Java application to InterSystems IRIS in addition to the built-in technologies: JDBC, XEP, and Native API. Hibernate allows you to map your Java objects to your relational tables in InterSystems IRIS. If you already know how to use Hibernate, you can use it with InterSystems IRIS in the exact same way as with any other database. The important settings to know for connecting using Hibernate are the following:
- Driver class: com.intersystems.jdbc.IRISDriver
- Connection URL: jdbc:IRIS://IP:Port/NAMESPACE where IP, Port, and NAMESPACE are replaced by your values)
- Dialect: org.hibernate.dialect.InterSystemsIRISDialect
The videos below show:
- When and why to use Hibernate
- Connecting Hibernate to InterSystems IRIS and annotating your Java data classes
- Saving, retrieving from, and querying InterSystems IRIS with Hibernate
1. Introduction
2. Setting Up and Annotating
Code Used in the "Setting Up and Annotating" Video
Reference Timestamp Code Set the driver class connection property 1:32 <property name="connection.driver_class">com.intersystems.jdbc.IRISDriver</property>
Set the URL connection property 1:49 <property name="connection.url">jdbc:IRIS://[IP]:[Port]/[namespace]?useSSL=false</property>
Select the SQL dialect 2:06 <property name="dialect">org.hibernate.dialect.InterSystemsIRISDialect</property>
Set mapping classes to define properties and mappings to InterSystems IRIS tables 2:19 <mapping class="demo.Person"/>
<mapping class="demo.Address"/>
Properties in the Person class 2:31 private long id; private String firstname; private String lastname; private String phone; private Address address;
Import libraries within java.persistence
to add annotations3:02 import javax.persistence.*;
Annotate the Person class as an entity and map to the demo.person table 3:22 @Entity @Table (name = "demo.person")
Add annotation for the phone property 3:42 @Column(name="Phonenumber") public String getPhone() { return phone; }
Use the ManyToOne annotation to map the Address column 4:00 @ManyToOne @JoinColumn(name = "address") public Address getAddress() { return address; }
Specify the IDENTITY column 4:30 @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
3. Saving, Retrieving, and Querying
Code Used in the "Saving, Retrieving, and Querying" Video
Reference Timestamp Code Import the required Hibernate libraries 0:21 Begin code: import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.*; //import org.hibernate.query.Query; import org.hibernate.boot.MetadataSources;
End codeInstantiate a SessionFactory object to set up the application 0:39 SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Open a session from the SessionFactory object 1:04 Session session = sessionFactory.openSession();
Start a transaction 1:18 session.beginTransaction();
Save the Address and Employee objects 1:19 session.save(homeaddress); session.save(employee);
Commit changes and close the session 1:20 session.getTransaction().commit(); session.close();
Fetch employee from the person class with an ID of 1 1:54 long personID = 1; Person newPerson = sessionRead.get(Person.class, personID);
Print employee information to the screen 1:59 Begin code: System.out.printIn(""); System.out.printIn("----Displaying Data for Person with ID of 1----"); System.out.printIn("FirstName: " + newPerson.getFirstname()); System.out.printIn("LastName: " + newPerson.getLastname()); System.out.printIn("Phone: " + newPerson.getPhone()); System.out.printIn("Street: " + newPerson.getAddress().getStreet()); System.out.printIn("City: " + newPerson.getAddress().getCity()); System.out.printIn("State: " + newPerson.getAddress().getState());
End code
System.out.printIn("Zip: " + newPerson.getAddress().getZip());Close the sessionFactory object 2:10 sessionFactory.close();
Set query to use Hibernate Query Language 2:29 String hql = "FROM Person";
Pass HQL string in to the CreateQuery
method to create and prepare query2:36 TypedQuery<person> people = sessionFind.createQuery(hql, Person.class);
Run the query 2:55 List<Person> results = people.getResultList();
Loop through results and print name and address fields on individual lines 2:58 Begin code: for (listIterator<Person> it = results.listIterator(results.size()0; it.hasPrevious(); ) { Person p = it.previous() System.out.printIn(p.getFirstname() + " " + p.getLastname()); if (p.getAddress() !=null) { System.out.printIn(p.getAddress().getStreet()); System.out.printIn(p.getAddress().getCity() + " " + p.getAddress().GetState() + " " + p.getAddress().getZip()); System.out.printIn(""); } }
End codeMark me "complete" for these videos.