hibernate query question ?

M

mike

I have two entities like this


public class FirstEntity{

private SecondEntity second;
.....
}

public class SecondEntity{

private String name;

},

and on web I have a bean that looks like ;

public class Bean{

private FirstEntity first;

}



and I do a select like "select first from FirstEntity first left join
fetch first.second", and i set the result to a bean on the web. Jspx
part has inputText ="#{bean.first.second.name}". My problem is what when
FK to second entity is null(what can be the case), than jspx breaks. Is
there any way to check if query doesn't find SecondEntity(FK) , that
than it creates new SecondEntity
so jspx can render.Thanks in advance.
 
J

Jacqui

I have two entities like this

public class FirstEntity{

private SecondEntity second;
....

}

public class SecondEntity{

private String name;

},

and on web I have a bean that looks like ;

public class Bean{

private FirstEntity first;

}

and I do a select like "select first from FirstEntity first left join
fetch first.second", and i set the result to a bean on the web. Jspx
part has inputText ="#{bean.first.second.name}". My problem is what when
FK to second entity is null(what can be the case),  than jspx breaks. Is
there any way to check if query doesn't find SecondEntity(FK) , that
than it creates new SecondEntity
so jspx can render.Thanks in advance.


I assume that the JSP is calling a getter method and if so, can't you
do something like this in the FirstEntity class:

public SecondEntity getSecondEntity() {
if (secondEntity == null) {
secondEntity = new SecondEntity();
}
return secondEntity;
}

Cheers,
Jacqui
 
M

mike

I assume that the JSP is calling a getter method and if so, can't you
do something like this in the FirstEntity class:

public SecondEntity getSecondEntity() {
if (secondEntity == null) {
secondEntity = new SecondEntity();
}
return secondEntity;
}

Cheers,
Jacqui

I was thinking of that also, but some other projects use SecondEntity
class, and I don't know how will this change have impact on their projects.
 
J

Jacqui

I was thinking of that also, but some other projects use SecondEntity
class, and I don't know how will this change have impact on their projects.


Ok - it always gets complicated when multiple projects are using the
same code :)

This is probably not the best, but the best I can come up with without
suggesting you dig into the code for the other projects - add a new
method called e.g., getSecondEntityDefaultIfNull() then put code above
in this and in the JSP call
inputText ="#{bean.first.secondEntityDefaultIfNull.name}"

If you can't change the source for SecondEntity the only other option
I can think of is to put an if statement in the JSP (which will make
it a bit messy unfortunately).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top