Example & Tutorial understanding programming in easy ways.

How will you retrieve an object by Identifier from the database in Hibernate?

Retrieve an object by Identifier from the database in Hibernate:


About the Retrieving an object by identifier from the database in Hibernate we Make a Employee class, In Employee class is the class whose object will be inserted to the database.


1. In RetrieveObjectByInHibernate we use the Hibernate API to make the interface with the database.


2. Now, we create a new configuration, that allows the application, to specify properties and mapping documents to be used when creating a SessionFactory.


3. Usually an application will create a single configuration , Build a simple instance of Sessionfactory and then instantiate Session is threads servicing client requests. Using configure() API method we use the mapping and properties specified in an application resource named hibernate.cfg.xml. Then with buildSessinFactory() we instantiate a new SessionFactory, using the properties and mapping in this configuration. 


4. Use getTransaction() API method of Session and commit() API method of Transaction to commit the Transaction.


5. Use the beginTransaction() API method again. Now create a new Query, using the createQuery(String queryString) API method of Session, with a given query.


6. Use get(Class clazz, Serializable id) API method of Session to get the persistent instance of the given entity class with the given identifier.


7. Use again getTransaction() API method of Session and commit() API method of Transaction to commit the Transaction.


8. In the code snippets that follow, you can see the Employee class and the RetrieveObjectByIdInHibernate Class that applies all above steps. You can also take a look at the hibernate.cfg.xml file, that holds all configuration for Hibernate, such as JDBC connection settings, and employee.hbm.xml file that holds the mapping configuration between the Employee class and the Employee table. 

Read More →
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!