I already read a lot of questions here about this same problem, and I can assure you that is not the EQUALs method, and also, it is not a problem as the bean scope. So, lets see what is happening.
I have this selectOne, that is working ‘partially’
<p:selectOneMenu id="acao" converter="#{acaoConverter}" value="#{prospectoRadarController.acaoSelecionada}">
<f:selectItem itemLabel="#{msg['label.selecione.item']}" itemValue="" />
<f:selectItems var="a" itemValue="#{a}" itemLabel="#{a.descricao}" value="#{acaoController.listarAcoesEmAberto()}" />
</p:selectOneMenu>
So I have the ‘Acao’ entity. All the entities are completly valid, as the HTML of the select is also correct (the value is the correct ID of the entity).
The data that I imported (using an SQL file) check out the last two lines:
INSERT INTO com_acao VALUES (190, 0.00, '2012-12-31', '2012-12-31', '2012-12-31', 'SELECIONADOS 2012-1', 0, 1, 1);
ALTER SEQUENCE com_acao_id_seq RESTART WITH 191;
Ok then, If i INSERT a new entity, it inserts correctly, with the right ID, and everything works fine.
THE PROBLEM: On that SelectOneMenu, if I choose an entity that was imported with the SQL file, it works. If I choose a new entity, that I saved using the system, I get the:
javax.faces.component.UISelectOne.INVALID
Validation error. So again, the equals is not broken. My best bet is that this is Hibernate related. But…. HOW come it adds, filters, searches for the entity, and no errors occurrs? Only the validation error is happening ? 🙁
This is the entity ID declaration:
public class Acao {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
Thanks !
My Repositories are Spring Data’s PagingAndSorting interface. I don´t know if this is a relevant information… If you guys need any info, just ask! Thanks again!
UPDATE
The method that loads the list of “Acao” to be displayed on the SelectItems:
public List<Acao> listarAcoesEmAberto() {
return acaoService.listarAcoesEmAberto();
}
With is this Query executed:
@Query("select a from Acao a where a.dataFinal is null OR current_date() <= a.dataFinal")
public List<Acao> listarAcoesEmAberto();
UPDATE 2 The converter methods:
@SuppressWarnings("unchecked")
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
if (arg2 == null || arg2.equals("")) {
return null;
}
try {
return baseService.findOne((PK) new SimpleTypeConverter().convertIfNecessary(arg2, ReflectUtil.getPrimaryKeyField(entityClass).getType()));
} catch (TypeMismatchException e) {
return null;
}
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
if (arg2 == null) {
return "";
}
return arg2.toString();
}
NOTE: this is a generic converter. The real converter is:
@Component
@Scope(“request”)
public class AcaoConverter extends BaseConverter implements Serializable {
private static final long serialVersionUID = 1L;
@Autowired
public AcaoConverter(AcaoService acaoService) {
super(acaoService, Acao.class);
}
So its the same converter for ALL of the entities. And it works.
Why not for this particular case? 🙁
UPDATE 3 Lets see the problem better…
This is the imported row from the sql file:
This is a row that I created using the program:
This is the same on the database (so it is commited):
This is the SELECT:
The HTML:
SUCESS CASE (selecting the imported row):
ERROR CASE (selecting the created row):
UPDATE 4 WOW ! I tested with some other entities that where imported, and… Some of them doesn´t work. So, bottom line: new entities doesn´t work, and some imported entities doesn´t work! Looking at the database, they are all fine data, nothing missing or violations. So wierd!
Another thing.. with autoComplete component it works!!!! (everything works)
Well,
no solution for selectOneMenu.
So I used an autoComplete with dropdown (true).
It works fine.
So, this is not really a solution, but a workaround.
Thanks all for the help.