Named Queries help in removing queries from the code and add in the mapping file or the entity. @NamedQuery For creating a named query, we can use the @NamedQuery annotation. The @NamedQuery annotation accepts a name and the query. Named Query Example This example creates a Named query called “getEmployees” that fetches Employee details. …
Continue reading Hibernate Named QueriesHibernate
Hibernate @Formula We can use the @Formula annotation to get a calculated column value in Hibernate. In the below Employee Entity class, we have a column called “name”. We have added another column “upperName” using the @Formula annotation to get the name always in uppercase. Here is the corresponding Employee table : Hibernate replaces the …
Continue reading How to use a calculated column or formula in HibernateNative SQL Query in Hibernate Hibernate provides HQL(Hibernate Query Language) to query the database. Along with that, Hibernate also provides a way to use native SQL statements directly against the database. For running native queries, we can use session.createSQLQuery() method. Syntax : public SQLQuery createSQLQuery(String queryString) throws HibernateException For example, following code uses Native …
Continue reading Hibernate : Native SQL QueriesIn this article, we will create many-to-many mapping between “person” and “address” tables. This is how the project structure will look like after we finish creating all classes : To start with, lets first create the tables. Creating tables We have set the “hibernate.hbm2ddl.auto” parameter to “create” which should create the tables when the …
Continue reading Hibernate many-to-many mapping using AnnotationsIn this article, we will create both one-to-many and many-to-one mappings between “person” and “phone” tables. Here, one person can have multiple phones and one phone can be associated with only one person. To create the relationships, lets first create the tables. Creating tables We have set the “hibernate.hbm2ddl.auto” parameter to “create” which should …
Continue reading Hibernate one-to-many or many-to-one mapping using Annotations