Query = session.createNativeQuery("select count(id) from User_Info") Creating a Query by calling createNativeQuery() method to execute SQL count() function Query = session.createNativeQuery("select avg(salary) from User_Info") Creating a Query by calling createNativeQuery() method to execute SQL avg() function Query = session.createNativeQuery("select max(salary) from User_Info") Creating a Query by calling createNativeQuery() method to execute SQL max() function Query = session.createNativeQuery("select min(salary) from User_Info") Creating a Query by calling createNativeQuery() method to execute SQL min() function ("Retrieving values in multiple columns with property 'id' and 'firstName' ") List collection = query1.getResultList() Query query = session.createNativeQuery("select ID,First_Name from User_Info") Creating a Query by calling createNativeQuery() method to execute an SQL query Session.save(ob3) //Saving the third object Session.save(ob2) //Saving the second object Session.save(ob1) //Saving the first object SessionFactory sf= new Configuration().configure().buildSessionFactory() Object to perform the object persistence operations. Using Configuration object we can create a SessionFactory object, which is eventually used to create a Session This object is used to specify the location of a configuration file and This is the first object we use when using the Hibernate. The Hiber class creates a Configuration object, used to configure the Hibernate. This class will create some UserInfo objects, which will be persisted in a database table using the HibernateĪnd then it's going to execute the SQL commands like accessing multiple columns of a table andĮxecuting SQL methods like max(), min(), sum(), count() and avg() using the method createNativeQuery() of Query class. Public void setFirstName(String firstName)Ĭreating the class that calls the Hibernate API This class is an entity class, hence it will be preceded with the annotation plus a few other annotations used below. Some may even refer to such class whose objects needs to be persisted as the Model class. This is a simple Java class whose objects needs to be persisted/saved, these objects are also known as Plain Old Java Objects (POJO) or Entity class. Representing our Model class(whose objects needs to be persisted). We are going to create a general parent class that will contain all the Java code files And yes, one more thing, we are going to create this program in an Integrated Development Environment (IDE),.įor those who are not aware of Hibernate Annotations, please read We are going to use some Hibernate Annotations, hence, there will be no need to create the mapping-resource with extension hbm.xml. In the upcoming example, We are going to create a Java class which will be mapped to a table in our database( Oracle Express Edition 10g).Today, we are going to show you how to execute the SQL commands like accessing multiple columns of a table andĮxecuting SQL methods like max(), min(), sum(), count() and avg() Hibernate also provides a way to execute the SQL queries by using its method createNativeQuery() of Query class. So far, you have seen how to execute commands in Hibernate Query Language (HQL) which are slightly different from SQL commands.įor those who are somehow uncomfortable with learning Hibernate Query Language to interact with the database, don't worry, Hibernate has got a solution.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |