When to Use EntityManager.clear()?

The articles explains it. Clearing the entity manager empties its associated cache, forcing new database queries to be executed later in the transaction. It’s almost never necessary to clear the entity manager when using a transaction-bound entity manager. I see two reasons to clear: when doing batch processing, in order to avoid having a giant … Read more

Unable to autowire the service inside my authentication filter in Spring

You cannot use dependency injection from a filter out of the box. Although you are using GenericFilterBean your Servlet Filter is not managed by spring. As noted by the javadocs This generic filter base class has no dependency on the Spring org.springframework.context.ApplicationContext concept. Filters usually don’t load their own context but rather access service beans … Read more

Create session factory in Hibernate 4

Yes, they have deprecated the previous buildSessionFactory API, and it’s quite easy to do well.. you can do something like this.. EDIT : ServiceRegistryBuilder is deprecated. you must use StandardServiceRegistryBuilder public void testConnection() throws Exception { logger.info(“Trying to create a test connection with the database.”); Configuration configuration = new Configuration(); configuration.configure(“hibernate_sp.cfg.xml”); StandardServiceRegistryBuilder ssrb = new … Read more

Why do we use @Embeddable In Hibernate

There are two types of objects in Hibernate 1. Value Object 2. Entities Value Objects are the objects which can not stand alone. Take Address, for example. If you say address, people will ask whose address is this. So it can not stand alone. Entity Objects are those who can stand alone like College and … Read more

hibernate 4 and joda-time

A distinct paucity of documentation, means it might be helpful for me to write down the steps required for integration. Make sure your libraries are up to date. You’ll need : [assuming you already have hibernate4] Latest version of joda-time <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.0</version> </dependency> and usertype lib <dependency> <groupId>org.jadira.usertype</groupId> <artifactId>usertype.core</artifactId> <version>3.0.0.CR1</version> </dependency> Then use … Read more

When to use DiscriminatorValue annotation in hibernate

These 2 links help me understand the inheritance concept the most: http://docs.oracle.com/javaee/6/tutorial/doc/bnbqn.html http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html?page=6 To understand discriminator, first you must understand the inheritance strategies: SINGLE_TABLE, JOINED, TABLE_PER_CLASS. Discriminator is commonly used in SINGLE_TABLE inheritance because you need a column to identify the type of the record. Example: You have a class Student and 2 sub-classes: GoodStudent … Read more

How to turn off hbm2ddl?

Just omitting hibernate.hbm2ddl.auto defaults to Hibernate not doing anything. From the reference documentation: 1.1.4. Hibernate configuration The hbm2ddl.auto option turns on automatic generation of database schemas directly into the database. This can also be turned off by removing the configuration option, or redirected to a file with the help of the SchemaExport Ant task. Setting … Read more