ciroanacleto / hibernate-generic-dao

Automatically exported from code.google.com/p/hibernate-generic-dao
0 stars 0 forks source link

Referencing the root object using addField() #51

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This is an issue for the problem I encountered in this thread:
http://groups.google.com/group/java-generic-dao/browse_frm/thread/b5bdcecde1786c
67

The problem appears when, in a query with a ResultMode such as
RESULT_ARRAY, you need to reference the root entity.

For example, you want the result to be a list of arrays, each array
containing both the root object and some additional information:
(see the forum thread for more background information)

Search search = new Search(Customer.class);
search.setResultMode(ISearch.RESULT_ARRAY);
// Note: search.addField("") does not add any field, since
// SearchUtil.addField(IMutableSearch search, String property) does
//nothing for null and empty properties
search.addField(new Field("")); // here we attempt to reference the root entity
search.addField("orders.size"); 

List<Object[]> results = dao.search(search);
for (Object[] result : results) {
    Customer customer = (Customer) result[0];
    int orderSize = (Integer) result[1];
    // do something with customer and size
}

Ideally, the search should generate a request like
"select _it, _it.orders.size from com.example.Customer _it; "

Instead, it generates:
"select *, _it.orders.size from com.example.Customer _it; "
and you get an Exception:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line
1, column 8 [select *, _it.orders.size from com.example.Customer _it]

What version of the product are you using? On what operating system?
trg-dao-0.5.0

Please provide any additional information below.

I'm not sure about the best way to specify root entities. A few ideas:
- Consider that a property like "" indeed refers to the root entity :
search.addField("");
(this should be documented clearly so that people do not mistakenly use it)
- Create a constant:
search.addField(ISearch.ROOT_PROPERTY);

with ROOT_PROPERTY defined on ISearch:
String ROOT_PROPERTY = "_trg_rootProperty";

I saw a TODO in the code which might be related to this:
BaseSearchProcessor.class, lines 222 - 227:
String prop;
if (field.getProperty() == null || "".equals(field.getProperty())) {
prop = "*"; // TODO: I think this will be a problem
} else {
prop = getPathRef(ctx, field.getProperty());
}

Best regards,
- Etienne

Original issue reported on code.google.com by nev...@gmail.com on 27 Oct 2009 at 10:24

GoogleCodeExporter commented 9 years ago

Original comment by dwolvert on 31 Oct 2009 at 2:56

GoogleCodeExporter commented 9 years ago
Fixed for the 0.5.1 release

Original comment by dwolvert on 20 Feb 2010 at 7:05