Pencils Down

This weblog is about my experiences in software development

Browsing Posts tagged Hibernate

Was getting this error trying to run a hibernate query in a thread during startup.

Need to add @Transactional to the class. This tells hibernate to add the session to the thread.

We have a case where people can submit multiple changes to their database records. Each change is handled separately with code like:
– from table where attribute=value
– object.setChangedValue(updatedValue)
– hibernate.flush()

However, if the user happened to submit two changes to the same record the initial query will return the initially obtained object – NOT the updated value.

We fixed this by performing a refresh() once we get the object back from the hql query.

We have a fairly large database with years of stored procedures and views and the like.

Recently we tried to access a view to get a count of items right after we update the items list. Seemed simple enough: just flush so that all the ducks are in a row. Wrong.

Turns out the view is based on an indirect access to the item list we changed, so as far as Hibernate is concerned – nothing changed.

Another engineer had the breakthrough: evict the view thereby forcing Hibernate to re-establish the values. Ta Da!

Alternatively we could have mapped that indirect set that the view uses.

Just got this (while trying to create history records of a set)

The error occurs because an attempt was made to store the exact same Collection object more than once (possibly in multiple objects).

In my case I just attempted to copy the collection out of the real object into the history object.

I think this is enforcement of the not allowing multiple inheritance in Java. Also, not sure how this would work at the database level

I have been developing Hibernate coding for a couple of years, seeing hundreds of mapping files.  I am guessing 1% of those required a full path to the class being referenced.  From the few of those that I worked on it was never clear why Hibernate, Java, Spring, whatever couldn’t find the class definition in those cases.

Just ran across another odd ball that needed the full path to the object (even though there are several others in the same package that do not need the path).

Any ideas would be appreciated.

I think I have it: if there are multiple objects of the same name in the build path then you have to differentiate.  For example, if you have a “” and a “package.two.Person” then when hibernate attempts to resolve your HQL “from Person” it will not know which Person object to attempt to locate.

I hope this is the answer – appears to make sense.

After tossing and turning for some time with the query:

from Change

and getting the error “Change not mapped” when running the associated JUnit test I finally remembered to use the full path:

from com.dantoomeysoftware.domain.Change

Not sure why it can’t find the object. I know there is probably some nice, theoretical explanation, but I don’t care. The first query should work.

I recently ran across the error “unable to find parameter [x]” where “x” was clearly defined in the list of parameters for findByNamedParam.

My first step was to remove the parameters and just try to grab the whole table, something like “from mytable”.  This resulted in “no persistent classes for query class”.  Again, the classes definitely exist and are correctly referenced in the mapping.

Then I re-added the hibernate mapping xml file to the project config.  (It was already mentioned, but appeared a little different than used successfully elsewhere so I clobbered the whole mapping section and re-entered it).  This resulted in a number of syntax errors in the xml file.

Once the xml syntax errors were cleared up I was able to retrace the coding to correctly locate the records of interest.

So, whenever you get a message from hibernate that is even remotely vague assume something else is wrong that it forgot to tell you about, but it is being nice enough to tell you something is wrong.

Yet another Oracle error.  However, in this case it is accurately described.

Ran into this with HQL where clause something like:

where =

One of the id’s was numeric, the other was a string.  Had made the silly assumption that PK’s would all be nice integers.

Our normal query execution path is:

1) change query to perform a select count on the query to get paging information established

2) load a page from the query

This works fine when you have something like “select distinct” – just replace the from part of the query to be “select count(distinct”

Once in a while we have the case of wanting a  multiple column distinct, e.g. “select distinct, table2.other,…”  HQL doesn’t let you use multiple columns/objects in a select count distinct.

The first step is to reduce your query from objects as to atomic values.  Instead of getting the Address object, just get the Address id.  This will be some Integer or other native Java that does not require any materialization (and follow-on Hibernate querying to fulfill due to aggressive loading).

So, now your standard query looks like “select distinct c1, c2, c3, …” where everything being selected is a native Java type.

Here is the leap of faith: just load the entire result set into a List and get the size() of the list.  Something like: 

Integer size = query.list().size();

Assuming you are performing some limits on the query, say under 20K records I think we are looking at about 50K of space allocated for the List resultset.  Further, the execution and transfer of data from the database to Java should be very quick (if not you probably have some work to do to optimize your query in the first place)

We ran across cases where a list (had correct size()) contained null objects on some of our queries.  At first we just put in a work around to ignore nulls.

However, a new use case arrived that required an auxiliary table to be populated.

Looking into the issue, the new table had a composite key with several elements.  It turns out when Hibernate is attempting to populate the object it verifies that each of the composite key elements is not null.  If it finds one that is null then it returns a null object.  The thinking is that a PK should never have a null.

Well, that’s nice rhetoric when you have a new system where you design from the ground up, including the database.  My current environment is the opposite: massive database compiled with over 20 years of data and associated changes.

The solution is to remove any element from a composite key that may be null.  In the particular case we were dealing with that was possible.  I can imagine there are other cases where that may not be possible.  It would then be pretty painful to massage the data to the form you need (not nulls) effect the change across the various applications using the data.