|
| 1 | +----------------- Hibernate ----------------- |
| 2 | + |
| 3 | +Benefits and risks of using Hibernate |
| 4 | + //todo |
| 5 | + |
| 6 | +What is a SessionFactory? Is it a thread-safe object? |
| 7 | + The main contract here is the creation of Session instances. Usually an |
| 8 | + application has a single SessionFactory instance and threads servicing client |
| 9 | + requests obtain Session instances from this factory. |
| 10 | + The internal state of a SessionFactory is immutable. Once it is created this |
| 11 | + internal state is set. This internal state includes all of the metadata about |
| 12 | + Object/Relational Mapping. |
| 13 | + Implementors must be threadsafe. |
| 14 | + |
| 15 | +What is the difference between merge and update |
| 16 | + saveOrUpdate: |
| 17 | + Calls either save or update depending on some checks. E.g. if no identifier |
| 18 | + exists, save is called. Otherwise update is called. |
| 19 | + save: |
| 20 | + Persists an entity. Will assign an identifier if one doesn't exist. If one |
| 21 | + does, it's essentially doing an update. Returns the generated ID of the |
| 22 | + entity. |
| 23 | + update: |
| 24 | + Update the persistent instance with the identifier of the given detached |
| 25 | + instance. If there is a persistent instance with the same identifier, |
| 26 | + an exception is thrown. This operation cascades to associated instances |
| 27 | + if the association is mapped with cascade="save-update". |
| 28 | + merge: |
| 29 | + Copy the state of the given object onto the persistent object with the same |
| 30 | + identifier. If there is no persistent instance currently associated with |
| 31 | + the session, it will be loaded. Return the persistent instance. If the |
| 32 | + given instance is unsaved, save a copy of and return it as a newly |
| 33 | + persistent instance. The given instance does not become associated with |
| 34 | + the session. This operation cascades to associated instances if the |
| 35 | + association is mapped with cascade="merge". |
| 36 | + In these cases you need to use merge for updates and persist for saving. |
| 37 | + persist: |
| 38 | + As mentioned above, this is used on transient objects. It does not return |
| 39 | + the generated ID. |
| 40 | + |
| 41 | +What is Hibernate Query Language (HQL)? |
| 42 | + Hibernate uses a powerful query language (HQL) that is similar in appearance |
| 43 | + to SQL. Compared with SQL, however, HQL is fully object-oriented and |
| 44 | + understands notions like inheritance, polymorphism and association. |
| 45 | + |
| 46 | +Lazy loading |
| 47 | + Proxy pattern. |
| 48 | + Say you have a parent and that parent has a collection of children. |
| 49 | + Hibernate now can "lazy-load" the children, which means that it does not |
| 50 | + actually load all the children when loading the parent. Instead, it loads them |
| 51 | + when requested to do so. You can either request this explicitly or, and this |
| 52 | + is far more common, hibernate will load them automatically when you try to |
| 53 | + access a child. |
| 54 | + Lazy-loading can help improve the performance significantly since often you |
| 55 | + won't need the children and so they will not be loaded. |
| 56 | + Also beware of the n+1-problem. Hibernate will not actually load all |
| 57 | + children when you access the collection. Instead, it will load each child |
| 58 | + individually. When iterating over the collection, this causes a query for |
| 59 | + every child. In order to avoid this, you can trick hibernate into loading |
| 60 | + all children simultaneously, e.g. by calling parent.getChildren().size(). |
| 61 | + |
| 62 | +What do you mean by Named � SQL query and how to invoke it? |
| 63 | + Named SQL queries may be defined in the mapping document and called in exactly |
| 64 | + the same way as a named HQL query. In this case, we do not need to call |
| 65 | + addEntity(). |
| 66 | + <sql-query name="persons"> |
| 67 | + <return alias="person" class="eg.Person"/> |
| 68 | + SELECT person.NAME AS {person.name}, |
| 69 | + person.AGE AS {person.age}, |
| 70 | + person.SEX AS {person.sex} |
| 71 | + FROM PERSON person |
| 72 | + WHERE person.NAME LIKE :namePattern |
| 73 | + </sql-query> |
| 74 | + .. |
| 75 | + List people = session.getNamedQuery("persons") |
| 76 | + .setString("namePattern", namePattern) |
| 77 | + .setMaxResults(50) |
| 78 | + .list(); |
| 79 | + |
| 80 | +What is the difference between sorted and ordered collection in hibernate? |
| 81 | + sorted collection: |
| 82 | + A sorted collection is sorting a collection by utilizing the sorting features |
| 83 | + provided by the Java collections framework. The sorting occurs in the memory |
| 84 | + of JVM which running Hibernate, after the data being read from database using |
| 85 | + java comparator. If your collection is not large, it will be more efficient |
| 86 | + way to sort it. |
| 87 | + order collection : |
| 88 | + Order collection is sorting a collection by specifying the order-by clause |
| 89 | + for sorting this collection when retrieval. If your collection is very large, |
| 90 | + it will be more efficient way to sort it. |
| 91 | + |
| 92 | +Difference between get() and load()? |
| 93 | + If load() can�t find the object in the cache or database, an exception is |
| 94 | + thrown. The load() method never returns null. The get() method returns |
| 95 | + null if the object can�t be found. |
| 96 | + The load() method may return a proxy instead of a real persistent instance. |
| 97 | + A proxy is a placeholder that triggers the loading of the real object when it�s |
| 98 | + accessed for the first time; we discuss proxies later in this section. On the |
| 99 | + other hand, get() never returns a proxy. |
| 100 | + |
| 101 | +What are the types of Hibernate instance states ? |
| 102 | + transient |
| 103 | + The instance is not associated with any persistence context. It has no |
| 104 | + persistent identity or primary key value. |
| 105 | + persistent |
| 106 | + The instance is currently associated with a persistence context. It has a |
| 107 | + persistent identity (primary key value) and can have a corresponding row in |
| 108 | + the database. For a particular persistence context, Hibernate guarantees |
| 109 | + that persistent identity is equivalent to Java identity in relation to the |
| 110 | + in-memory location of the object. |
| 111 | + detached |
| 112 | + The instance was once associated with a persistence context, but that |
| 113 | + context was closed, or the instance was serialized to another process. |
| 114 | + It has a persistent identity and can have a corresponding row in the |
| 115 | + database. For detached instances, Hibernate does not guarantee the |
| 116 | + relationship between persistent identity and Java identity. |
| 117 | + |
| 118 | +Hierarchy-to-tables mapping strategies //todo check with reference doc (table per class hierarchy | table per subclass | table per concrete class | implicit polymorphism ) |
| 119 | + Table per concrete class with implicit polymorphism�Use no explicit |
| 120 | + inheritance mapping, and default runtime polymorphic behavior. |
| 121 | + You can use exactly one table for each (nonabstract) class. |
| 122 | + All properties of a class, including inherited properties, can be mapped to |
| 123 | + columns of this table, |
| 124 | + Table per concrete class�Discard polymorphism and inheritance relationships |
| 125 | + completely from the SQL schema. Table per concrete class with unions. |
| 126 | + If your superclass is concrete, then an additional table is needed to hold |
| 127 | + instances of that class. |
| 128 | + Table per class hierarchy�Enable polymorphism by denormalizing the SQL |
| 129 | + schema, and utilize a type discriminator column that holds type information. |
| 130 | + An entire class hierarchy can be mapped to a single table. |
| 131 | + This table includes columns for all properties of all classes in the hierarchy. |
| 132 | + The concrete subclass represented by a particular row is identified by the |
| 133 | + value of a type discriminator column. |
| 134 | + There is one major problem: Columns for properties declared by subclasses |
| 135 | + must be declared to be nullable. |
| 136 | + Another important issue is normalization. We�ve created functional |
| 137 | + dependencies between nonkey columns, violating the third normal form. |
| 138 | + Table per subclass�Represent is a (inheritance) relationships as has a |
| 139 | + (foreign key) relationships. |
| 140 | + The fourth option is to represent inheritance relationships as relational |
| 141 | + foreign key associations. Every class/subclass that declares persistent |
| 142 | + properties�including abstract classes and even interfaces�has its own table. |
| 143 | + Unlike the table per concrete class strategy we mapped first, the table here |
| 144 | + contains columns only for each noninherited property (each property declared |
| 145 | + by the subclass itself) along with a primary key that is also a foreign key of |
| 146 | + the superclass table. |
| 147 | + The primary advantage of this strategy is that the SQL schema is normalized. |
| 148 | + Schema evolution and integrity constraint definition are straightforward. |
| 149 | + A polymorphic association to a particular subclass may be represented as a |
| 150 | + foreign key referencing the table of that particular subclass. |
| 151 | + As you can see, this mapping strategy is more difficult to implement by hand� |
| 152 | + even ad-hoc reporting is more complex. This is an important consideration if |
| 153 | + you plan to mix Hibernate code with handwritten SQL. |
| 154 | + Furthermore, even though this mapping strategy is deceptively simple, our |
| 155 | + experience is that performance can be unacceptable for complex class |
| 156 | + hierarchies. Queries always require either a join across many tables or many |
| 157 | + sequential reads. |
| 158 | + |
| 159 | +What are the Collection types in Hibernate ? |
| 160 | + A collection is defined as a one-to-many reference. The simplest collection |
| 161 | + type in Hibernate is <bag>.This collection is a list of unordered objects and |
| 162 | + can contain duplicates. This is similar to java.util.List. The content of a |
| 163 | + collection is required for a SQL query. This won�t work until the code is |
| 164 | + actually accessed. This process has a benefit that allows the developer to |
| 165 | + separate the database access logic from that of the object traversal logic. |
| 166 | + This is called lazy collection. |
| 167 | + Filtering logic can be applied with lazy collection. This can alter the SQL |
| 168 | + which invokes when the actual collection is invoked. |
| 169 | + ArrayType, |
| 170 | + Constructor: ArrayType(String role, String propertyRef, Class elementClass, boolean isEmbeddedInXML) |
| 171 | + BagType, |
| 172 | + Constructor: BagType(String role, String propertyRef, boolean isEmbeddedInXML) |
| 173 | + CustomCollectionType, A custom type for mapping user-written classes that implement PersistentCollection |
| 174 | + Constructor: CustomCollectionType(Class userTypeClass, String role, String foreignKeyPropertyName, boolean isEmbeddedInXML) |
| 175 | + IdentifierBagType, |
| 176 | + Constructor: IdentifierBagType(String role, String propertyRef, boolean isEmbeddedInXML) |
| 177 | + ListType, |
| 178 | + Constructor: ListType(String role, String propertyRef, boolean isEmbeddedInXML) |
| 179 | + MapType, |
| 180 | + Constructor: MapType(String role, String propertyRef, boolean isEmbeddedInXML) |
| 181 | + SetType |
| 182 | + Constructor: SetType(String role, String propertyRef, boolean isEmbeddedInXML) |
0 commit comments