My Model:
@Entity(name = "WORKFLOW_ROLE")
public class WorkflowRole extends GenericModel {
@Id
@Column(name = "IDNR", unique = true, nullable = false, precision = 22, scale = 0)
@Required
@Min(0)
public long id;
}
My Controller-Action:
public static void postNewRole(@Valid models.WorkflowRole role) {
try
{
role.workflow = models.Workflow.findById(role.workflow.id);
if (validation.hasErrors()) {
params.flash(); // add http parameters to the flash scope
validation.keep(); // keep the errors for the next request
newRole();
}
role.create();
flash.success("Rolle erstellt");
index();
}
catch (Exception e)
{
params.flash();
validation.keep();
flash.put("error", e.getMessage());
newRole();
}
}
Now i get follwowing exception if i try to pass the WorkflowRole.id as a String in my form:
@6chopgia8
Internal Server Error (500) for request POST /workflowrole/postnewrole
Oops: UnexpectedException
An unexpected error occured caused by exception UnexpectedException: Unexpected Error
play.exceptions.UnexpectedException: Unexpected Error
at play.data.validation.ValidationPlugin.beforeActionInvocation(ValidationPlugin.java:80)
at play.plugins.PluginCollection.beforeActionInvocation(PluginCollection.java:639)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:134)
at Invocation.HTTP Request(Play!)
Caused by: play.exceptions.UnexpectedException: Unexpected Error
at play.db.jpa.JPAPlugin.bind(JPAPlugin.java:84)
at play.plugins.PluginCollection.bind(PluginCollection.java:579)
at play.data.binding.Binder.bind(Binder.java:112)
at play.mvc.ActionInvoker.getActionMethodArgs(ActionInvoker.java:651)
at play.data.validation.ValidationPlugin$Validator.validateAction(ValidationPlugin.java:117)
at play.data.validation.ValidationPlugin.beforeActionInvocation(ValidationPlugin.java:72)
... 3 more
Caused by: java.lang.NumberFormatException: For input string: "sdf"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:410)
at java.lang.Long.parseLong(Long.java:468)
at play.data.binding.Binder.internalDirectBind(Binder.java:612)
at play.data.binding.Binder.directBind(Binder.java:529)
at play.db.jpa.JPAPlugin.bind(JPAPlugin.java:76)
... 8 more
Is anybody experiencing similar problems with the binding of generic models in play 1.2.5?
Edit: Yes, this appears to be a bug. Patch here: https://gist.github.com/4242156
Try making your ID a Long instead of a long – I suspect this is why the binder crashes. There is special logic for primitive vars – and I think your case demonstrates a bug. However, I think if you switch to Long the code should work as expected.
In general, you should avoid primitive values for database bound objects unless you can be 100% certain there are no null values in the database. (but your code should have worked – so there is a bug)