Ovidiu Guse

Greenhorn
+ Follow
since Oct 12, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ovidiu Guse

I did found a smarter but still Hibernate intrusive way of handling it. Please check the code bellow with my original method updated (yes, it is a generic entity loading method, that's why I was trying to load via class object and serializable id instead of searching):




Does someone have a better solution?

Thanks,
Ovi
Hi guys,

I have an issue with Hibernate and I don't know for sure if it is me doing something wrong or this is a bug / "feature". Maybe someone has stepped into the same problem and there is a way out.

Supposing I have a table 'Persons' with the normal structure: ID, first_name and last_name, and the following records:

ID FIRST_NAME LAST_NAME
1 Jon Doe
2 Jane Doe

On my persistence service implementation (Spring based), I have a method like:

public Person findById(final Serializable id) {
return getHibernateTemplate().load(Person.class, id);
}

Now, everything is working fine if I'm passing to this method one of the valid IDs 1 or 2.

The problem appears when I'm passing for example -1 or 3, which do not exist at the time of the query in the table. The result I'm getting is a proxy object with all fields set to null, when in fact I was expecting the null value as result. And of course, my code implements specific behavior depending on that operation result, for example if the result is null, will throw an exception triggering some other execution paths.

First I guessed it was because of the javassist bytecode provider used by Hibernate, but it turns out the same thing is happening with the cglib provider. This issue has appeared when using Hibernate 3.2 GA, but is still there in Hibernate 3.5.1 Final.
The only (odd) difference between the two Hibernate version is that in Hibernate 3.2 the proxy object returns the invalid id value used for search (e.g. person.getId() returns -1 or 3), while Hibernate 3.5.1 will throw an exception about "No row with given identifier was found ..." when trying to read the object's ID value.

I have searched the Hibernate forums and came across these two links which are not pointing to a valid solution:

http://opensource.atlassian.com/projects/hibernate/browse/EJB-167
http://opensource.atlassian.com/projects/hibernate/browse/HHH-1714

For now the ugly solution was to implement the toString() method on every domain object model and after each search like this to log the result object. This will generate an exception if the proxy result is not mapping a valid database row.

Is there a better way to handle this issue, or has somebody a solution in disabling this "beautiful" Hibernate behavior?

Thanks,
Ovi

Hi Emmanuel,

Thank you for your reply. Indeed, that is a great news, I will try it out.

Thanks again,
Ovi
... it will be possible with the above solutions to express queries like Emmanuel's example: return items where one of the actors is
Tom and his hometown is Atlanta.

Regards,
Ovi
Hello Emmanuel.

Thank you for your reply.

I have two distinct solutions for the problem, both of them using pure Hibernate Search capabilities, first of them a little unpleasant and you can say "barbaric" while the other I think is best.

Solution 1:
The solution propose define a dummy property used only for indexing purposes. The property does not have to be actually declared or mapped into Hibernate, it is enough to define its getter method. Thus, we can define something like this:

public class Part {
..................
@Field(name = "materialType", index = Index.TOKENIZED)
public String getMaterialType() {
String materialType = "";

if(getMaterial() != null) {
materialType += getMaterial().getType() + " ";
}

return materialType.trim();
}
...........
}
Hibernate Search will think the materialType it is a valid property and will index it.

Solution 2: (recommended as does not require us to alter the Part class definition)

The solution is to use the 'prefix' property of the @IndexedEmbedded annotation, inside the Part class. Hibernate Search will treat prefixed entities as own properties of the indexed object. In other words,

public class Part {
.......
@IndexedEmbedded
private Material material;
.......
}

, will become:

public class Part {
.......
@IndexedEmbedded(depth = 1, prefix = "material_")
private Material material;
.......
}

Of course, inside the Material class we must annotate the properties going to be indexed.

The generated indexes for such an approach will be:

'parts.name' - 'name' property from the Part class
'parts.description' - 'description' property from the Part class
'parts.material_id' - the 'id' property from the Material class
'parts.material_name' - the 'name' property from the Material class
'parts.material_type' - the 'type' property from the Material class

Thus we will be able to build pure queries for 'parts.material_type' indexed value for example.

Hope this helps somebody.

Regards,
Ovi
Greetings,

Can anyone help me out with indexing collections?

I have the following problem:

An object CompositeItem is holding a collection named 'parts' containing instances of a Part object.

The Part object is defined through an int - 'id', String - 'name', String - 'description' and a property called 'material' of type Material.

The Material itself is defined through an int - 'id', a String - 'type' and a String - 'name'.

Now, after indexing the CompositeItem 'parts' collection, I need to be able to query the collection for Part objects with a specific 'name', having a property 'material' with a specific 'type' property.

In other words, I need to be able to perform collection searches by querying the collection element's embedded object's properties (e.g. collection.part.material.type).

Does anyone have a solution for this?

Thank you,
Ovi
I was also interested in this. In case someone is still looking for a solution, have a look here:
http://cognitivecache.blogspot.com/2008/08/log4j-writing-to-dynamic-log-file-for.html