我是新来的^^
我正在使用一个db,其中boolean注册为VARCHAR(3)所以我做了一个转换器:
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);
}
}
我把我的注释@Convert:
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;[/CODE]
但是当我按照标准提出请求时:
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;
}
我发生了这个错误:
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()
看起来它没有使用我的转换器,我试过断点它没有进入我的转换器...在转换器文档中它说“你要做的就是把@convert”但显然没有..请帮助我真的输了。