lordv Posté(e) le 1 juin 2011 Partager Posté(e) le 1 juin 2011 Salut tout le monde je me suis mis à faire un peu de J2ee, Seulement j'ai un problème pour récupérer le contenu d'une vue, j'ai utilisé Hibernate pour la partie Orm toutes mes requêtes fonctionnent sauf ceux des view. En effet il me renvoi une liste de 513 éléments correspondant au nombre de record dans la base mais chaque élément est null. Voici un exemple C'est une vue qui contient les catégories de produits les sous-catégories et le nombre de produit pour chaque cat_id : je recupere les informations de la vue ainsi : Session session = HibernateUtil.currentSession(); //debut transaction session.beginTransaction(); List<ArbreCatShop> result = session.createQuery(" from ArbreCatShop ").list(); //commit hibernate session.getTransaction().commit(); int i = 0; while (i < result.size()) { out.println(result.get(i).getId().getCatWebLink()); out.println("<br>"); i++; } HibernateUtil.closeSession(); la vue au niveau de la bdd : CREATE VIEW HR.ARBRE_CAT_SHOP ( CAT_WEB_LINK, CAT_PT_NAME, CAT_SHOP_NBR, CAT_ID, CAT_ID_P, CAT_NAME, CAT_LINK, CAT_COM, CAT_WEIGHT, CAT_SHOP, CAT_ANN, CAT_CLASS, CAT_LOGO, CAT_LEVEL ) AS SELECT '/shopping' || SYS_CONNECT_BY_PATH(u.cat_link, '/') AS cat_web_link , SUBSTR( SUBSTR(SYS_CONNECT_BY_PATH(u.CAT_NAME, '||'),3), instr(SUBSTR(SYS_CONNECT_BY_PATH(u.CAT_NAME, '||'),3),'||',2)+2, (instr(SUBSTR(SYS_CONNECT_BY_PATH(u.CAT_NAME, '||'),3),'||',-1) - instr(SUBSTR(SYS_CONNECT_BY_PATH(u.CAT_NAME, '||'),3),'||',2)-2)) AS cat_pt_name, count_produit_dispo(u.cat_id) as CAT_SHOP_NBR, u."CAT_ID",u."CAT_ID_P",u."CAT_NAME",u."CAT_LINK",u."CAT_COM",u."CAT_WEIGHT",u."CAT_SHOP",u."CAT_ANN",u."CAT_CLASS",u."CAT_LOGO",LEVEL as cat_level FROM categories u WHERE u.cat_shop=1 start with u.cat_id_p IS NULL connect by u.cat_id_p = prior u.cat_id WITH READ ONLY; Mon fichier Cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name=""> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <property name="show_sql">true</property> <!-- config de la session factory pour utiliser Oracle --> <property name="hibernate.connection.username">HR</property> <property name="hibernate.connection.password">passwd</property> <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE</property> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!-- fichiers de mapping --> <mapping resource="com/ndak/hjx/db/ArbreCatShop.hbm.xml"/> <mapping resource="com/ndak/hjx/db/Categories.hbm.xml"/> </session-factory> </hibernate-configuration> le fichier de mapping ArbreCatShop.hbm.xml : <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated May 31, 2011 11:56:33 PM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.ndak.hjx.db.ArbreCatShop" table="ARBRE_CAT_SHOP" schema="HR"> <composite-id name="id" class="com.ndak.hjx.db.ArbreCatShopId"> <key-property name="catWebLink" type="string"> <column name="CAT_WEB_LINK" length="4000" /> </key-property> <key-property name="catPtName" type="string"> <column name="CAT_PT_NAME" length="4000" /> </key-property> <key-property name="catShopNbr" type="big_decimal"> <column name="CAT_SHOP_NBR" precision="22" scale="0" /> </key-property> <key-property name="catId" type="big_decimal"> <column name="CAT_ID" precision="22" scale="0" /> </key-property> <key-property name="catIdP" type="big_decimal"> <column name="CAT_ID_P" precision="22" scale="0" /> </key-property> <key-property name="catName" type="string"> <column name="CAT_NAME" length="200" /> </key-property> <key-property name="catLink" type="string"> <column name="CAT_LINK" length="200" /> </key-property> <key-property name="catCom" type="big_decimal"> <column name="CAT_COM" precision="22" scale="0" /> </key-property> <key-property name="catWeight" type="big_decimal"> <column name="CAT_WEIGHT" precision="22" scale="0" /> </key-property> <key-property name="catShop" type="java.lang.Boolean"> <column name="CAT_SHOP" precision="1" scale="0" /> </key-property> <key-property name="catAnn" type="java.lang.Boolean"> <column name="CAT_ANN" precision="1" scale="0" /> </key-property> <key-property name="catClass" type="string"> <column name="CAT_CLASS" length="50" /> </key-property> <key-property name="catLogo" type="string"> <column name="CAT_LOGO" length="200" /> </key-property> <key-property name="catLevel" type="big_decimal"> <column name="CAT_LEVEL" precision="22" scale="0" /> </key-property> </composite-id> </class> </hibernate-mapping> la class ArbreCatShop package com.ndak.hjx.db; // Generated May 31, 2011 11:56:32 PM by Hibernate Tools 3.4.0.CR1 /** * ArbreCatShop generated by hbm2java */ public class ArbreCatShop implements java.io.Serializable { /** * */ private static final long serialVersionUID = 1L; private ArbreCatShopId id; public ArbreCatShop() { } public ArbreCatShop(ArbreCatShopId id) { this.id = id; } public ArbreCatShopId getId() { return this.id; } public void setId(ArbreCatShopId id) { this.id = id; } } la class ArbreCatShopId package com.ndak.hjx.db; // Generated May 31, 2011 11:56:32 PM by Hibernate Tools 3.4.0.CR1 import java.math.BigDecimal; /** * ArbreCatShopId generated by hbm2java */ public class ArbreCatShopId implements java.io.Serializable { /** * */ private static final long serialVersionUID = 1L; private String catWebLink; private String catPtName; private BigDecimal catShopNbr; private BigDecimal catId; private BigDecimal catIdP; private String catName; private String catLink; private BigDecimal catCom; private BigDecimal catWeight; private Boolean catShop; private Boolean catAnn; private String catClass; private String catLogo; private BigDecimal catLevel; public ArbreCatShopId() { } public ArbreCatShopId(BigDecimal catId, String catName, String catLink) { this.catId = catId; this.catName = catName; this.catLink = catLink; } public ArbreCatShopId(String catWebLink, String catPtName, BigDecimal catShopNbr, BigDecimal catId, BigDecimal catIdP, String catName, String catLink, BigDecimal catCom, BigDecimal catWeight, Boolean catShop, Boolean catAnn, String catClass, String catLogo, BigDecimal catLevel) { this.catWebLink = catWebLink; this.catPtName = catPtName; this.catShopNbr = catShopNbr; this.catId = catId; this.catIdP = catIdP; this.catName = catName; this.catLink = catLink; this.catCom = catCom; this.catWeight = catWeight; this.catShop = catShop; this.catAnn = catAnn; this.catClass = catClass; this.catLogo = catLogo; this.catLevel = catLevel; } public String getCatWebLink() { return this.catWebLink; } public void setCatWebLink(String catWebLink) { this.catWebLink = catWebLink; } public String getCatPtName() { return this.catPtName; } public void setCatPtName(String catPtName) { this.catPtName = catPtName; } public BigDecimal getCatShopNbr() { return this.catShopNbr; } public void setCatShopNbr(BigDecimal catShopNbr) { this.catShopNbr = catShopNbr; } public BigDecimal getCatId() { return this.catId; } public void setCatId(BigDecimal catId) { this.catId = catId; } public BigDecimal getCatIdP() { return this.catIdP; } public void setCatIdP(BigDecimal catIdP) { this.catIdP = catIdP; } public String getCatName() { return this.catName; } public void setCatName(String catName) { this.catName = catName; } public String getCatLink() { return this.catLink; } public void setCatLink(String catLink) { this.catLink = catLink; } public BigDecimal getCatCom() { return this.catCom; } public void setCatCom(BigDecimal catCom) { this.catCom = catCom; } public BigDecimal getCatWeight() { return this.catWeight; } public void setCatWeight(BigDecimal catWeight) { this.catWeight = catWeight; } public Boolean getCatShop() { return this.catShop; } public void setCatShop(Boolean catShop) { this.catShop = catShop; } public Boolean getCatAnn() { return this.catAnn; } public void setCatAnn(Boolean catAnn) { this.catAnn = catAnn; } public String getCatClass() { return this.catClass; } public void setCatClass(String catClass) { this.catClass = catClass; } public String getCatLogo() { return this.catLogo; } public void setCatLogo(String catLogo) { this.catLogo = catLogo; } public BigDecimal getCatLevel() { return this.catLevel; } public void setCatLevel(BigDecimal catLevel) { this.catLevel = catLevel; } @Override public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof ArbreCatShopId)) return false; ArbreCatShopId castOther = (ArbreCatShopId) other; return ((this.getCatWebLink() == castOther.getCatWebLink()) || (this .getCatWebLink() != null && castOther.getCatWebLink() != null && this .getCatWebLink().equals(castOther.getCatWebLink()))) && ((this.getCatPtName() == castOther.getCatPtName()) || (this .getCatPtName() != null && castOther.getCatPtName() != null && this .getCatPtName().equals(castOther.getCatPtName()))) && ((this.getCatShopNbr() == castOther.getCatShopNbr()) || (this .getCatShopNbr() != null && castOther.getCatShopNbr() != null && this .getCatShopNbr().equals(castOther.getCatShopNbr()))) && ((this.getCatId() == castOther.getCatId()) || (this .getCatId() != null && castOther.getCatId() != null && this .getCatId().equals(castOther.getCatId()))) && ((this.getCatIdP() == castOther.getCatIdP()) || (this .getCatIdP() != null && castOther.getCatIdP() != null && this .getCatIdP().equals(castOther.getCatIdP()))) && ((this.getCatName() == castOther.getCatName()) || (this .getCatName() != null && castOther.getCatName() != null && this .getCatName().equals(castOther.getCatName()))) && ((this.getCatLink() == castOther.getCatLink()) || (this .getCatLink() != null && castOther.getCatLink() != null && this .getCatLink().equals(castOther.getCatLink()))) && ((this.getCatCom() == castOther.getCatCom()) || (this .getCatCom() != null && castOther.getCatCom() != null && this .getCatCom().equals(castOther.getCatCom()))) && ((this.getCatWeight() == castOther.getCatWeight()) || (this .getCatWeight() != null && castOther.getCatWeight() != null && this .getCatWeight().equals(castOther.getCatWeight()))) && ((this.getCatShop() == castOther.getCatShop()) || (this .getCatShop() != null && castOther.getCatShop() != null && this .getCatShop().equals(castOther.getCatShop()))) && ((this.getCatAnn() == castOther.getCatAnn()) || (this .getCatAnn() != null && castOther.getCatAnn() != null && this .getCatAnn().equals(castOther.getCatAnn()))) && ((this.getCatClass() == castOther.getCatClass()) || (this .getCatClass() != null && castOther.getCatClass() != null && this .getCatClass().equals(castOther.getCatClass()))) && ((this.getCatLogo() == castOther.getCatLogo()) || (this .getCatLogo() != null && castOther.getCatLogo() != null && this .getCatLogo().equals(castOther.getCatLogo()))) && ((this.getCatLevel() == castOther.getCatLevel()) || (this .getCatLevel() != null && castOther.getCatLevel() != null && this .getCatLevel().equals(castOther.getCatLevel()))); } @Override public int hashCode() { int result = 17; result = 37 * result + (this.getCatWebLink() == null ? 0 : this.getCatWebLink() .hashCode()); result = 37 * result + (this.getCatPtName() == null ? 0 : this.getCatPtName().hashCode()); result = 37 * result + (this.getCatShopNbr() == null ? 0 : this.getCatShopNbr() .hashCode()); result = 37 * result + (this.getCatId() == null ? 0 : this.getCatId().hashCode()); result = 37 * result + (this.getCatIdP() == null ? 0 : this.getCatIdP().hashCode()); result = 37 * result + (this.getCatName() == null ? 0 : this.getCatName().hashCode()); result = 37 * result + (this.getCatLink() == null ? 0 : this.getCatLink().hashCode()); result = 37 * result + (this.getCatCom() == null ? 0 : this.getCatCom().hashCode()); result = 37 * result + (this.getCatWeight() == null ? 0 : this.getCatWeight().hashCode()); result = 37 * result + (this.getCatShop() == null ? 0 : this.getCatShop().hashCode()); result = 37 * result + (this.getCatAnn() == null ? 0 : this.getCatAnn().hashCode()); result = 37 * result + (this.getCatClass() == null ? 0 : this.getCatClass().hashCode()); result = 37 * result + (this.getCatLogo() == null ? 0 : this.getCatLogo().hashCode()); result = 37 * result + (this.getCatLevel() == null ? 0 : this.getCatLevel().hashCode()); return result; } } la requete que Hibernate Genere ( qui marche au niveau du terminal sql): Hibernate: select arbrecatsh0_.CAT_WEB_LINK as CAT1_14_, arbrecatsh0_.CAT_PT_NAME as CAT2_14_, arbrecatsh0_.CAT_SHOP_NBR as CAT3_14_, arbrecatsh0_.CAT_ID as CAT4_14_, arbrecatsh0_.CAT_ID_P as CAT5_14_, arbrecatsh0_.CAT_NAME as CAT6_14_, arbrecatsh0_.CAT_LINK as CAT7_14_, arbrecatsh0_.CAT_COM as CAT8_14_, arbrecatsh0_.CAT_WEIGHT as CAT9_14_, arbrecatsh0_.CAT_SHOP as CAT10_14_, arbrecatsh0_.CAT_ANN as CAT11_14_, arbrecatsh0_.CAT_CLASS as CAT12_14_, arbrecatsh0_.CAT_LOGO as CAT13_14_, arbrecatsh0_.CAT_LEVEL as CAT14_14_ from HR.ARBRE_CAT_SHOP arbrecatsh0_ Lien vers le commentaire Partager sur d’autres sites More sharing options...
lordv Posté(e) le 2 juin 2011 Auteur Partager Posté(e) le 2 juin 2011 je l'ai résolu en modifiant la class ArbreCatShop et le fichier de mapping ArbreCatShop.hbm.xml de toute façon elle me sert juste à afficher les résultats. Voici le contenu de mes deux fichiers : package com.ndak.hjx.th; import java.util.List; import org.hibernate.Session; import com.ndak.hjx.db.HibernateUtil; @SuppressWarnings("serial") public class ArbreCatShop { private String cat_web_link; private String cat_pt_name; private Long cat_shop_nbr; private Long cat_id; private Long cat_id_p; private String cat_name; private String cat_link; private Long cat_com; private Long cat_weight; private Boolean cat_shop; private Boolean cat_ann; private String cat_class; private String cat_logo; private Long cat_level; /** * @param cat_web_link the cat_web_link to set */ public void setCat_web_link(String cat_web_link) { this.cat_web_link = cat_web_link; } /** * @return the cat_web_link */ public String getCat_web_link() { return this.cat_web_link; } /** * @param cat_pt_name the cat_pt_name to set */ public void setCat_pt_name(String cat_pt_name) { this.cat_pt_name = cat_pt_name; } /** * @return the cat_pt_name */ public String getCat_pt_name() { return this.cat_pt_name; } /** * @param cat_shop_nbr the cat_shop_nbr to set */ public void setCat_shop_nbr(Long cat_shop_nbr) { this.cat_shop_nbr = cat_shop_nbr; } /** * @return the cat_shop_nbr */ public Long getCat_shop_nbr() { return this.cat_shop_nbr; } /** * @param cat_id the cat_id to set */ public void setCat_id(Long cat_id) { this.cat_id = cat_id; } /** * @return the cat_id */ public Long getCat_id() { return this.cat_id; } /** * @param cat_id_p the cat_id_p to set */ public void setCat_id_p(Long cat_id_p) { this.cat_id_p = cat_id_p; } /** * @return the cat_id_p */ public Long getCat_id_p() { return this.cat_id_p; } /** * @param cat_name the cat_name to set */ public void setCat_name(String cat_name) { this.cat_name = cat_name; } /** * @return the cat_name */ public String getCat_name() { return this.cat_name; } /** * @param cat_link the cat_link to set */ public void setCat_link(String cat_link) { this.cat_link = cat_link; } /** * @return the cat_link */ public String getCat_link() { return this.cat_link; } /** * @param cat_com the cat_com to set */ public void setCat_com(Long cat_com) { this.cat_com = cat_com; } /** * @return the cat_com */ public Long getCat_com() { return this.cat_com; } /** * @param cat_weight the cat_weight to set */ public void setCat_weight(Long cat_weight) { this.cat_weight = cat_weight; } /** * @return the cat_weight */ public Long getCat_weight() { return this.cat_weight; } /** * @param cat_shop the cat_shop to set */ public void setCat_shop(Boolean cat_shop) { this.cat_shop = cat_shop; } /** * @return the cat_shop */ public Boolean getCat_shop() { return this.cat_shop; } /** * @param cat_ann the cat_ann to set */ public void setCat_ann(Boolean cat_ann) { this.cat_ann = cat_ann; } /** * @return the cat_ann */ public Boolean getCat_ann() { return this.cat_ann; } /** * @param cat_class the cat_class to set */ public void setCat_class(String cat_class) { this.cat_class = cat_class; } /** * @return the cat_class */ public String getCat_class() { return this.cat_class; } /** * @param cat_logo the cat_logo to set */ public void setCat_logo(String cat_logo) { this.cat_logo = cat_logo; } /** * @return the cat_logo */ public String getCat_logo() { return this.cat_logo; } /** * @param cat_level the cat_level to set */ public void setCat_level(Long cat_level) { this.cat_level = cat_level; } /** * @return the cat_level */ public Long getCat_level() { return this.cat_level; } public static synchronized List<ArbreCatShop> GetSections() { Session session = HibernateUtil.currentSession(); //debut transaction session.beginTransaction(); String orgChartQuery = "select * from arbre_cat_shop WHERE cat_id_p is null AND cat_level=1 order by cat_name "; @SuppressWarnings("unchecked") List<ArbreCatShop> result = session.createSQLQuery(orgChartQuery) .addEntity("a", ArbreCatShop.class).list(); session.getTransaction().commit(); HibernateUtil.closeSession(); return result; } public static synchronized List<ArbreCatShop> GetSectionsCom() { Session session = HibernateUtil.currentSession(); //debut transaction session.beginTransaction(); String orgChartQuery = "select * from arbre_cat_shop WHERE cat_id_p is null AND cat_level=1 AND cat_com=1 order by cat_name "; @SuppressWarnings("unchecked") List<ArbreCatShop> result = session.createSQLQuery(orgChartQuery) .addEntity("a", ArbreCatShop.class).list(); session.getTransaction().commit(); HibernateUtil.closeSession(); return result; } public static synchronized List<ArbreCatShop> GetCategories() { Session session = HibernateUtil.currentSession(); //debut transaction session.beginTransaction(); String orgChartQuery = "select * from arbre_cat_shop WHERE cat_level=2 order by cat_name "; @SuppressWarnings("unchecked") List<ArbreCatShop> result = session.createSQLQuery(orgChartQuery) .addEntity("a", ArbreCatShop.class).list(); session.getTransaction().commit(); HibernateUtil.closeSession(); return result; } public static synchronized List<ArbreCatShop> GetCategories(int cat_id) { Session session = HibernateUtil.currentSession(); //debut transaction session.beginTransaction(); String orgChartQuery = "select * from arbre_cat_shop WHERE cat_level=2 AND a.cat_id=? order by cat_name "; @SuppressWarnings("unchecked") List<ArbreCatShop> result = session.createSQLQuery(orgChartQuery) .addEntity("a", ArbreCatShop.class).setInteger(0, cat_id).list(); session.getTransaction().commit(); HibernateUtil.closeSession(); return result; } public static synchronized List<ArbreCatShop> GetTreeSections() { Session session = HibernateUtil.currentSession(); //debut transaction session.beginTransaction(); String orgChartQuery = "select a.* from arbre_cat_shop a start with a.cat_id_p is null connect by a.cat_id_p = prior a.cat_id "; @SuppressWarnings("unchecked") List<ArbreCatShop> result = session.createSQLQuery(orgChartQuery) .addEntity("a", ArbreCatShop.class).list(); session.getTransaction().commit(); HibernateUtil.closeSession(); return result; } public static synchronized List<ArbreCatShop> GetTreeSections(int cat_id) { Session session = HibernateUtil.currentSession(); //debut transaction session.beginTransaction(); String orgChartQuery = "select a.* from arbre_cat_shop a start with a.cat_id=? connect by a.cat_id_p = prior a.cat_id "; @SuppressWarnings("unchecked") List<ArbreCatShop> result = session.createSQLQuery(orgChartQuery) .addEntity("a", ArbreCatShop.class).setInteger(0, cat_id).list(); session.getTransaction().commit(); HibernateUtil.closeSession(); return result; } } le fichier de mapping : <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated May 31, 2011 11:56:33 PM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.ndak.hjx.th.ArbreCatShop" table="ARBRE_CAT_SHOP" schema="HR"> <id name="cat_id" column="cat_id"> <generator class="assigned"/> </id> <property name="cat_id_p"/> <property name="cat_name"/> <property name="cat_link"/> <property name="cat_com"/> <property name="cat_weight"/> <property name="cat_shop"/> <property name="cat_ann"/> <property name="cat_class"/> <property name="cat_logo"/> <property name="cat_web_link"/> <property name="cat_pt_name"/> <property name="cat_shop_nbr"/> </class> </hibernate-mapping> Merci pour tout. Lien vers le commentaire Partager sur d’autres sites More sharing options...
fabien29200 Posté(e) le 2 juin 2011 Partager Posté(e) le 2 juin 2011 Salut ! Bienvenue dans le monde du J2EE Je me posais la question de l'utilisé de la classe ArbreCatShopId dans ton premier post, mais tu l'as enlevée dans le second. Je suis pas un pro de Hibernate, mais pourquoi tu fais un commit sur des requêtes de lecture ? Autres remarques : - tu as des noms de méthode qui commencent par une majuscule (ce qui est réservé aux classes en Java). - Les méthodes liées à la base de données ne devraient pas se trouver dans l'objet data en lui même. En général on crée une classe spécifique (genre ArbreCatShopDAO) qui réunit tous les appels à la base de données pour cette classe (lecture, écriture, recherche). Bon courage ! Lien vers le commentaire Partager sur d’autres sites More sharing options...
lordv Posté(e) le 2 juin 2011 Auteur Partager Posté(e) le 2 juin 2011 Salut C’est un ami qui m’a conseillé Hibernate. En plus je ne voulais pas recoder toutes mes classes DAO de PDO à JDBC . Cela fait deux jours que je fouine dedans pour l’instant je veux juste comprendre le principe après je nettoierais pour les noms de méthodes et autre.je migre mon code php à cause de problèmes avec les types clob que je n’arrivais pas à résoudre avec les driver pdo_oci.merci pour le conseil du DAO. J’ai utilisé Hibernate Tools pour générer ces classes et je pense qu’elles sont seulement valide pour les tables je vais devoir modifier manuellement pour les vues. Encore merci pour les conseils. Lien vers le commentaire Partager sur d’autres sites More sharing options...
Messages recommandés
Archivé
Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.