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 :

Relation OneToOne strict


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2013
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2013
    Messages : 6
    Par défaut Relation OneToOne strict
    Bonjour,

    j'ai une relation OneToOne entre 2 entités via une table d'association :

    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
    @Entity
    @Table(name="table1")	
    public class Table1 {
     
    	@Id @GeneratedValue
    	private int id;
     
    	@Column(name="colTable1", nullable=false) 
    	private String colTable1;
     
    	@OneToOne(cascade=CascadeType.ALL, optional=false) 
            @JoinTable(name="associationtable",	
            joinColumns = @JoinColumn(name="idTable1"), 
            inverseJoinColumns = @JoinColumn(name="idTable2")
    	private Table2 table2;
     
    ...
     
    }

    et

    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
    @Entity
    @Table(name="table2")	
    public class Table2 {
     
    	@Id @GeneratedValue
    	private int id;
     
    	@Column(name="colTable2", nullable=false)
    	private String colTable2;
     
    	@OneToOne(cascade=CascadeType.ALL, optional=false) 
            @JoinTable(name="associationtable",	
            joinColumns = @JoinColumn(name="idTable2"), 
            inverseJoinColumns = @JoinColumn(name="idTable1")
    	private Table1 table1;
     
    ...
     
    }

    Je voudrais mettre au niveau hibernate une contrainte qui fasse que la relation OneToOne soit strict : c'est à dire ne plus avoir de possibilité d'enregistrer du zero-or-one <> zero-or-one quand on svgd table1 ou table2.

    Comment faire ?
    Merci.

  2. #2
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2009
    Messages
    153
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2009
    Messages : 153
    Par défaut
    Je penses que tu dois ajouter un attribut mappedBy(...) au niveau d'une de tes annotations @OneToOne

  3. #3
    Expert éminent
    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 : 46
    Localisation : Belgique

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

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Met simplement un notnull sur tes deux champs.

    Aussi, normalement, une des relations devrait être marquée l'inverse de l'autre, sinon hibernate ignore qui est responsable du stockage de l'information et ça deviens le bordel rapidement

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2013
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2013
    Messages : 6
    Par défaut
    Bonjour,

    effectivement j'avais pas vu cette histoire de mappedBy ... Merci.
    Du coup maintenant j'ai ça :

    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
    @Entity
    @Table(name="table1")	
    public class Table1 {
     
    	@Id @GeneratedValue
    	private int id;
     
    	@Column(name="colTable1", nullable=false) 
    	private String colTable1;
     
    	@OneToOne(cascade=CascadeType.ALL) 
            @JoinTable(name="associationtable",	
            joinColumns = @JoinColumn(name="idTable1"), 
            inverseJoinColumns = @JoinColumn(name="idTable2") 
            ) 
    	@NotNull
    	private Table2 table2;
     
    ....
    }

    et
    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
     
    @Entity
    @Table(name="table2")	
    public class Table2 {
     
    	@Id @GeneratedValue
    	private int id;
     
    	@Column(name="colTable2", nullable=false)
    	private String colTable2;
     
    	@OneToOne(cascade=CascadeType.ALL, mappedBy = "table2") 
            @NotNull
    	private Table1 table1;
     
    ...
     
    }

    et lors d'une simple sauvegarde :

    Table1 table1 = new Table1();
    table1 .setColTable1("11111");

    Table2 table2 = new Table2();
    table2.setColTable2("22222");

    table1.setTable2(table2);
    table2.setTable1(table1);

    dao.save(table1);
    j'ai l'erreur suivante :

    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
    nov. 06, 2013 7:56:29 PM org.hibernate.action.internal.UnresolvedEntityInsertActions logCannotResolveNonNullableTransientDependencies
    WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
    	Unsaved transient entity: ([com.example.hibernateSpring03.model.Table1#0])
    	Dependent entities: ([[com.example.hibernateSpring03.model.Table2#<null>]])
    	Non-nullable association(s): ([com.example.hibernateSpring03.model.Table2.table1])
    nov. 06, 2013 7:56:29 PM org.hibernate.action.internal.UnresolvedEntityInsertActions logCannotResolveNonNullableTransientDependencies
    WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
    	Unsaved transient entity: ([com.example.hibernateSpring03.model.Table2#0])
    	Dependent entities: ([[com.example.hibernateSpring03.model.Table1#<null>]])
    	Non-nullable association(s): ([com.example.hibernateSpring03.model.Table1.table2])
    org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: com.example.hibernateSpring03.model.Table1.table2 -> com.example.hibernateSpring03.model.Table2
    	at org.hibernate.action.internal.UnresolvedEntityInsertActions.checkNoUnresolvedActionsAfterOperation(UnresolvedEntityInsertActions.java:135)
    	at org.hibernate.engine.spi.ActionQueue.checkNoUnresolvedActionsAfterOperation(ActionQueue.java:251)
    	at org.hibernate.internal.SessionImpl.checkNoUnresolvedActionsAfterOperation(SessionImpl.java:711)
    	at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:766)
    ....
    ....
    Alors que si je met pas le @NotNull ça enregistre bien.

  5. #5
    Expert éminent
    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 : 46
    Localisation : Belgique

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

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    essaie

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    Table1 table1 = new Table1();
    table1 .setColTable1("11111");	
     
    Table2 table2 = new Table2();
    table2.setColTable2("22222");
     
     
    table1.setTable2(table2);
    table2.setTable1(table1);
     
     
    dao.save(table2);
    dao.save(table1);
    comme dit le message, Table1.table2 doit être sauvé en premier

  6. #6
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Octobre 2013
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2013
    Messages : 6
    Par défaut
    En essayant ça j'ai la même erreur mais pour table2 :

    org.hibernate.action.internal.UnresolvedEntityInsertActions logCannotResolveNonNullableTransientDependencies
    WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
    Unsaved transient entity: ([com.example.hibernateSpring03.model.Table2#0])
    Dependent entities: ([[com.example.hibernateSpring03.model.Table1#<null>]])
    Non-nullable association(s): ([com.example.hibernateSpring03.model.Table1.table2])
    nov. 06, 2013 9:46:46 PM org.hibernate.action.internal.UnresolvedEntityInsertActions logCannotResolveNonNullableTransientDependencies
    WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
    Unsaved transient entity: ([com.example.hibernateSpring03.model.Table1#0])
    Dependent entities: ([[com.example.hibernateSpring03.model.Table2#<null>]])
    Non-nullable association(s): ([com.example.hibernateSpring03.model.Table2.table1])
    nov. 06, 2013 9:47:00 PM org.apache.catalina.core.StandardWrapperValve invoke
    Grave: Servlet.service() for servlet [appServlet] in context with path [/hibernateSpring03] threw exception [Request processing failed; nested exception is org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: com.example.hibernateSpring03.model.Table1.table2 -> com.example.hibernateSpring03.model.Table2] with root cause
    org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: com.example.hibernateSpring03.model.Table1.table2 -> com.example.hibernateSpring03.model.Table2
    at org.hibernate.action.internal.UnresolvedEntityInsertActions.checkNoUnresolvedActionsAfterOperation(UnresolvedEntityInsertActions.java:135)
    at org.hibernate.engine.spi.ActionQueue.checkNoUnresolvedActionsAfterOperation(ActionQueue.java:251)
    at org.hibernate.internal.SessionImpl.checkNoUnresolvedActionsAfterOperation(SessionImpl.java:711)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:766)

Discussions similaires

  1. [EJB3.1] Plusieurs relation OneToOne vers la même entité.
    Par piemur2000 dans le forum Java EE
    Réponses: 1
    Dernier message: 11/01/2011, 09h05
  2. probleme avec une relation OneToOne
    Par aniss77 dans le forum JPA
    Réponses: 1
    Dernier message: 01/04/2010, 22h07
  3. Relation OneToOne en HQL
    Par aigleborgne dans le forum JPA
    Réponses: 1
    Dernier message: 04/09/2009, 14h18
  4. Réponses: 1
    Dernier message: 16/12/2008, 14h38
  5. [Nhibernate] Relation OneToOne
    Par Vercity dans le forum NHibernate
    Réponses: 1
    Dernier message: 12/03/2007, 11h36

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