[Hibernate + JPA + HSQLdb] Colonne Date
Bonjour,
J'ai une architecture simple : JPA qui déclare des entités, implémentée par Hibernate et connectée à HSQLdb. Rien de plus classique.
Tout marche parfaitement sauf dès que je souhaite créer une colonne/propriété Date (java.util.Date) :
ServiceDAO :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package creagarde.dao;
import creagarde.data.Service;
public class ServiceDAO extends BasicDAO {
@SuppressWarnings("unchecked")
public Service[] getServices() {
return (Service[]) session.createQuery("FROM Service s ORDER BY s.nom").list().toArray(new Service[] {});
}
public Service getService(long id) {
return (Service) session.createQuery("FROM Service s WHERE s.id = :id").setParameter("id", id).uniqueResult();
}
} |
Service :
Code:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| package creagarde.data;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import com.creatixea.gwt.data.BasicData;
@Entity
public class Service extends BasicData implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3340660154632376275L;
private String nom;
@ManyToMany(mappedBy="services", fetch=FetchType.EAGER)
@OrderColumn(name="id")
private Utilisateur[] utilisateurs;
@ManyToOne
private Utilisateur responsable;
@OneToMany(mappedBy="service", fetch=FetchType.EAGER)
@OrderColumn(name="dateInscription")
private PreInscription[] preInscriptions;
private int semaineDebut;
private int semaineFin;
private int samediDebut;
private int samediFin;
private int dimancheDebut;
private int dimancheFin;
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public Utilisateur[] getUtilisateurs() {
return utilisateurs;
}
public int getSemaineDebut() {
return semaineDebut;
}
public void setSemaineDebut(int semaineDebut) {
this.semaineDebut = semaineDebut;
}
public int getSemaineFin() {
return semaineFin;
}
public void setSemaineFin(int semaineFin) {
this.semaineFin = semaineFin;
}
public int getSamediDebut() {
return samediDebut;
}
public void setSamediDebut(int samediDebut) {
this.samediDebut = samediDebut;
}
public int getSamediFin() {
return samediFin;
}
public void setSamediFin(int samediFin) {
this.samediFin = samediFin;
}
public int getDimancheDebut() {
return dimancheDebut;
}
public void setDimancheDebut(int dimancheDebut) {
this.dimancheDebut = dimancheDebut;
}
public int getDimancheFin() {
return dimancheFin;
}
public void setDimancheFin(int dimancheFin) {
this.dimancheFin = dimancheFin;
}
public Utilisateur getResponsable() {
return responsable;
}
public void setResponsable(Utilisateur responsable) {
this.responsable = responsable;
}
public PreInscription[] getPreInscriptions() {
return preInscriptions;
}
} |
PreInscription :
Code:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| package creagarde.data;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import com.creatixea.gwt.data.BasicData;
@Entity
public class PreInscription extends BasicData {
/**
*
*/
private static final long serialVersionUID = -7342079877367354617L;
@ManyToOne
@JoinColumn
private Utilisateur utilisateur;
@ManyToOne
@JoinColumn
private Service service;
@Temporal(TemporalType.DATE)
private Date dateInscription = new Date();
@Temporal(TemporalType.DATE)
private Date dateActualisation = new Date();
public Utilisateur getUtilisateur() {
return utilisateur;
}
public void setUtilisateur(Utilisateur utilisateur) {
this.utilisateur = utilisateur;
}
public Service getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
public Date getDateInscription() {
return dateInscription;
}
public void setDateInscription(Date dateInscription) {
this.dateInscription = dateInscription;
}
public Date getDateActualisation() {
return dateActualisation;
}
public void setDateActualisation(Date dateActualisation) {
this.dateActualisation = dateActualisation;
}
} |
Et enfin le log d'erreur :
Citation:
Caused by: org.hibernate.exception.SQLGrammarException: could not initialize a collection: [creagarde.data.Service.preInscriptions#1]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2069)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:628)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1853)
at org.hibernate.type.CollectionType.getCollection(CollectionType.java:646)
at org.hibernate.type.CollectionType.resolveKey(CollectionType.java:430)
at org.hibernate.type.CollectionType.resolve(CollectionType.java:424)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:140)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
at org.hibernate.loader.Loader.doQuery(Loader.java:773)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2294)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
at org.hibernate.loader.Loader.list(Loader.java:2167)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:859)
at creagarde.dao.ServiceDAO.getService(ServiceDAO.java:13)
at creagarde.serveur.CreagardeServiceImpl.getService(CreagardeServiceImpl.java:123)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at net.sf.gilead.gwt.PersistentRemoteService.processCall(PersistentRemoteService.java:174)
... 22 more
Caused by: java.sql.SQLSyntaxErrorException: incompatible data type in conversion: from SQL type DATE to java.lang.Integer, value: instance of org.hsqldb.types.TimestampData
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.getColumnInType(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.getInt(Unknown Source)
at org.hsqldb.jdbc.JDBCResultSet.getInt(Unknown Source)
at org.hibernate.type.IntegerType.get(IntegerType.java:51)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:186)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:175)
at org.hibernate.persister.collection.AbstractCollectionPersister.readIndex(AbstractCollectionPersister.java:769)
at org.hibernate.collection.PersistentArrayHolder.readFrom(PersistentArrayHolder.java:137)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:1052)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:690)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:630)
at org.hibernate.loader.Loader.doQuery(Loader.java:745)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2062)
... 49 more
Caused by: org.hsqldb.HsqlException: incompatible data type in conversion: from SQL type DATE to java.lang.Integer, value: instance of org.hsqldb.types.TimestampData
at org.hsqldb.error.Error.error(Unknown Source)
... 63 more
Est-ce que quelqu'un aurait une idée de comment il faut s'y prendre ? Une recherche Google sur cette erreur ne renvoit vers aucun forum, comme si j'étais le premier à tomber dessus. Or je pense que c'est quelque chose de commun d'utiliser un champ Date directement ...
Merci par avance de vos idées / solutions
Émilien