Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3627346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:52:36+00:00 2026-05-18T23:52:36+00:00

According to the tutorial : http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=27hibernateloadvshibernateget , If you initialize a JavaBean instance with

  • 0

According to the tutorial : http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=27hibernateloadvshibernateget,

If you initialize a JavaBean instance with a load method call, you can only access the properties of that JavaBean, for the first time, within the transactional context in which it was initialized.

If you try to access the various properties of the JavaBean after the transaction that loaded it has been committed, you’ll get an exception, a LazyInitializationException, as Hibernate no longer has a valid transactional context to use to hit the database.

But with my experiment, using hibernate 3.6, and postgres 9, it doesnt throw any exception at all. Am i missing something ?

Here’s my code :

import org.hibernate.Session;


public class AppLoadingEntities {
    /**
    * @param args
    */
    public static void main(String[] args) {
        HibernateUtil.beginTransaction();
        Session session = HibernateUtil.getSession();
        
        EntityUser userFromGet = get(session);
        EntityUser userFromLoad = load(session);
        
        // finish the transaction
        session.getTransaction().commit();
        
        // try fetching field value from entity bean that is fetched via get outside transaction, and it'll be okay
        System.out.println("userFromGet.getId() : " + userFromGet.getId());
        System.out.println("userFromGet.getName() : " + userFromGet.getName());
        
        // fetching field from entity bean that is fetched via load outside transaction, and it'll be errornous
        // NOTE : but after testing, load seems to be okay, what gives ? ask forums
        try {
            System.out.println("userFromLoad.getId() : " + userFromLoad.getId());
            System.out.println("userFromLoad.getName() : " + userFromLoad.getName());
        } catch(Exception e) {
            System.out.println("error while fetching entity that is fetched from load : " + e.getMessage());
        }
    }

    private static EntityUser load(Session session) {
        EntityUser user = (EntityUser) session.load(EntityUser.class, 1l);
        return user;
    }

    private static EntityUser get(Session session) {
        // safe to set it to 1, coz the table got recreated at every run of this app
        EntityUser user = (EntityUser) session.get(EntityUser.class, 1l);
        return user;
    }

}

And here’s the output :

82 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
87 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.0.Final
87 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
90 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
92 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
132 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
132 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
172 [main] WARN org.hibernate.util.DTDEntityResolver - recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
189 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
228 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: EntityUser
254 [main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity EntityUser on table MstUser
285 [main] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
287 [main] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
291 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
291 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
291 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
300 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost:5432/hibernate
300 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=sofco, password=****}
369 [main] INFO org.hibernate.cfg.SettingsFactory - Database ->
       name : PostgreSQL
    version : 9.0.1
      major : 9
      minor : 0
369 [main] INFO org.hibernate.cfg.SettingsFactory - Driver ->
       name : PostgreSQL Native Driver
    version : PostgreSQL 9.0 JDBC4 (build 801)
      major : 9
      minor : 0
386 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.PostgreSQLDialect
395 [main] INFO org.hibernate.engine.jdbc.JdbcSupportLoader - Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
396 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
397 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
397 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
397 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
397 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
397 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
397 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
397 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
397 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
398 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
398 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
398 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
398 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
398 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
398 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
399 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
399 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
399 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
399 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
399 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
399 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
399 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
401 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
402 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
402 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
402 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
402 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
402 [main] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
424 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
548 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Hibernate: select entityuser0_.id as id0_0_, entityuser0_.name as name0_0_, entityuser0_.password as password0_0_ from MstUser entityuser0_ where entityuser0_.id=?
Hibernate: select entityuser0_.id as id0_0_, entityuser0_.name as name0_0_, entityuser0_.password as password0_0_ from MstUser entityuser0_ where entityuser0_.id=?
userFromGet.getId() : 1
userFromGet.getName() : Albert Kam xzy
userFromLoad.getId() : 1
userFromLoad.getName() : Albert Kam xzy
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:52:37+00:00Added an answer on May 18, 2026 at 11:52 pm

    You are not seeing that exception because in your load(…) method you have the below System.out

    System.out.println("user fetched with 'load' inside transaction : " + user);
    

    And as you can see, you are printing the “user” by calling toString() on it (this is called automatically). And in the logs I see the below is being printed which means you have overriden the toString() method which internally is calling getter method of each of the given properties. And since this is all happening while the transaction is still open (you are commiting the transaction after the load(…) method has been called) you doesn’t see this exception.

    user fetched with 'load' inside transaction : 1:Albert Kam xzy:abc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just add gcal to my project according to this tutorial http://www.youtube.com/watch?v=it_9H0GxRNI but 12
I've used a tutorial ( http://support.microsoft.com/kb/317535 ) to create a VB.NET Class that exports
I asked another question poorly so i'll ask something else. According to http://www.c-point.com/javascript_tutorial/special_characters.htm there
I am using following a tutorial from here: http://www.shopdev.co.uk/blog/cookies-with-jquery-designing-collapsible-layouts/ This is the script I
a) Quote is taken from http://www.postgresql.org/docs/current/static/tutorial-window.html for each row, there is a set of
According to a py2exe tutorial I found I need MSVCR90.dll version 9.0.21022.8 to run
link http://msdn.microsoft.com/en-us/library/ee796239%28v=vs.91%29.aspx#Y3078 the code return this.ObjectContext.SalesOrderHeaders.OrderBy(e=>e.SalesOrderID); note have done and checked everything done till
According to this discussion , the iphone agreement says that it doesn't allow loading
According to the manual , git dcommit will create a revision in SVN for
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.