Is there a built in converter for Y/N and boolean in JSF and/or SEAM to use with h:selectBooleanCheckbox?
-
08-09-2020 - |
Pergunta
If an entity has the property defined as
private String noWstManagedFlg;
and the database is constraint is set to enforce a 'Y' or 'N'. Is there a built in Y N to boolean converter I can use with h:selectBooleanCheckbox? Or will I need to add my own converter and/or property on my entity that returns a boolean?
<h:selectBooleanCheckbox value="#{entity.noWstManagedFlg}" />
Solução
There is no such converter in Seam, but if your JPA implementation is Hibernate, you can map that property with 'yes_no
' type and have it boolean in the entity.
@Type(type= "yes_no")
private boolean noWstManagedFlg;
Outras dicas
From experience and what I've read h:selectBooleanCheckbox does not support converters. I had written a converter that would convert "Y"/"N" to true/false. The "getAsString" method is called as you'd expect, but the "getAsObject" method is never called. @Stefano is right the best way to go is use the Hibernate "yes_no" or "true_false" Type on your entity property.
I ran into a problem when using '@Type(type = "yes_no")'. First of all the hibernate failed to properly convert the "Y"/"N" strings to true/false values. And secondly, trying to update an entity would cause the program to hang. Using the following mapping solved these issues for me and now everything works as expected.
@Type(type = "org.hibernate.type.YesNoType")
private boolean noWstManagedFlg;