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

Spring Java Discussion :

Spring enlever l'interface devant variable [Débutant(e)]


Sujet :

Spring Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    graphisme & impression
    Inscrit en
    Mars 2011
    Messages
    118
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : graphisme & impression

    Informations forums :
    Inscription : Mars 2011
    Messages : 118
    Par défaut Spring enlever l'interface devant variable
    Bonjour à tous,

    Je suis en train de faire le tutoriel sur JPA disponible sur ce site et j'ai un petit problème pour l'adapter à mon code.

    J'utilise hibernate et spring pour implémenter mes couches de services. Dans le tutoriel, les couches sont implémenter via leur interface.
    Est-il possible de me montrer comment déclarer une couche sans l'interface?
    (j'ai une méthode pour valider un contrainte d'intégrité qui n'est pas dans l'interface)!?

    Merci beaucoup pour votre aide.
    Bonne journée

    ce que je voudrais faire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    // couche [dao]
    private Dao dao;
    ex tutoriel
    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
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
     
    package service;
     
    import java.util.List;
     
    import org.springframework.transaction.annotation.Transactional;
     
    import dao.IDao;
    import entites.Article;
    import entites.Categorie;
     
    //ttes les méthodes de la classe se déroulent dans une transaction
    @Transactional
    public class Service implements IService {
     
    	// couche [dao]
    	private IDao dao;
     
    	public IDao getDao() {
    		return dao;
    	}
     
    	public void setDao(IDao dao) {
    		this.dao = dao;
    	}
     
    	// catégories
    	public Categorie getCategorie(Long categorieId) {
    		return dao.getCategorie(categorieId);
    	}
     
    	@SuppressWarnings("unchecked")
    	public List<Categorie> getAllCategories() {
    		return dao.getAllCategories();
    	}
     
    	@SuppressWarnings("unchecked")
    	public List<Categorie> getAllCategoriesWithNomLike(String modeleNom) {
    		return dao.getAllCategoriesWithNomLike(modeleNom);
    	}
     
    	@SuppressWarnings("unchecked")
    	public List<Article> getArticlesFromCategorie(Long categorieId) {
    		return dao.getArticlesFromCategorie(categorieId);
    	}
     
    	public Categorie updateCategorie(Categorie categorie) {
    		return dao.updateCategorie(categorie);
    	}
     
    	public Categorie saveCategorie(Categorie categorie) {
    		return dao.saveCategorie(categorie);
    	};
     
    	public void deleteCategorie(Long categorieId) {
    		dao.deleteCategorie(categorieId);
    	}
     
    	public void saveCategoriesWithArticles(Categorie[] categories) {
    		for (Categorie categorie : categories) {
    			dao.saveCategorie(categorie);
    			for (Article article : categorie.getArticles()) {
    				dao.saveArticle(article);
    			}
    		}
    	}
     
    	public void deleteCategoriesWithArticles(Categorie[] categories) {
    		for (Categorie categorie : categories) {
    			for (Article article : getArticlesFromCategorie(categorie.getId())) {
    				dao.deleteArticle(article.getId());
    			}
    			dao.deleteCategorie(categorie.getId());
    		}
    	}
     
    	// articles
    	public Article getArticle(Long articleId) {
    		return dao.getArticle(articleId);
    	}
     
    	@SuppressWarnings("unchecked")
    	public List<Article> getAllArticles() {
    		return dao.getAllArticles();
    	}
     
    	@SuppressWarnings("unchecked")
    	public List<Article> getAllArticlesWithNomLike(String modeleNom) {
    		return dao.getAllArticlesWithNomLike(modeleNom);
    	};
     
    	public Article saveArticle(Article article) {
    		return dao.saveArticle(article);
    	}
     
    	public Article updateArticle(Article article) {
    		return dao.updateArticle(article);
    	}
     
    	public void deleteArticle(Long articleId) {
    		dao.deleteArticle(articleId);
    	}
     
    }
     
    package dao;
     
    import java.util.List;
     
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
     
    import entites.Article;
    import entites.Categorie;
     
    public class Dao implements IDao {
     
    	@PersistenceContext
    	private EntityManager em;
     
    	// catégories
     
    	public Categorie getCategorie(Long categorieId) {
    		return em.find(Categorie.class, categorieId);
    	}
     
    	@SuppressWarnings("unchecked")
    	public List<Categorie> getAllCategories() {
    		return em.createQuery("select c from Categorie c").getResultList();
    	}
     
    	@SuppressWarnings("unchecked")
    	public List<Categorie> getAllCategoriesWithNomLike(String modeleNom) {
    		return em.createQuery("select c from Categorie c where c.nom like :modele").setParameter("modele", modeleNom).getResultList();
    	}
     
    	@SuppressWarnings("unchecked")
    	public List<Article> getArticlesFromCategorie(Long categorieId) {
    		return em.createQuery("select a from Article a where a.categorie.id=:categorieId").setParameter("categorieId", categorieId).getResultList();
    	}
     
    	public Categorie updateCategorie(Categorie categorie) {
    		return em.merge(categorie);
    	}
     
    	public Categorie saveCategorie(Categorie categorie) {
    		em.persist(categorie);
    		return categorie;
    	}
     
    	public void deleteCategorie(Long categorieId) {
    		Categorie categorie = em.find(Categorie.class, categorieId);
    		if (categorie == null) {
    			throw new DaoException(30);
    		}
    		em.remove(categorie);
    	}
     
     
    	// articles
     
    	public Article getArticle(Long articleId) {
    		return em.find(Article.class, articleId);
    	}
     
    	@SuppressWarnings("unchecked")
    	public List<Article> getAllArticles() {
    		return em.createQuery("select a from Article a").getResultList();
    	}
     
    	@SuppressWarnings("unchecked")
    	public List<Article> getAllArticlesWithNomLike(String modeleNom) {
    		return em.createQuery("select a from Article a where a.nom like :modele").setParameter("modele", modeleNom).getResultList();
    	}
     
    	public Article saveArticle(Article article) {
    		em.persist(article);
    		return article;
    	}
     
    	public Article updateArticle(Article article) {
    		return em.merge(article);
    	}
     
    	public void deleteArticle(Long articleId) {
    		Article article = em.find(Article.class, articleId);
    		if (article == null) {
    			throw new DaoException(20);
    		}
    		em.remove(article);
    	}
     
     
     
    }
     
    package dao;
     
    import java.util.List;
     
    import entites.Article;
    import entites.Categorie;
     
    public interface IDao {
     
    	// articles
    	public Article getArticle(Long articleId);
     
    	public List<Article> getAllArticles();
     
    	public Article saveArticle(Article article);
     
    	public Article updateArticle(Article article);
     
    	public void deleteArticle(Long articleId);
     
    	public List<Article> getAllArticlesWithNomLike(String modeleNom);
     
    	// catégories
    	public Categorie getCategorie(Long categorieId);
     
    	public List<Categorie> getAllCategories();
     
    	public Categorie saveCategorie(Categorie categorie);
     
     
    	public Categorie updateCategorie(Categorie categorie);
     
    	public void deleteCategorie(Long categorieId);
     
    	public List<Categorie> getAllCategoriesWithNomLike(String modeleNom);
     
    	public List<Article> getArticlesFromCategorie(Long categorieId);
     
    }

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Février 2009
    Messages
    43
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 43
    Par défaut Oui mais c'est dommage
    Hello,

    Oui tu peux déclarer ta variable dans le type de la classe injectée mais c'est quand même dommage, tu perds le découplage que l'interface t'apporte et donc la possibilité d'injecter une implémentation différente si besoin (Evolutions, tests...).

    A+

Discussions similaires

  1. [SHELL] Enlever \n d'une variable
    Par magicwill dans le forum Shell et commandes GNU
    Réponses: 3
    Dernier message: 26/07/2007, 13h59
  2. enlever les 0 devant une chaine
    Par linux dans le forum MS SQL Server
    Réponses: 6
    Dernier message: 17/03/2006, 11h06
  3. enlever les '0' devant une chaine
    Par linux dans le forum Langage SQL
    Réponses: 2
    Dernier message: 16/03/2006, 19h12
  4. [Mail] Enlever les Slash devant les apostraphes
    Par arnolem dans le forum Langage
    Réponses: 6
    Dernier message: 21/02/2006, 22h39

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