Bonjour j'ai à faire à une db dans laquelle les booleens sont stockés sous forme de string de 3 caracteres "OUI" ou "NON" alors ni une ni deux je pense converter, je récupère un petit bout de code et je me lance :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
 
@Converter
public class BooleanToStringConverter implements
		AttributeConverter<Boolean, String> {
 
	@Override
	public String convertToDatabaseColumn(Boolean value) {
		if (value == null)
			return "";
		else if (value)
			return "OUI";
		else
			return "NON";
 
	}
 
	@Override
	public Boolean convertToEntityAttribute(String value) {
		if (value == null)
			return null;
		else if (value.trim().equals("OUI"))
			return true;
		else if (value.trim().equals("NON"))
			return false;
		else if (value.trim().equals(""))
			return null;
		else
			throw new IllegalStateException("Invalid boolean character: "
					+ value);
	}
}
Je pense à préciser là ou il faut utiliser le converter :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Institution extends AbstractHREntity {
 
	@Column(name = "IDOU00")
	private String code;
	@Column(table = "ZEXX", name = "LBOULG")
	private String libelle;
	@Column(table = "ZEXX", name = "LBOUSH")
	private String abreviation;
	@Column(table = "ZEYY", name = "LIBEXL")
	private String libelleLong;
	@Convert(converter = BooleanToStringConverter.class)
	@Column(table = "ZEZZ", name = "ESTBUDGET", length = 3)
	private Boolean isBudget;
Mais voilà quand je veux faire une requete avec un criteria :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public List<Institution> findInstitutions(RechercheInstitutionData datas) throws DaoException{
		List<Institution> resultList = new ArrayList<Institution>();
		DetachedCriteria criteria = DetachedCriteria.forClass(Institution.class, "institution");
 
		if(null!=datas.getInstitutionSearched())
		{
			if (StringUtils.isNotBlank(datas.getInstitutionSearched().getLibelleLong())){
				criteria.add(Restrictions.like("institution.libelleLong", datas.getInstitutionSearched().getLibelleLong().toUpperCase(), MatchMode.START));				
			}
			if (StringUtils.isNotBlank(datas.getInstitutionSearched().getAbreviation())){
				criteria.add(Restrictions.like("institution.abreviation", datas.getInstitutionSearched().getAbreviation().toUpperCase(), MatchMode.START));				
			}
			if (StringUtils.isNotBlank(datas.getInstitutionSearched().getLibelle())){
				criteria.add(Restrictions.like("institution.libelle", datas.getInstitutionSearched().getLibelle(), MatchMode.START).ignoreCase());				
			}
			if (StringUtils.isNotBlank(datas.getInstitutionSearched().getCode())){
				criteria.add(Restrictions.like("institution.code", datas.getInstitutionSearched().getCode(), MatchMode.START));				
			}
			criteria.addOrder(Order.asc("institution.code"));
		}
		resultList = find(criteria);
		return resultList;
	}
J'ai le droit à :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
10:25:31,172 INFO  [RechercheInstitution.beans.RechercheInstitutionBean] (http-localhost/127.0.0.1:8080-5) --> findInstitutions()
10:25:32,549 INFO  [stdout] (http-localhost/127.0.0.1:8080-5) Hibernate: select this_.NUDOSS as NUDOSS1_35_0_, this_.IDOU00 as IDOU2_35_0_, this_1_.TBUDGE as TBUDGE1_39_0_, this_2_.LIBEXL as LIBEXL1_37_0_, this_3_.LBOUSH as LBOUSH1_36_0_, this_3_.LBOULG as LBOULG2_36_0_ from ZE00 this_ left outer join ZEWD this_1_ on this_.NUDOSS=this_1_.NUDOSS left outer join ZE04 this_2_ on this_.NUDOSS=this_2_.NUDOSS left outer join ZE01 this_3_ on this_.NUDOSS=this_3_.NUDOSS where this_3_.LBOUSH like ? order by this_.IDOU00 asc
 
10:25:33,310 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:8080-5) SQL Error: -99999, SQLState: 07006
10:25:33,311 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:8080-5) Data type mismatch. (For input string: "OUI")
10:25:33,313 ERROR [RechercheInstitution.ejb.institution.InstitutionMgrBean] (http-localhost/127.0.0.1:8080-5) InstitutionMgrBean.findInstitutions : common_HR.exception.DaoException: org.hibernate.exception.SQLGrammarException: could not execute query
10:25:33,315 INFO  [RechercheInstitution.beans.RechercheInstitutionBean] (http-localhost/127.0.0.1:8080-5) <-- findInstitutions()
On dirait qu'avec les criteria le converter n'est pas utilisé, j'ai mis mes points d'arrets et jamais on n'entre dedans. Alors je suis un peu perdu... Que faire?