Vous devez être membre et vous identifier pour publier un article.
Les visiteurs peuvent toutefois commenter chaque article par une réponse.
Hibernate et JPA; Accès aux membres ou via les accesseurs
Article publié le 05/10/2010 09:21:02Dans certaines applications, je continue à utiliser un bon vieux DAO avec JDBC, mais c’est quand même beaucoup plus rapide de développer avec Hibernate.
Le problème, c’est que le modèle (la logique métier) se trouve dans un projet Java qui est utilisé comme dépendance Maven dans le projet qui s’occupe de la persistance des données. Il n’est donc pas question de modifier le modèle pour l’adapter à Hibernate, c’est à Hibernate de s’adapter au modèle.
Hibernate et encapsulation
Nous pouvons spécifier dans Hibernate la manière d’accéder aux champs, soit directement au champ lui-même par réflexion, soit au travers des accesseurs(get... et set...).
Pour cela, nous pouvons utiliser l’attribut "default-access" pour le comportement par défaut pour la classe, ou encore simplement "access" au niveau d’un membre.
Exemple avec Hibernate et un fichier de configuration(.hbm)
Code Java (127 lignes)
package be.gaudry.model.brolmeter; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.RandomAccess; import java.util.Set; import java.util.TreeSet; import be.gaudry.model.LightObject; /** * Adding or removing measures from the indexes may lead to dirty relative * measures (i.e. adding an index with an oldest date than the last index). This * method sort the original measures (indexes), and re-evaluate the relative * measures. The cost of maintaining relative measures is minimized by the fact * nothing is done on an insert or a remove, but the state is set to dirty, and * the relative measures must be re-evaluated the next time they will be asked * for. Asking for relative measures if dirty flag is not set has no cost. * * @date Feb 7, 2009 * @author Steph GAUDRY * */ public class Meter extends LightObject { /** * Indexes of the meter * @todo: use a Set and builds a treeset to avoid sorting manually */ private EMeterType meterType; public Meter() { this("?", "?"); } this(-1, EMeterType.DEFAULT, unit); this.display = name; } /** * @param id * @param _meterType * @param _unit */ super(id, ""); this.unit = _unit; this.meterType = _meterType; } /** * @return the meterType */ public EMeterType getMeterType() { return meterType; } /** * Returns an unmodifiable view of the measures (only ‹b›indexes of the * meter‹/b›). This collection is ‹b›sorted by insertion order‹/b› and not * by date; call the {@link java.util.Collections#sort(List) * Collections.sort(List)} method to sort by date. ‹br /› * This method allows modules to provide users with "read-only" access to * the measures. Query operations on the returned list "read through" to the * specified list, and attempts to modify the returned list, whether direct * or via its iterator, result in an ‹tt›UnsupportedOperationException‹/tt›. ‹br /› * ‹br /› * The returned list will be serializable if the specified list is * serializable. Similarly, the returned list will implement * {@link RandomAccess} if the specified list does. * * @return an unmodifiable view of the measures (never null). */ } /** * @param unit * the unit to set */ this.unit = unit; } /** * @return the unit */ return unit; } /** * Adds a {@link Measure} for this meter. * ‹br /›Sets also the {@link Measure#setMeter(Meter) measure meter} with this. * @param measure * @deprecated use {@link #addMeasure(Date, double)} instead of this (will be protected into next versions) */ public void addMeasure(Measure measure) { measures.add(measure); measure.setMeter(this); } /** * Builds and adds a {@link Measure} for this meter. */ measures.add(new Measure(this, date, value)); } public void clearMeasures() { measures.clear(); relativeMeasures.clear(); dirty = false; } }
Code XML (15 lignes)
‹?xml version="1.0" encoding="UTF-8"?› ‹!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.6//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"› ‹hibernate-mapping default-access="field" default-cascade="none" default-lazy="false" package="be.gaudry.model.brolmeter"› ‹union-subclass name="MeterHibernateWrapper" table="T_METERS" extends="be.gaudry.model.LightObject"› ‹list name="measures" cascade="all"› ‹key column="meter"/› ‹index column="MEASURES_LIST_INDEX"/› ‹one-to-many class="MeasureHibernateWrapper"/› ‹/list› ‹property name="unit" type="string" not-null="false" /› ‹property name="meterType" type="be.gaudry.model.brolmeter.EMeterType" not-null="false" /› ‹/union-subclass› ‹/hibernate-mapping›
Ce qui nous intéresse est la ligne suivante:
Code source (1 ligne)
default-access="field"
Remarque: et JPA?
Depuis JPA 2.0, nous avons l’annotation @Access qui nous permet aussi de spécifier comment accéder aux champs.
Un article de StephModifié 1 fois. (dernière modification le 05/10/2010 09:33:53 par Steph)
Source : indéterminée
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 13/09/2004, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/ast-rf-464.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.