2

It is possible in hibernate to get a result of a query to a mapped db object.

Is it possible to create a non-persistent object and get the query result into this object?

for example

session.createSQLQuery(select a,b,c from table).list.addEntity(myclass.class)

if myclass is:

public class myclass{

private int a;

private int b;

private int c; 
.
.
.
}

2 Answers 2

4

You can use AliasToBeanResultTransformer:

session.createSQLQuery("select a,b,c from table")
    .setResultTransformer(new AliasToBeanResultTransformer(myclass.class))
    .list();
Sign up to request clarification or add additional context in comments.

3 Comments

how can i do it if 'a' is a column with a foreign key to another table, and i want that table's records fetched into an object, but the object is also a non-persistent object?
@dan: I think you'll need to create a custom ResultTransformer.
this doesn't work for me, trying "select x as someValue ..." and i get exception telling no SOMEVALUE setter found, traced down discovered it really looks for setSOMEVALUE case sensitive :(
0

No, Hibernate will only work with classes that are mapped ahead of time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.