dans Astuce, Hibernate

Comment mapper une entité hibernate avec plusieurs tables

Il arrive parfois que pour un besoin particulier on soit contraint de mapper une entité hibernate à plusieurs tables.

Eh bien il existe depuis hibernate 4.3 une nouvel attribut uniquement disponible dans le mapping par fichier. Cet attribut est entity-name.

Nous verrons dans la suite de l’article comment utiliser cette attribut afin de résoudre la problématique ci-dessus.

Dans le projet sur lequel j’interviens actuellement nous avons plusieurs vues identiques mais qui remontent des données différentes et potentiellement issues de tables.

En résumé les colonnes de chacune des vues sont identiques mais les informations remontées sont différentes.

De façon générale avec Hibernate on a souvent un fichier de mapping par entity et chaque fichier de mapping mappe une table à une entité.

Comme vous l’aurez compris dans mon cas cité plus haut, il serait dommage de créer n entités présentant exactement les mêmes champs, puis de les mapper chacune à une vue.

Une bonne pratique de développement consiste à respecter DRY (Don’t Repeat Yourself), ainsi il est préférable de mutualiser au max.

Avant hibernate 4.3 il aurait fallut faire comme cela:

<hibernate-mapping>
<class name="package.class1" table="view1OrTable1">
<id>...</id>
<property>...</property>
<property>...</property>
<property>...</property>
</class>

<class name="package.class2" table="view1OrTable2">
<id>...</id>
<property>...</property>
<property>...</property>
<property>...</property>
</class>
</hibernate-mapping>

Ici nous voyons clairement que nous avons 2 classes  class1 et class2 qui portent les mêmes attributs (duplication de code). Bien entendu nous pourrions utiliser de l’héritage ou de la composition pour factoriser le code, mais la solution proposée par Hibernate reste quand même beaucoup plus propre.

Depuis hierbnate 4.3

<hibernate-mapping>
<class name="package.class" entity-name="entity1" table="view1OrTable1">
<id>...</id>
<property>...</property>
<property>...</property>
<property>...</property>
</class>

<class name="package.class" entity-name="entity2" table="view1OrTable2">
<id>...</id>
<property>...</property>
<property>...</property>
<property>...</property>
</class>
</hibernate-mapping>

L’introduction de l’attribut entity-name nous permet de résoudre ce problème de facon élégante.

Ici la même classe java est utilisée, seul l’attribut entity-name va service de discriminant.

Et pour le requetage avec l’api Criteria:

// Pour requeter entity1
 Criteria crit = sessionFactory.getCurrentSession().createCriteria("entity1");
// Pour requeter entity2
 Criteria crit = sessionFactory.getCurrentSession().createCriteria("entity2");

Si vous souhaitez utiliser le HQL pareil le nom de classe est à remplacer par celui définit dans entity-name.

Voila, j’espère que cet article aura aidé certains.

Ressources:
http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch05.html#mapping-entityname

Ecrire un Commentaire

Commenter

Webmentions

  • site

    site

    site

  • allopurinol indication

    allopurinol indication

    allopurinol indication

  • amitriptyline dosage for sleep

    amitriptyline dosage for sleep

    amitriptyline dosage for sleep

  • baby aspirin during pregnancy

    baby aspirin during pregnancy

    baby aspirin during pregnancy

  • aripiprazole 10 mg high

    aripiprazole 10 mg high

    aripiprazole 10 mg high

  • what will 200 mg of citalopram do

    what will 200 mg of citalopram do

    what will 200 mg of citalopram do

  • ingredients in contrave

    ingredients in contrave

    ingredients in contrave

  • how long does augmentin stay in your system

    how long does augmentin stay in your system

    how long does augmentin stay in your system

  • flexeril 10mg high

    flexeril 10mg high

    flexeril 10mg high

  • depakote cost

    depakote cost

    depakote cost

  • diltiazem brand name

    diltiazem brand name

    diltiazem brand name

  • why does flomax have to be taken after a meal

    why does flomax have to be taken after a meal

    why does flomax have to be taken after a meal

  • ddavp rash

    ddavp rash

    ddavp rash

  • effexor generic name

    effexor generic name

    effexor generic name

  • diclofenac sodium 75 mg

    diclofenac sodium 75 mg

    diclofenac sodium 75 mg

  • ezetimibe ficha tecnica

    ezetimibe ficha tecnica

    ezetimibe ficha tecnica

  • cozaar classification

    cozaar classification

    cozaar classification

  • is amoxicillin 875 mg a strong dose

    is amoxicillin 875 mg a strong dose

    is amoxicillin 875 mg a strong dose

  • grandpashabet

    Comment mapper une entité hibernate avec plusieurs tables – Javacoding

    https://www.slgentile.it/lavoro_vector2/

  • porno izle

    Comment mapper une entité hibernate avec plusieurs tables – Javacoding

    https://unima.ch/component/k2/item/4-how-to-make-impressive-designs?start=12430

  • grandpashabet

    Comment mapper une entité hibernate avec plusieurs tables – Javacoding

    https://konpart.de/component/k2/item/2-ae-cam365-camshaft/2-ae-cam365-camshaft.html?start=48140

  • vxi.su

    vxi.su

    vxi.su

  • bactrim use

    bactrim use

    bactrim use

  • how to take bactrim

    how to take bactrim

    how to take bactrim

  • allergic to penicillin can i take ciprofloxacin

    allergic to penicillin can i take ciprofloxacin

    allergic to penicillin can i take ciprofloxacin

  • cephalexin dosage

    cephalexin dosage

    cephalexin dosage

  • Spiral Dynamics

    Spiral Dynamics

    Spiral Dynamics

  • porn

    Comment mapper une entité hibernate avec plusieurs tables – Javacoding

    https://tecnologiainteligente.com.br/a-thinkwise-agora-adere-a-sustentabilidade-por-design/

  • child porn

    Comment mapper une entité hibernate avec plusieurs tables – Javacoding

    https://mis15web.com/post-number-three-lorem-ipsum-dolor-sit-amet/

  • porn

    Comment mapper une entité hibernate avec plusieurs tables – Javacoding

    https://moody-monkey.com/20231110diary/

  • porno izle

    Comment mapper une entité hibernate avec plusieurs tables – Javacoding

    https://actucongo.net/presse-kasai-7-un-media-en-ligne-dans-nos-kiosques-a-journaux/

  • spiraldynamics

    spiraldynamics

    spiraldynamics

  • expired cephalexin 500mg

    expired cephalexin 500mg

    expired cephalexin 500mg

  • escitalopram m generic

    escitalopram m generic

    escitalopram m generic

  • viagra order online australia

    viagra order online australia

    viagra order online australia

  • batman apollo ru

    batman apollo ru

    batman apollo ru

  • cymbalta and prozac

    cymbalta and prozac

    cymbalta and prozac

  • gabapentin epocrates

    gabapentin epocrates

    gabapentin epocrates

  • duloxetine green and white capsule

    duloxetine green and white capsule

    duloxetine green and white capsule

  • fluoxetine 40 mg capsule

    fluoxetine 40 mg capsule

    fluoxetine 40 mg capsule

  • Tucker Carlson - Vladimir Putin

    Tucker Carlson – Vladimir Putin

    Tucker Carlson – Vladimir Putin

  • Tucker Carlson - Vladimir Putin - 2024-02-09 Putin interview summary, full interview.

    Tucker Carlson – Vladimir Putin – 2024-02-09 Putin interview summary, full interview.

    Tucker Carlson – Vladimir Putin – 2024-02-09 Putin interview summary, full interview.

  • keflex same as cephalexin

    keflex same as cephalexin

    keflex same as cephalexin

  • a

    a

    a

  • metronidazole staphylococcus

    metronidazole staphylococcus

    metronidazole staphylococcus

  • xanax vs zoloft

    xanax vs zoloft

    xanax vs zoloft

  • list

    list

    list

  • link

    link

    link

  • psy

    psy

    psy

  • 911

    911

    911

  • rybelsus and metformin

    rybelsus and metformin

    rybelsus and metformin

  • vision changes with rybelsus

    vision changes with rybelsus

    vision changes with rybelsus

  • semaglutide knee osteoarthritis

    semaglutide knee osteoarthritis

    semaglutide knee osteoarthritis

  • metformin ratings

    metformin ratings

    metformin ratings

  • lasix stipsi

    lasix stipsi

    lasix stipsi

  • explain lisinopril

    explain lisinopril

    explain lisinopril

  • nolvadex-d bodybuilding

    nolvadex-d bodybuilding

    nolvadex-d bodybuilding

  • valacyclovir effectiveness

    valacyclovir effectiveness

    valacyclovir effectiveness

  • pregabalin gad dose

    pregabalin gad dose

    pregabalin gad dose

  • bactrim dente

    bactrim dente

    bactrim dente

  • gabapentin magyar

    gabapentin magyar

    gabapentin magyar

  • metronidazole extravasation

    metronidazole extravasation

    metronidazole extravasation

  • buy cialis online free shipping

    buy cialis online free shipping

    buy cialis online free shipping

  • amino tadalafil

    amino tadalafil

    amino tadalafil

  • best price for tadalafil

    best price for tadalafil

    best price for tadalafil

  • how long for cialis to work

    how long for cialis to work

    how long for cialis to work

  • generic sildenafil online

    generic sildenafil online

    generic sildenafil online

  • real viagra online canada

    real viagra online canada

    real viagra online canada

  • where can i order viagra online in canada

    where can i order viagra online in canada

    where can i order viagra online in canada

  • where to get generic viagra online

    where to get generic viagra online

    where to get generic viagra online

  • cheap generic viagra india

    cheap generic viagra india

    cheap generic viagra india

  • can you order viagra from canada

    can you order viagra from canada

    can you order viagra from canada

  • us pharmacy ambien no prescription

    us pharmacy ambien no prescription

    us pharmacy ambien no prescription

  • cheap cialis generic online

    cheap cialis generic online

    cheap cialis generic online

  • viagra online pharmacies

    viagra online pharmacies

    viagra online pharmacies

  • how much does cialis cost without insurance

    how much does cialis cost without insurance

    how much does cialis cost without insurance

  • cheap viagra mexico

    cheap viagra mexico

    cheap viagra mexico

  • side effect of cialis

    side effect of cialis

    side effect of cialis

  • phentermine online pharmacy

    phentermine online pharmacy

    phentermine online pharmacy

  • how much does vicodin cost from a pharmacy

    how much does vicodin cost from a pharmacy

    how much does vicodin cost from a pharmacy

  • cialis and lisinopril

    cialis and lisinopril

    cialis and lisinopril

  • cialis super active reviews

    cialis super active reviews

    cialis super active reviews

  • tadalafil professional 20 mg

    tadalafil professional 20 mg

    tadalafil professional 20 mg

  • 2competency

    2wildfire