2016-03-12 3 views
1

Tenir compte du code client Hibernateveille prolongée SessionFactory ob

Configuration cfg = new Configuration(); 
cfg.configure(); 

à ce point le constructeur par défaut de la classe de persistance ne sera pas appeler. Cela signifie aucun cas créerons pour la persistance classe

mais après avoir créé l'objet SessionFactory i.e.

Configuration cfg = new Configuration(); 
cfg.configure(); 
SessionFactory sf = cfg.buildSessionFactory(); 

défaut construcort appellera 3 fois exactement la question est donc pourquoi se crée exactement trois objets de la classe de persistance après la création objet SessionFactory.

Répondre

0

C'est un avis intéressant.

Hibernate obtient toutes les informations sur le mappage en chargeant et en vérifiant les objets Class<?> correspondants. Mais, parfois, il a besoin d'une information impossible à obtenir à partir d'un objet Class<?>, pour un exemple, un résultat de l'appel d'une méthode d'un persistant. Dans de telles situations, Hibernate crée un objet temporaire d'une méthode persistante et l'appelle.

Vous pouvez définir un point d'arrêt sur le constructeur par défaut et vérifier toutes les situations dans lesquelles Hibernate instancie les objets persistants.

J'essaie de tester ce comportement avec Hibernate 4 et 5. Mise en veille prolongée Dans les deux cas, un constructeur par défaut est appelé une seule fois dans cette méthode, dans la ligne

final Serializable defaultValue = (Serializable) identifierGetter.get(instantiate(constructor));

/** 
    * Return an IdentifierValue for the specified unsaved-value. If none is specified, 
    * guess the unsaved value by instantiating a test instance of the class and 
    * reading it's id property, or if that is not possible, using the java default 
    * value for the type 
    * 
    * @param unsavedValue The mapping defined unsaved value 
    * @param identifierGetter The getter for the entity identifier attribute 
    * @param identifierType The mapping type for the identifier 
    * @param constructor The constructor for the entity 
    * 
    * @return The appropriate IdentifierValue 
    */ 
    public static IdentifierValue getUnsavedIdentifierValue(
      String unsavedValue, 
      Getter identifierGetter, 
      Type identifierType, 
      Constructor constructor) { 
     if (unsavedValue == null) { 
      if (identifierGetter != null && constructor != null) { 
       // use the id value of a newly instantiated instance as the unsaved-value 
       final Serializable defaultValue = (Serializable) identifierGetter.get(instantiate(constructor)); 
       return new IdentifierValue(defaultValue); 
      } 
      else if (identifierGetter != null && (identifierType instanceof PrimitiveType)) { 
       final Serializable defaultValue = ((PrimitiveType) identifierType).getDefaultValue(); 
       return new IdentifierValue(defaultValue); 
      } 
      else { 
       return IdentifierValue.NULL; 
      } 
     } 

     ... 
    } 
+0

@prathamesh Ok. Merci d'accepter. –

+0

Oui, ce comportement concerne uniquement Hibernate 3.0. même avec des annotations, le constructeur par défaut n'est appelé qu'une seule fois. – prathamesh