We have:
public class TicketDbo {
@Required
@ManyToOne
public ProjectDbo project;
}
We then have many projects to choose from, so we add a pulldown menu when you edit the Ticket where you can choose one of the projects.
We want to render something like this(this is the easy part and we know how to do this part with #{select}:
<select ... >
<option value="" selected="selected">Please select...</option>
<option value="1">Project1</option>
<option value="56">Project2</option>
</select>
NOTE: The value above is the entity id in the database.
What we want is some piece of code outside the controller where this code would run before the controller is invoked (AND we could hopefully re-use this code for any of these ManyToOne situations that happen all the time):
ProjectDbo project = JPA.em().getReference(selectedOptionValueFromAbove);
//note, getReference doesn't hit DB!!!!
ticketDbo.setProject(project);
Then our controller would be the same as always:
public static void postTicket(TicketDbo ticket) {
//at this point the ticket already has the ProjectDbo and validation of it being required has already been run from the @Required annotation
}
Anyone knows how to do something like this? Or is there a plugin to help with this?
@2ND QUESTION: How do this with an enumeration as well? (Hopefully that is just a tweak to the above).
This seems to work well(formed from one of the comments @maartencls made on another answer)
Then in the page, I can do a #{ticket.project} and in my controller method, my TicketDbo ends up with a ProjectDbo proxy wrapping the id selected!!! works great so far I think. This class also works not just for my TicketDbo for other entities as well that have a ProjectDbo in them.