Questions tagged [hibernate-session]

A main Java interface to Hibernate persistence service.

A main Java interface to Hibernate persistence service. The Hibernate Session embodies the concept of a persistence service (or persistence manager) that can be used to query and perform insert, update, and delete operations on instances of a class mapped by Hibernate. Instances may exist in one of three states:

  • transient (never persistent, not associated with any Session)
  • persistent (associated with a unique Session)
  • detached (previously persistent, not associated with any Session)

The ORM tool allows to perform all of these interactions using object-oriented semantics by no longer referring to tables and columns, but using Java classes and object properties. As its name implies, the Session is a short-lived, lightweight object used as a bridge during a conversation between the application and the database. The Session wraps the underlying JDBC connection or Java EE data source, and it serves as a first-level cache for persistent objects bound to it.

The Session object is not intended to be thread-safe, instead a new/current Session should be obtained from the session factory for each thread/transaction.

A Session instance is serializable if its persistent classes are serializable.

126 questions
89
votes
8 answers

Hibernate: Difference between session.get and session.load

From the API, I could see it has something to do with proxy. But I couldn't find a lot of information on proxy and do not understand the difference between calling session.get and session.load. Could someone please explain or direct me to a…
tomato
  • 5,644
  • 13
  • 43
  • 48
23
votes
1 answer

Spring Transactions and hibernate.current_session_context_class

I have a Spring 3.2 application that uses Hibernate 4 and Spring Transactions. All the methods were working great and I could access correctly the database to save or retrieve entities. Then, I introduced some multithreading, and since each thread…
user1781028
  • 1,478
  • 4
  • 22
  • 45
21
votes
4 answers

Is Hibernate's session thread safe?

I need to know, whether the Hibernate's session is thread safe or not. But obvious a new session is attached to every thread for execution. But my question is if in one thread I have updated some value of an entity, so will that be reflected in…
M.J.
  • 16,266
  • 28
  • 75
  • 97
21
votes
4 answers

How to flush data into db inside active spring transaction?

I want to test hibernate session's save() method using spring testing framework. @Test method is : @Test @Transactional public void testSave() { User expected = createUser(); getGenericDao().currentSession().save(expected); User actual =…
18
votes
4 answers

How to I bind a Hibernate Session to a thread in Grails?

I'm writing a multi-threaded application in Grails and the additional threads need access to GORM/Hibernate. When they try to access GORM I get the error "org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does…
Fletch
  • 4,829
  • 2
  • 41
  • 55
11
votes
1 answer

using current_session_context_class property hibernate 3 hibernate 4

I have an application with Spring and Hibernate3 running well in production. Following is the config for session factory in Spring's applicationContext.xml
Shailesh Vaishampayan
  • 1,766
  • 5
  • 24
  • 52
9
votes
4 answers

Spring @Async: null hibernate session on LAZY collection

I'm using an @Async annotation on a service layer method. Everything works fine when I EAGERLY load @OneToMany collection fields, but when I try to access LAZY loaded element I found that Hibernate SessionImplementor object session is null. That…
gipinani
  • 14,038
  • 12
  • 56
  • 85
7
votes
8 answers

org.hibernate.service.UnknownServiceException: Unknown service requested

I am writing a unit test to for my AbstractHibernateRepository save method. I'm using spring test runner but I get the following exception when it runs: org.hibernate.service.UnknownServiceException: Unknown service requested…
user2054833
  • 2,115
  • 7
  • 24
  • 39
6
votes
3 answers

Hibernate first level cache - does it Sync?

I am aware of the fact that Session is first level cache used by Hibernate, and once we retrieve an entity from the session, the subsequent get calls for the same entity with same identifier is fetched from the session instead of DB, until the…
sanbhat
  • 17,522
  • 6
  • 48
  • 64
5
votes
3 answers

Listener on transaction start

I am looking for a clean solution to have a listener for transaction start. That means I would like the listener to be a bean (component) in the spring context that would receive an event on transaction start from TransactionPlatformManager or…
redhead
  • 1,264
  • 1
  • 17
  • 32
4
votes
2 answers

Good ideas for debugging Hibernate Session/Transaction errors?

I've used Hibernate for a little while now and have become accustomed to most of the common error messages. Most point me straight to the problem, but I've been having issues with this one: org.hibernate.NonUniqueObjectException: a different object…
RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
4
votes
1 answer

Are Session and EntityManager the same?

New to JPA and was wondering if Session (Hibernate) and EntityManager are the same thing or not? As both of them basically allow you to manage and search for entities in the relational database. Also any good source materials on the 2 would be…
user3837019
  • 211
  • 1
  • 2
  • 16
4
votes
1 answer

Grails 3.1 json rendering

I have just upgraded my application from grails 3.0.13 to grails 3.1.1. In doing so I got an interesting problem with JSON rendering. I have been using a custom JSON marshaler: JSON.registerObjectMarshaller(Appointment) { Appointment appointment -> …
Andréas K
  • 228
  • 1
  • 8
4
votes
1 answer

nullpointer exception in getcurrentsession

I am trying to integrate Hibernate 4.1 with Spring 3.1.3. //configuration file (beans.xml)
Maheshwaran K
  • 1,982
  • 5
  • 19
  • 22
4
votes
1 answer

hibernate session.get return null

I have such code: Session session = HibernateSessionFactory.sessionFactory.openSession(); System.out.println("------------------" + session.get(User.class, (long) 10)); System.out.println("------------------" +…
Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
1
2 3
8 9