Hi everyone.
I've been having some problems with Lazy loading of non-collection associations, using a non-default constructor.
Looking at this example:
Code:
select new Message(m.id, action)
from Message as m
join m.action as action
If we assume the action property as being an entity of the type Action, and also assuming that on the Message we have it defined like this:
Code:
class Message {
...
@ManyToOne(targetEntity = Action.class, fetch = FetchType.LAZY)
@JoinColumn(name = "ACTION_ID", referencedColumnName = "ACTION_ID", nullable = false)
private Action action;
...
}
so with this scenario, if i run the first query, i will get LazyInitializationException, since the mate Object received by the Message constructor wasn't "fetch", thus i tried to change the query to:
Code:
select new Message(m.id, action)
from Message as m
join fetch m.action as action
but then i get this:
QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
Any ideas on how to work around this issue, other than doing it like this:
Code:
select new Message(m.id, action.field1, action.field2, action.field3 ... )
from Message as m
join fetch m.action as action
and then having a Message constructor receiving all the fields from the Action that i need to create an Action object and assign it to the Message's action field manually ?
Thanks in advance.
<3