The application obtains Session instances from a SessionFactory. There is typically a single SessionFactory for the whole application—created during application initialization. The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work
SessionFactorysessionFactory = configuration.buildSessionFactory();
The general flow of Hibernate communication with RDBMS is :
<hibernate-mapping> <class name=”com.test.User” table=”user”> <property column=”USER_NAME” length=”255″ name=”userName” not-null=”true” type=”java.lang.String”/> <property column=”USER_PASSWORD” length=”255″ name=”userPassword” not-null=”true” type=”java.lang.String”/> </class> </hibernate-mapping>
load() vs. get()
Only use the load() method if you are sure that the object exists.
load() method will throw an exception if the unique id is not found in the database. load() just returns a proxy by default and database won’t be hit until the proxy is first invoked.
If you are not sure that the object exists, then use one of the get() methods.
get() method will return null if the unique id is not found in the database.