Difference between Hibernate createCriteria, createQuery, createSQLQuery functions

To create query in the Hibernate ORM framework, there is three different types. The following are the three ways to create query instance: session.createQuery() session.createSQLQuery() session.createCriteria() Look into the details of each category in detail. Session.createQuery() The method createQuery() creates Query object using the HQL syntax. For example: Query query = session.createQuery(“from Student s where … Read more

How to connect to multiple databases in Hibernate

Using annotation mappings as an example: Configuration cfg1 = new AnnotationConfiguration(); cfg1.configure(“/hibernate-oracle.cfg.xml”); cfg1.addAnnotatedClass(SomeClass.class); // mapped classes cfg1.addAnnotatedClass(SomeOtherClass.class); SessionFactory sf1 = cfg1.buildSessionFactory(); Configuration cfg2 = new AnnotationConfiguration(); cfg2.configure(“/hibernate-mysql.cfg.xml”); cfg2.addAnnotatedClass(SomeClass.class); // could be the same or different than above cfg2.addAnnotatedClass(SomeOtherClass.class); SessionFactory sf2 = cfg2.buildSessionFactory(); Then use sf1 and sf2 to get the sessions for each database. For … Read more

Hibernate @Embeddable class which extends another @Embeddable class, Properties not found for @OneToMany mapping

You can implement inheritance between @Embeddable classes. You just have to annotate the parent class with @MappedSuperclass too. So, e.g.: @Embeddable @MappedSuperclass public class Parent { @Basic private String parentProperty; // … getters/setters } @Embeddable public class Child extends Parent { @Basic private String childProperty; // … getters/setters } This way Hibernate (tested with 5.x) … Read more

How do you handle with bulk deleting by an array of IDs in Spring Data JPA?

Just add the following to your user repository interface void deleteByIdIn(List<Integer> ids); Spring will automatically generate the appropriate query via method name derivation. https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods EDIT: A litte more detail on this Using Springs Repository interfaces like CrudRepository, JpaRespository brings the basic set of database operations, like create, read, update, delete, paging, sorting and so on. … Read more

hibernate connection pool

<!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”> <hibernate-configuration> <session-factory> <!– datasource config –> <property name=”connection.url”>jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true</property> <property name=”hibernate.dialect”>org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name=”connection.driver_class”>com.mysql.jdbc.Driver</property> <property name=”connection.username”>user</property> <property name=”connection.password”>pass</property> <!– c3p0 config http://www.hibernate.org/214.html –> <property name=”connection.provider_class”>org.hibernate.connection.C3P0ConnectionProvider</property> <property name=”hibernate.c3p0.acquire_increment”>1</property> <property name=”hibernate.c3p0.idle_test_period”>60</property> <property name=”hibernate.c3p0.min_size”>1</property> <property name=”hibernate.c3p0.max_size”>2</property> <property name=”hibernate.c3p0.max_statements”>50</property> <property name=”hibernate.c3p0.timeout”>0</property> <property name=”hibernate.c3p0.acquireRetryAttempts”>1</property> <property name=”hibernate.c3p0.acquireRetryDelay”>250</property> <property name=”hibernate.show_sql”>true</property> <property name=”hibernate.use_sql_comments”>true</property> <property name=”hibernate.transaction.factory_class”>org.hibernate.transaction.JDBCTransactionFactory</property> <property name=”hibernate.current_session_context_class”>thread</property> … Read more

hibernate show query execution time

There is a simple way (built in hibernate) to achieve it. It was fixed by HHH-3659 in 3.5.4 and 3.6.0.Beta1. To go for it, use: logging on the package: org.hibernate.stat at least on DEBUG and set hibernate property: hibernate.generate_statistics=true Afterwards all the SQLs will be logged including the row count fetched and the time the … Read more

Hibernate hbm2ddl.auto=update doesn’t update column definitions in MySQL

hibernate.hbm2ddl.auto” value=”update won’t modify existing table column definitions. Doing some testing I found that: hibernate.hbm2ddl.auto” value=”update will add a db column that doesn’t already exist. hibernate.hbm2ddl.auto” value=”update will not delete a db column that is removed/no longer in your entity. hibernate.hbm2ddl.auto” value=”update will not modify a db column that has already been created. You’ll need … Read more

Advantages of Named queries in hibernate?

Named queries are compiled when SessionFactory is instantiated (so, essentially, when your application starts up). The obvious advantage, therefore, is that all your named queries are validated at that time rather than failing upon execution. The other advantage is that they’re easy(-ier) to maintain – certainly for complex queries. The disadvantage is that named queries … Read more

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: USERS, for columns: [org.hibernate.mapping.Column(invoices)]

If I remember correctly, Hibernate doesn’t let you mix and match annotation in conjunction with field / getter. If your @Id annotation is set over a field, all your mappings should follow fields. Try moving @OneToMany @JoinColumn(name=”INVOICE_ID”, nullable=false) from getInvoices() to private Set<Invoice> invoices; This pattern should be applied to your Invoice class as well