Hibernate session examples in java

Hibernate для самых маленьких и не только

Доброго всем времени суток! При написании программы, которая так или иначе будет взаимодействовать с базой данных, пользуются разными средствами. Это и старый добрый jdbc, также применяют: EclipseLink,TopLink, iBatis (уже MyBatis), Spring Framework и конечно же герой нашей статьи — Hibernate. Конечно я здесь перечислил не все средства работы с базой данных, но постарался указать самые распространенные. В данной статье будет показано, как при помощи Hibernate вызывать хранимые процедуры, маппить как таблицы, так и запросы к классам. В качестве подопытной базы данных возьмем Oracle.
Подготовим все необходимое для экспериментов. Начнем с базы данных. Для начала создадим 3 таблички, над которыми мы и будем практиковаться.

CREATE TABLE book ( id NUMBER NOT NULL, name VARCHAR2 (100 BYTE) NOT NULL, description VARCHAR2 (1000 BYTE) NOT NULL, CONSTRAINT pk$book_id PRIMARY KEY (id) ) CREATE TABLE student ( id NUMBER NOT NULL, name VARCHAR2 (100 BYTE) NOT NULL, CONSTRAINT pk$student_id PRIMARY KEY (id) ) CREATE TABLE catalog ( id_student NUMBER NOT NULL, id_book NUMBER NOT NULL ) 

Теперь создадим функцию. которая будет нам возвращать название книги по ее id, пример глупый, но зато будет показывать принцип вызова функции с входным числовым параметром и выходным — строковым.

CREATE OR REPLACE FUNCTION get_book_name_by_id (p_id IN NUMBER) RETURN VARCHAR2 IS v_name VARCHAR2 (100); BEGIN SELECT name INTO v_name FROM book WHERE RETURN v_name; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN 'ну нет такой книги!!'; END; 

И также в качестве эксперимента создадим простую процедуру сохранения в БД, у которой будут как входные параметры, так и выходной.

CREATE OR REPLACE PROCEDURE save_book (p_id IN OUT NUMBER, p_name IN VARCHAR2, p_descr IN VARCHAR2) IS BEGIN IF p_id > 0 THEN UPDATE book SET name = p_name, description = p_descr WHERE ELSE SELECT catalog_seq.NEXTVAL INTO p_id FROM DUAL; INSERT INTO book VALUES (p_id, p_name, p_descr); END IF; END; 
@Entity @Table public class Student implements Serializable < private static final long serialVersionUID = -5170875020617735653L; @Id @GeneratedValue(strategy = GenerationType.AUTO, generator = "my_entity_seq_gen") @SequenceGenerator(name = "my_entity_seq_gen", sequenceName = "catalog_seq") private long id; @Column private String name; @OneToMany(mappedBy = "student", fetch = FetchType.LAZY) private SetbookList; // здесь идет реализация методов getter, setter, hashCode(), equals(), toString() > 
  • если у вас название класса не совпадает с названием таблицы, то например пишем так: Table(name = «STUDENT»);
  • одно из требований фреймворка — у каждой таблицы должен быть id, также если название поля id таблицы и название нашей переменной не совпадают, тогда аннотация будет выглядить так: Id @Column(name=«id таблицы в базе»);
  • аннотациями @GeneratedValue и @SequenceGenerator мы определяем стратегию генерации уникального идентификатора, в данном случае мы говорим, что при сохранении информации в базу данных, мы берем число из sequence с названием «catalog_seq»;
  • так как у нас один студент может иметь несколько книг, то отобразим это при помощи аннотации таким образом @OneToMany(mappedBy = «student», fetch = FetchType.LAZY), где mappedBy = «student» — это имя поля в классе Book(смотри ниже), а FetchType.LAZY — говорит нам о том, что коллекцию
@Entity @Table public class Book implements Serializable < private static final long serialVersionUID = 1L; @Id @Column(name="ID") @GeneratedValue(strategy = GenerationType.AUTO, generator = "my_entity_seq_gen") @SequenceGenerator(name = "my_entity_seq_gen", sequenceName = "catalog_seq") private long id; @Column(name="NAME",unique = true, nullable = false, length = 100) private String name; @Column(name="DESCRIPTION",unique = true, nullable = false, length = 100) private String description; @ManyToOne(fetch = FetchType.LAZY,optional=true) @JoinTable(name = "CATALOG", joinColumns = @JoinColumn(name = "ID_BOOK"), inverseJoinColumns = @JoinColumn(name = "ID_STUDENT")) private Student student; // здесь идет реализация методов getter, setter, hashCode(), equals(), toString() >
 List book = (List)session.createQuery("from Book order by name").list(); 
 List book = (List)session.createSQLQuery("select ID, DESCRIPTION, NAME from book order by NAME") .addScalar("id",Hibernate.LONG).addScalar("name").addScalar("description") .setResultTransformer(Transformers.aliasToBean(Book.class)).list(); 
 List book=(List)session.createCriteria(Book.class).createAlias("student", "st").add(Restrictions.eq("st.name", "Maxim")).list(); 
String bookName = (String)session.createSQLQuery("").setLong("id",1).uniqueResult(); 

Допустим у нас на сервере нет таблицы с названием Student, а есть только функция, которая возвращает курсор:

FUNCTION get_all_students RETURN SYS_REFCURSOR IS l_cur SYS_REFCURSOR; BEGIN OPEN l_cur FOR SELECT * FROM student ORDER BY 1; RETURN l_cur; END; 
@NamedNativeQuery(name="getAllStudent",query="", callable=true, resultClass=Student.class)
 List student = (List) session.getNamedQuery("entity").list();
CallableStatement st = session.connection().prepareCall(""); st.setLong(1,0); st.setString(2, "Золотой ключик, или Приключения Буратино"); st.setString(3,"повесть-сказка Алексея Николаевича Толстого"); st.registerOutParameter(1, java.sql.Types.NUMERIC); st.execute(); System.out.println(st.getLong(1)); 

Как Вы наверно успели заметить, при написании команд для обращения к базе данных и заполнением коллекции, использовалось слово session. В нашем случае это слово указывает на главный интерфейс между нашим Java-приложением и фреймворком Hibernate, то есть org.hibernate.Session session. Но для начала нам необходимо воспользоваться еще одним основополагающим и важным интерфейсом — SessionFactory. SessionFactory — это глобальная фабрика, ответственная за конкретную базу данных. Чтобы получить эту фабрику нам необходимо получить экземпляр класса org.hibernate.cfg.Configuration. Делается это так:

 SessionFactory sessions = new Configuration().configure().buildSessionFactory(); 

Где Configuration().configure().buildSessionFactory() парсит файлик с названием hibernate.cfg.xml, который находится рядом с вызываемой программой, конечно если не указан путь. Приведем файл конфигурации, в котором показана настройка соединения к базе данных и отображение наших таблиц:

     oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@localhost:port:baseName username password org.hibernate.dialect.Oracle10gDialect true   

И теперь, когда мы имеем фабрику, в которой есть вся необходимая конфигурация (коннект к базе через пул, маппинг/отображение таблиц и др.) мы можем работать с Session

Session session = sessions.openSession();

Работать с транзакциями можно так: session.beginTransaction(); и соответственно session.getTransaction().commit();
Вроде бы все. Конечно я не все смог осветить, но думаю для быстрого старта этого хватит.

Источник

Java Guides

A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.

Overview of Session Interface

The Session interface is the main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion of a persistence service.

The lifecycle of a Session is bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)

The main methods of the Session interface are to create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states:

  • transient − A new instance of a persistent class, which is not associated with a Session and has no representation in the database, and no identifier value is considered transient by Hibernate.
  • persistent − You can make a transient instance persistent by associating it with a Session. A persistent instance has a representation in the database, an identifier value, and is associated with a Session.
  • detached − Once we close the Hibernate Session, the persistent instance will become a detached instance.

Session Interface Methods

  1. Transaction beginTransaction() — Begin a unit of work and return the associated Transaction object.
  2. void cancelQuery() — Cancel the execution of the current query.
  3. void clear() — Completely clear the session.
  4. Connection close() — End the session by releasing the JDBC connection and cleaning up.
  5. Criteria createCriteria(Class persistentClass) — Create a new Criteria instance, for the given entity class, or a superclass of an entity class.
  6. Criteria createCriteria(String entityName) — Create a new Criteria instance, for the given entity name.
  7. Serializable getIdentifier(Object object) — Return the identifier value of the given entity as associated with this session.
  8. Query createFilter(Object collection, String queryString) — Create a new instance of Query for the given collection and filter string.
  9. Query createQuery(String queryString) — Create a new instance of Query for the given HQL query string.
  10. SQLQuery createSQLQuery(String queryString) — Create a new instance of SQLQuery for the given SQL query string.
  11. void delete(Object object) — Remove a persistent instance from the datastore.
  12. void delete(String entityName, Object object) — Remove a persistent instance from the datastore.
  13. Session get(String entityName, Serializable id) — Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.
  14. SessionFactory getSessionFactory() — Get the session factory which created this session.
  15. void refresh(Object object) — Re-read the state of the given instance from the underlying database.
  16. Transaction getTransaction() — Get the Transaction instance associated with this session.
  17. boolean isConnected() — Check if the session is currently connected.
  18. boolean isDirty() — Does this session contain any changes which must be synchronized with the database?
  19. boolean isOpen() — Check if the session is still open.
  20. Serializable save(Object object) — Persist the given transient instance, first assigning a generated identifier.
  21. void saveOrUpdate(Object object) — Either save(Object) or update(Object) the given instance.
  22. void update(Object object) — Update the persistent instance with the identifier of the given detached instance.
  23. void update(String entityName, Object object) — Update the persistent instance with the identifier of the given detached instance.

You can check out all Session interface methods at the official Javadoc at https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/Session.html

Important Session Interface Methods with Examples

  • Hibernate 5 — Save an Entity Example — In this article, we will create a simple Hibernate application to demonstrate how to save an entity into a database.
  • Hibernate 5 — Persist an Entity Example — In this article, we will create a simple Hibernate application to demonstrate how to persist an entity into a database.
  • Hibernate 5 — saveOrUpdate() Method Example — In this article, we will create a simple Hibernate application to demonstrate how to save or update an entity in the database using the saveOrUpdate() method.
  • Hibernate 5 — get(), load() and byId() Method Examples — In this article, we will show you how to use Session.get(), Session.load() and Session.byId() methods to retrieve an entity from database.
  • Hibernate 5 — merge() Example — In this article, we will show you how to use Session.merge() method to merge an entity in Hibernate Application.
  • Hibernate 5 — Delete or Remove an Entity Example — In Hibernate, an entity can be removed from a database by calling the Session.delete() or Session.remove(). Using these methods, we can remove a transient or persistent object from the datastore.
  • Hibernate 5 — load() Method Example — In this article, we will create a simple Hibernate application using Java configuration without using hibernate.cfg.xml to demonstrates the usage of Session.load() method.
  • Hibernate 5 c3p0 Connection Pool Example — In this article, we will show how to use c3p0 connection pooling in hibernate applications.

Key Points

  • A Session instance is serializable if its persistent classes are serializable.
  • If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs.

Источник

Читайте также:  Javascript объекты добавить свойство
Оцените статью