IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

Problème classe Java générées via Hibernate Tools


Sujet :

Hibernate Java

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 8
    Points : 8
    Points
    8
    Par défaut Problème classe Java générées via Hibernate Tools
    Bonjour,

    Dans le cas d'une refonte d'une application web, je repars uniquement de la base de données (Oracle).
    Comment exploiter les classes générées par Hibernate Tools ?

    Pour une table Profil par exemple :
    Nom : tableProfil.png
Affichages : 68
Taille : 17,1 Ko

    Le plugin hibernate Tools me génére deux classes : une pour la primary key et une autre pour les colonnes
    Classe Profil.java : qui ne mappe pas correctement les colonnes de ma table.
    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
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
     
    package com.xxx.hibernateDB.database.entities;
     
    // Generated 27 janv. 2015 16:25:25 by Hibernate Tools 4.0.0
     
    import javax.persistence.AttributeOverride;
    import javax.persistence.AttributeOverrides;
    import javax.persistence.Column;
    import javax.persistence.EmbeddedId;
    import javax.persistence.Entity;
    import javax.persistence.Table;
     
    /**
     * Profil generated by hbm2java
     */
    @Entity
    @Table(name = "PROFIL", schema = "USR_ROUTAGE")
    public class Profil implements java.io.Serializable {
     
    	private ProfilId id;
     
    	public Profil() {
    	}
     
    	public Profil(ProfilId id) {
    		this.id = id;
    	}
     
    	@EmbeddedId
    	@AttributeOverrides({
    			@AttributeOverride(name = "idProfil", column = @Column(name = "ID_PROFIL", nullable = false, precision = 22, scale = 0)),
    			@AttributeOverride(name = "idPointEntree", column = @Column(name = "ID_POINT_ENTREE", length = 32)),
    			@AttributeOverride(name = "proIdProfil", column = @Column(name = "PRO_ID_PROFIL", precision = 22, scale = 0)),
    			@AttributeOverride(name = "idSegment", column = @Column(name = "ID_SEGMENT", length = 100)),
    			@AttributeOverride(name = "idCible", column = @Column(name = "ID_CIBLE", precision = 22, scale = 0)),
    			@AttributeOverride(name = "poiIdPointEntree", column = @Column(name = "POI_ID_POINT_ENTREE", length = 32)),
    			@AttributeOverride(name = "nomProfil", column = @Column(name = "NOM_PROFIL", nullable = false)),
    			@AttributeOverride(name = "typeProfil", column = @Column(name = "TYPE_PROFIL", nullable = false, length = 100)),
    			@AttributeOverride(name = "descriptionProfil", column = @Column(name = "DESCRIPTION_PROFIL", length = 10)) })
    	public ProfilId getId() {
    		return this.id;
    	}
     
    	public void setId(ProfilId id) {
    		this.id = id;
    	}
     
    }
    Une classe ProfilId:
    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
    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
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    package com.xxx.hibernateDB.database.entities;
     
    // Generated 27 janv. 2015 16:25:25 by Hibernate Tools 4.0.0
     
    import java.math.BigDecimal;
    import javax.persistence.Column;
    import javax.persistence.Embeddable;
     
    /**
     * ProfilId generated by hbm2java
     */
    @Embeddable
    public class ProfilId implements java.io.Serializable {
     
    	private BigDecimal idProfil;
    	private String idPointEntree;
    	private BigDecimal proIdProfil;
    	private String idSegment;
    	private BigDecimal idCible;
    	private String poiIdPointEntree;
    	private String nomProfil;
    	private String typeProfil;
    	private String descriptionProfil;
     
    	public ProfilId() {
    	}
     
    	public ProfilId(BigDecimal idProfil, String nomProfil, String typeProfil) {
    		this.idProfil = idProfil;
    		this.nomProfil = nomProfil;
    		this.typeProfil = typeProfil;
    	}
     
    	public ProfilId(BigDecimal idProfil, String idPointEntree,
    			BigDecimal proIdProfil, String idSegment, BigDecimal idCible,
    			String poiIdPointEntree, String nomProfil, String typeProfil,
    			String descriptionProfil) {
    		this.idProfil = idProfil;
    		this.idPointEntree = idPointEntree;
    		this.proIdProfil = proIdProfil;
    		this.idSegment = idSegment;
    		this.idCible = idCible;
    		this.poiIdPointEntree = poiIdPointEntree;
    		this.nomProfil = nomProfil;
    		this.typeProfil = typeProfil;
    		this.descriptionProfil = descriptionProfil;
    	}
     
    	@Column(name = "ID_PROFIL", nullable = false, precision = 22, scale = 0)
    	public BigDecimal getIdProfil() {
    		return this.idProfil;
    	}
     
    	public void setIdProfil(BigDecimal idProfil) {
    		this.idProfil = idProfil;
    	}
     
    	@Column(name = "ID_POINT_ENTREE", length = 32)
    	public String getIdPointEntree() {
    		return this.idPointEntree;
    	}
     
    	public void setIdPointEntree(String idPointEntree) {
    		this.idPointEntree = idPointEntree;
    	}
     
    	@Column(name = "PRO_ID_PROFIL", precision = 22, scale = 0)
    	public BigDecimal getProIdProfil() {
    		return this.proIdProfil;
    	}
     
    	public void setProIdProfil(BigDecimal proIdProfil) {
    		this.proIdProfil = proIdProfil;
    	}
     
    	@Column(name = "ID_SEGMENT", length = 100)
    	public String getIdSegment() {
    		return this.idSegment;
    	}
     
    	public void setIdSegment(String idSegment) {
    		this.idSegment = idSegment;
    	}
     
    	@Column(name = "ID_CIBLE", precision = 22, scale = 0)
    	public BigDecimal getIdCible() {
    		return this.idCible;
    	}
     
    	public void setIdCible(BigDecimal idCible) {
    		this.idCible = idCible;
    	}
     
    	@Column(name = "POI_ID_POINT_ENTREE", length = 32)
    	public String getPoiIdPointEntree() {
    		return this.poiIdPointEntree;
    	}
     
    	public void setPoiIdPointEntree(String poiIdPointEntree) {
    		this.poiIdPointEntree = poiIdPointEntree;
    	}
     
    	@Column(name = "NOM_PROFIL", nullable = false)
    	public String getNomProfil() {
    		return this.nomProfil;
    	}
     
    	public void setNomProfil(String nomProfil) {
    		this.nomProfil = nomProfil;
    	}
     
    	@Column(name = "TYPE_PROFIL", nullable = false, length = 100)
    	public String getTypeProfil() {
    		return this.typeProfil;
    	}
     
    	public void setTypeProfil(String typeProfil) {
    		this.typeProfil = typeProfil;
    	}
     
    	@Column(name = "DESCRIPTION_PROFIL", length = 10)
    	public String getDescriptionProfil() {
    		return this.descriptionProfil;
    	}
     
    	public void setDescriptionProfil(String descriptionProfil) {
    		this.descriptionProfil = descriptionProfil;
    	}
     
    	public boolean equals(Object other) {
    		if ((this == other))
    			return true;
    		if ((other == null))
    			return false;
    		if (!(other instanceof ProfilId))
    			return false;
    		ProfilId castOther = (ProfilId) other;
     
    		return ((this.getIdProfil() == castOther.getIdProfil()) || (this
    				.getIdProfil() != null && castOther.getIdProfil() != null && this
    				.getIdProfil().equals(castOther.getIdProfil())))
    				&& ((this.getIdPointEntree() == castOther.getIdPointEntree()) || (this
    						.getIdPointEntree() != null
    						&& castOther.getIdPointEntree() != null && this
    						.getIdPointEntree()
    						.equals(castOther.getIdPointEntree())))
    				&& ((this.getProIdProfil() == castOther.getProIdProfil()) || (this
    						.getProIdProfil() != null
    						&& castOther.getProIdProfil() != null && this
    						.getProIdProfil().equals(castOther.getProIdProfil())))
    				&& ((this.getIdSegment() == castOther.getIdSegment()) || (this
    						.getIdSegment() != null
    						&& castOther.getIdSegment() != null && this
    						.getIdSegment().equals(castOther.getIdSegment())))
    				&& ((this.getIdCible() == castOther.getIdCible()) || (this
    						.getIdCible() != null && castOther.getIdCible() != null && this
    						.getIdCible().equals(castOther.getIdCible())))
    				&& ((this.getPoiIdPointEntree() == castOther
    						.getPoiIdPointEntree()) || (this.getPoiIdPointEntree() != null
    						&& castOther.getPoiIdPointEntree() != null && this
    						.getPoiIdPointEntree().equals(
    								castOther.getPoiIdPointEntree())))
    				&& ((this.getNomProfil() == castOther.getNomProfil()) || (this
    						.getNomProfil() != null
    						&& castOther.getNomProfil() != null && this
    						.getNomProfil().equals(castOther.getNomProfil())))
    				&& ((this.getTypeProfil() == castOther.getTypeProfil()) || (this
    						.getTypeProfil() != null
    						&& castOther.getTypeProfil() != null && this
    						.getTypeProfil().equals(castOther.getTypeProfil())))
    				&& ((this.getDescriptionProfil() == castOther
    						.getDescriptionProfil()) || (this
    						.getDescriptionProfil() != null
    						&& castOther.getDescriptionProfil() != null && this
    						.getDescriptionProfil().equals(
    								castOther.getDescriptionProfil())));
    	}
     
    	public int hashCode() {
    		int result = 17;
     
    		result = 37 * result
    				+ (getIdProfil() == null ? 0 : this.getIdProfil().hashCode());
    		result = 37
    				* result
    				+ (getIdPointEntree() == null ? 0 : this.getIdPointEntree()
    						.hashCode());
    		result = 37
    				* result
    				+ (getProIdProfil() == null ? 0 : this.getProIdProfil()
    						.hashCode());
    		result = 37 * result
    				+ (getIdSegment() == null ? 0 : this.getIdSegment().hashCode());
    		result = 37 * result
    				+ (getIdCible() == null ? 0 : this.getIdCible().hashCode());
    		result = 37
    				* result
    				+ (getPoiIdPointEntree() == null ? 0 : this
    						.getPoiIdPointEntree().hashCode());
    		result = 37 * result
    				+ (getNomProfil() == null ? 0 : this.getNomProfil().hashCode());
    		result = 37
    				* result
    				+ (getTypeProfil() == null ? 0 : this.getTypeProfil()
    						.hashCode());
    		result = 37
    				* result
    				+ (getDescriptionProfil() == null ? 0 : this
    						.getDescriptionProfil().hashCode());
    		return result;
    	}
     
    }
    Ma question est comment faire une rêquete pour avoir une liste de tous les profils ?

    Voici ma fonction qui récupère tous les profils :
    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
    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
    package com.xxx.hibernateDB.database.dao.impl;
    
    import java.util.Collection;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.Query;
    
    import org.hibernate.Criteria;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.boot.registry.StandardServiceRegistry;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.dao.DataAccessException;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.xxx.hibernateDB.database.dao.ProfilDao;
    import com.xxx.hibernateDB.database.entities.PointEntree;
    import com.xxx.hibernateDB.database.entities.Profil;
    
    
    /**
     * ProfilPE DAO 
     * @author 
     *
     */
    @Repository("profilDaoImpl")
    @Transactional(propagation = Propagation.REQUIRED)
    public class ProfilDaoImpl implements ProfilDao  { 
    
    	private static final Logger LOGGER = LoggerFactory.getLogger(ProfilDaoImpl.class);
    
    	private EntityManager entityManager;
    
    	private static final String SELECT_QUERY = "select ID_PROFIL from Profil p";
    	
    	@PersistenceContext
    	public void setEntityManager(EntityManager entityManager) {
    		this.entityManager = entityManager;
    	}
    	
    	public EntityManager getEntityManager() {
    		return entityManager;
    	}
    	/**
    	 * Insert Profil
    	 * @param transientInstance
    	 */
    	public void persist(Profil transientInstance) {
    		LOGGER.debug("persisting Profil instance");
    		try {
    			entityManager.persist(transientInstance);
    			LOGGER.debug("persist successful");
    		} catch (RuntimeException re) {
    			LOGGER.error("persist failed", re);
    			throw re;
    		}
    	}
    	public Collection<Profil> selectAll() {
    		Query query = entityManager.createQuery(SELECT_QUERY);
    		Collection<Profil> persons = (Collection<Profil>) query.getResultList();
    		return persons;
    	}
    	public Collection<Profil> getListeProfilPE() {
    		return null;
    	}
    }
    Je ne peux pas utiliser le nom Profil dans ma rêquete puisqu'il ne correspond pas a la configuration de ma table .

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 804
    Points
    48 804
    Par défaut
    La génération a l'air un peu foirax. Je ferais le mapping à la main si j'étais toi.

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 8
    Points : 8
    Points
    8
    Par défaut
    J'ai beaucoup de tables dans ma base, c'est justement pour ça que je voulais utiliser un outils pour générer les classes.

    Tu connais une autre alternative a hibernate Tools ?

  4. #4
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 804
    Points
    48 804
    Par défaut
    Hibernate fait ce qu'il peut avec les contraintes de la table. Si tu te retrouve avec les 3/4 de la table en clé primaire, il y a un problème sur la table même, aucun outil ne va te sauver.

Discussions similaires

  1. Utilité des classes DAO générées avec Hibernate Tools
    Par marooned dans le forum Hibernate
    Réponses: 12
    Dernier message: 13/01/2013, 20h17
  2. [Core] Problème de connexion à MySQL via Hibernate
    Par wadi3 dans le forum Hibernate
    Réponses: 6
    Dernier message: 16/06/2012, 16h40
  3. Réponses: 2
    Dernier message: 27/02/2012, 11h53
  4. JAVA + SFTP via J2SSH TOOLS
    Par DragonLebna dans le forum API standards et tierces
    Réponses: 0
    Dernier message: 02/06/2009, 15h55
  5. [Plugin][Hibernate Tools] java.lang.NoClassDefFoundError
    Par notrustinyou dans le forum Eclipse Java
    Réponses: 2
    Dernier message: 28/11/2005, 17h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo