hibernate

Hibernate fetching strategy - when to use "join" and when to use "select"?

Most Hibernate associations support "fetch" parameter: fetch="join|select" with "select" being default value. How to decide which one to use for which association? I tried changing all from "select" to "join" application wide - number of generated queries decreased probably 10 times but performance stayed exactly the same (even bec...

Best way to use hibernate for complex queries like top n per group

I'm working now for a while on a reporting applications where I use hibernate to define my queries. However, more and more I get the feeling that for reporting use cases this is not the best approach. 1) The queries only result partial columns, and thus not typed objects (unless you cast all fields in java). 2) It is hard to express que...

Hibernate SQL Audit Logging

I want to do audit logging generated SQL statements in Hibernate. I wrote Custom Interceptor that extents EmptyInterceptor. I overrided onPrepareStatement method to get generated SQL. But When I debug this code I am only getting generated SQL without parameters like this. INSERT INTO TableA(Col1,Col2,Col3) VALUES(?,?,?) How can I get ...

How do I prevent Hibernate from trimming strings?

I'm using Oracle 10g and Hibernate 2.1 (old version, but not allowed to upgrade to fix). I've got a table with a not nullable column named TIN, varchar2(9). This column is used to stage data loaded from flat files and so can store any string of length 9, including 9 spaces (that is: ' ') if the input file had 9 spaces. What ...

Best Practice Updating DB Records

Say you retrieve 100 records, and display them on a page. The user only updates 2 of the records on the page. Now you want to update only the two records, and not the other 98. Is it best to have one submit on the page, then somehow know which 2 are updated, then send only those two to the db for an update? What does the "somehow" look ...

Case-insensitive equals using Hibernate Criteria

I've seen Restrictions.ilike('property', '%value%'), but would like to generate SQL like: lower(property) = 'value'. Any ideas? I used: Restrictions.eq("email", email).ignoreCase() since Expression is deprecated. The SimpleExpression will call toLowerCase() on the value, so it is not necessary to do it beforehand. See: SimpleE...

Zero Downtime with Hibernate

What changes to a database (MySQL in this case) does Hibernate survive (data, schema, ...)? I ask this because of zero downtime with Hibernate. Change database, split app servers into two clusters, redeploy the application on one of the clusters and switch application. Thanks Stephan ...

Spring Framework

What is Spring Framework? What's its contribution to Hibernate? ...

Hibernate is calling public methods on the entities after a query, why?

I'm using hibernate 3.0 (with posgre 8.3), Java 1.6 and Netbeans 6.5. I've created one native query to return all the unique most recent entries like this: String query = "SELECT DISTINCT ON (origem) * FROM entrada " + "ORDER BY origem, horadata DESC"; SQLQuery sqlQuery = this.getSession().createSQLQuery(query); ...

Why am I getting a Hibernate LazyInitializationException in this Spring MVC web application when the data displays correctly?

I am attempting to create a web application using Spring MVC, with Hibernate as its ORM layer. However, due to my inexperience with both frameworks I'm struggling. The following code will properly display all the records I am looking for but still throw a stack trace into my logs. I'm having trouble finding thorough documentation concer...

Get the table name from the model in Hibernate

How do I get the table name for a model in Hibernate? Apparently there used to be a getTableName() method in ClassMetadata, but it's been removed. There's a getClassMapping(String entityName) method in Configuration, but I don't know how I can (or if I should) use Configuration from within my DAO implementation. My DAO implementation ...

About Hibernate save using Dynamic-Map entity mode

In the following example, how can I save the value of role to the role with id=1 without loading it? I have tried: Map user = new HashMap(); user.put("address","Address test"); user.put("role",1); session.save("User",user); But that results in: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Map at o...

transaction timeout not working on hibernate with oracle

I am having problem setting the transaction timeout for hibernate on oracle. It does not work.Can anyone help? The "SaveOrUpdate" will not return within the stated 10 seconds. It will hang for a very long time. I am using Oracle 10r2. Hibernate Configuration File oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@9.9.9.9:1521...

How to get the Join of Two One to Many associations?

I have two hibernate entities User and Blog. User can be interested in multiple Tags. Blog can belong to multiple Tags. For a User, How do i find the Blogs which belong to the Tags the User is interested in? I need something like Select * from Blog where Blog.Tags IN User.Tags except that SQL or HQL doesnt allow such comparisons in ...

Integrating Spring + Hibernate + Sql server with unicode support

How could I integrate Spring with Hibernate using sql server 2005 and have Unicode support. I tried many different ways but I just couldn't get it to work. Column in the table is nvarchar, character set in Spring is UTF-8. I can read Unicode text (which I added myself using the sql server management tool) just fine but writing doesn't w...

HIbernate and CMT

Lately I decided to implement in my project CMT transactions (jBoss5, Hibernate, jta postgres datasource). Everything is working fine except Hibernate.initialize() in my entities. It works in EJB beans but when trying to invoke initialize in entity getter I get "couldn't associate with session" exception. It worked just fine before imple...

Performing Date/Time Math In HQL?

I'm looking how to perform date/time math within an HQL query. Specifically, how do I add or subtract (x) amount of time from the result of the current_timestamp() function? Or do I have to drop into SQL for this and hope that whatever database is being run supports it? HQL query example: FROM RandomThing WHERE randomTime IS NOT NULL A...

OptimisticLockException when trying to fetch data from postgreSQL

We're developing a WebSite that's intended to have several requests per second.- Our current environment is JavaEE, JBoss 4.2.3, Struts2 for MVC, JPA with Hibernate as ORM and Postgres as DB.- This is the scenario: whenever a request comes to any of the pages, some action checks for information in the DB to fill the requested page (tha...

Can I specify a hibernate relationship with a filter?

I have a foo that has a relationship to many bar's. When I delete bar's in my system I want to keep them in the database for some crazy business reason so I just set a deleted field to true. Can I specify in my hibernate mapping that I only want my collection to hold elements where that field is false? ...

Hibernate Query runs slow in the system, but fast when run directly.

I have a problem similar to the on in this weeks podcast. We have a Java application using hibernate with Sql Server 2005. Hibernate is generating a Query for us that is taking nearly 20 minutes to complete. If we take the same query using show_sql and replace the questions marks with constant value the answer is returned immediately....