Hibernate: hbm2ddl.auto=update in production?

No, it’s unsafe. Despite the best efforts of the Hibernate team, you simply cannot rely on automatic updates in production. Write your own patches, review them with DBA, test them, then apply them manually. Theoretically, if hbm2ddl update worked in development, it should work in production too. But in reality, it’s not always the case. … Read more

The JPA hashCode() / equals() dilemma

Read this very nice article on the subject: Don’t Let Hibernate Steal Your Identity. The conclusion of the article goes like this: Object identity is deceptively hard to implement correctly when objects are persisted to a database. However, the problems stem entirely from allowing objects to exist without an id before they are saved. We … Read more

Hibernate show real SQL [duplicate]

Can I see (…) the real SQL If you want to see the SQL sent directly to the database (that is formatted similar to your example), you’ll have to use some kind of jdbc driver proxy like P6Spy (or log4jdbc). Alternatively you can enable logging of the following categories (using a log4j.properties file here): log4j.logger.org.hibernate.SQL=DEBUG … Read more

Create the perfect JPA entity [closed]

The JPA 2.0 Specification states that: The entity class must have a no-arg constructor. It may have other constructors as well. The no-arg constructor must be public or protected. The entity class must a be top-level class. An enum or interface must not be designated as an entity. The entity class must not be final. … Read more

How to solve the “failed to lazily initialize a collection of role” Hibernate exception

If you know that you’ll want to see all Comments every time you retrieve a Topic then change your field mapping for comments to: @OneToMany(fetch = FetchType.EAGER, mappedBy = “topic”, cascade = CascadeType.ALL) private Collection<Comment> comments = new LinkedHashSet<Comment>(); Collections are lazy-loaded by default, take a look at this if you want to know more.

How to print a query string with parameter values when using Hibernate

You need to enable logging for the the following categories: org.hibernate.SQL   – set to debug to log all SQL DML statements as they are executed org.hibernate.type – set to trace to log all JDBC parameters So a log4j configuration could look like: # logs the SQL statements log4j.logger.org.hibernate.SQL=debug # Logs the JDBC parameters passed to … Read more

What is the difference between DAO and Repository patterns?

DAO is an abstraction of data persistence. Repository is an abstraction of a collection of objects. DAO would be considered closer to the database, often table-centric. Repository would be considered closer to the Domain, dealing only in Aggregate Roots. Repository could be implemented using DAO‘s, but you wouldn’t do the opposite. Also, a Repository is … Read more

Hibernate throws MultipleBagFetchException – cannot simultaneously fetch multiple bags

I think a newer version of hibernate (supporting JPA 2.0) should handle this. But otherwise you can work it around by annotating the collection fields with: @LazyCollection(LazyCollectionOption.FALSE) Remember to remove the fetchType attribute from the @*ToMany annotation. But note that in most cases a Set<Child> is more appropriate than List<Child>, so unless you really need … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)