hibernate – config properties 4 ways to set

Four “equivalent” methods:

1) xml — Traditional

2) properties file — Traditional

3) set programmatically in source code

4) pass in on java command line as -D

Advertisements

hibernate losing popularity on Wall St?

XR,

I’m always rather suspicious about newer app-dev technologies – android, distributed cache, weblogic, ruby, php, jaxb, xls, web service… I feel all of these (including superpowers Spring and WPF) will have to stand more tests of time.

With the decline of EJB, I don’t know what weblogic, websphere and JBoss are good for. Servlet containers? But tomcat is quite adequate?

Now I feel hibernate is not as popular as it used to be, at least in finance. My recent Wall St projects all decided not to use hibernate. We chose JDBC. It’s not that we don’t know hibernate. We know it well enough to avoid it.

It actually took these many years (5 +) for wall st developers to see the value/cost/benefit/limitations of Hibernate. Therefore, in the first few years of a new technology, it’s hard to predict if it will catch on, dominate and become entrenched. Over the past 30 years, only a small number of app development technologies achieved that status – SQL, MOM, threading, browser-based enterprise app, Excel, socket, javascript, and of course java/c++.

hibernate mock interview question

short and imprecise? ok
[m=answer found in official hibernate manual such as http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html ]
page numbers -> [[ java enterprise in a nutshell ]]

  1. how do you process a bunch of records when you select half the columns? What kind of data structure does hibernate return? P 650
  2. how do u update a “disconnected object”
  3. why proxy?
  4. Lazy loading? (seems to be important)
  5. write a finder statement used in your (pushmail) DAO class to illustrate the difference hql ^ sql
  6. big signature of Session.load(). AR-revealing!
  7. access modifier for fields in a domain object? P 630
  8. name the 2 types of hib config files
  9. generator class=”native” means? P 627
  10. what typical java types to use for persistent fields?
  11. what happens if there’s a clash in an id field/column when writing to DB? P627
  12. describe exactly when hibernate writes to DB. I think this is covered in my blog. P643
  13. what’s a bag?
  14. describe the session object
  15. describe the query object
  16. what’s the difference between the properties file vs the xml file
  17. what performance issues did you face using hibernate?
  18. how did you integrate hibernate with proc?
  19. 2 types of hibernate caching? (2nd-level caching)

non-iview questions:
* how does hibernate manage connection pool(s)? P641
* what if you try to load one object and all the linked objects are loaded and it takes huge memory and db query resources?

hibernate m:1 LotException

       
           
       
       
       
      
       
       
        <many-to-one name="lotBean" column="LotId"
          insert=”false” update=”false”
          not-null=”false” not-found=”ignore”/> —–> ignore if a LotException object has a null lotBean or an invalid lotBean which doesn't exist in the lotBean table.
 
Also note LotId column in this table is mapped twice.

Hibernate basic 2-table join — nested scan implemented in java

Zhu Rong,

To my dismay, hibernate (a popular,stable,fast java ORM framework) implements a 2 table join with individual SELECT’s on the inner table. As you explained, it’s more efficient to join inside the database?

Here’s my experiment. Hibernate supports a basic many-to-one mapping. Student object/table joining Course object/table. if i load 22 Student objects, hibernate uses lazy loading to delay querying the Course table until necessary. When it does query Course, it uses CourseId (join column, but not necessarily a PK or FK) to select just one Course. If 22 students are in 11 courses, hibernates runs 11 queries!

real world challenges using hibernate

1) performance. Hierbernate holds up well for mid-sized databases. For one GS system, we employ 3 techniques
 
* configuation on cachsing, lazy loading etc
* customize important queries using HQL
* hand-written SQL, when the generated SQL isn't optimized, but this tends to disable builtin performance features such as caching.
 
2) doens't work easily with stored procedures
 
3) if another db operation is already coded using jdbc, then one transaction can't easily span both

hibernate interceptors

In one GS system, we use interceptor to incercept inserts or deletes. As a standard, we don't put business logic in interceptors.
 
We put in lastUpdated and lastUpdatedBy and such things. It's somewhat similar to triggers but more efficient.
 

template objects in spring framework

*JMS — a template instance is a jms session
** anon inner class

*JDBC — a template instance is a database connection
** callback — an anon inner class

*hibernate — a template instance is a hibernate Session

*transaction — a template instance is a … transaction session?
** callback — an anon inner class

Spring Templates follow template-method design pattern. The callback objects provide the missing steps int the template method.

tibRV transport^connection^session

In JMS, session represents (roughly) a connection, or a socket. More precisely, one or more sessions are created from a single live connection. In JMS, sessions are single-threaded.

In RV, transport is the terminology. Means the same as a session, a socket, a connection to the “network”, to the daemon… Transport is a real object in memory.

In hibernate, session represents a connection to the DB. More precisely, a hibernate session HAS-A real connection object in memory

hibernate — essential techniques to SELECT query

* Technique: standard associations (m:1,m:n etc) — You need not write the query to SELECT the “associated” objects. If you load Students and want the associated Course loaded, Hibernate automatically constructs the Course query based on the Course.hbm.xml.

* Technique: HQL — You may need to write HQL to select students.

* Technique: views — Completely outside and unknown to hibernate, you can implement complex SELECT in a view and mention its name in a hbm.xml file, as if it’s a table.

* technique: native sql —

* technique: proc — existing logic in proc? This would be the ultimate. most powerful and customized.

hibernate n:m mapping

“name=” always means a property of THIS class
 
“table=” is necessary only for many-to-many [1] => link table required
 
[1] not the other 3 types. P632 [[ java enterprise ]]
 
Suppose the persistent object is Student.java. For a given property,
“class=” is optional if the property is declared a type (like Department) with a hbm (like Department.hbm.xml)
“class=” is required if the property is a java.util.Set => no type declared for the elements in the collection. Applicable to set-mapping.

hibernate cache-chain == money changers

Any one of these caches ALONE can eliminate a large percentage of database disk-reads, esp. for a read-mostly database.

Analogy: a chain of money changers A, B, C…. When a customer asks A about a forex rate, A can ask B (A->B->C), or use his “last-received” data ie A’s cache.

Caching at each stage of request-processing:
A = browser cache and client site proxy, the proxy server you configure in browser
B = server-side proxy — squid, proven by –> wikipedia <–
C = page-cache — smarty, JSP (?)
D = ORM “cached objects”, at several stages — hibernate http://www.devx.com/dbzone/Article/29685/1954?pf=true . I think this is also Jing Song’s suggestion.
E = raw query output cache — weblogic
F = DB-side caches, a few stages — P446 [[ mysql stored procedure ]]

hibernate self-quiz

[m=answer found in official hibernate manual such as http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html ]
page numbers -> [[ java enterprise in a nutshell ]]

* in a element, what does the “name” attribute mean to the system? [m]
* In a element, is the “column” attribute mandatory? [m]
* what hib perf features have you used?
* how do you avoid loading up an entire database accidentally?
* on which familiar hibernate object is the Transaction api defined? [m]
* what’s the relationship between hibernate.properties ^ hibernate.cfg.xml ? [m]
* Is SELECT optional? why? P 649
* what if no WHERE? P 649
* since client objects only uses getters and setters, can we use a hashmap to hold the persistence fields?

parts of hibernate set mapping

The set-mapping xml specifies a few things on the O and R sides. (Let’s not worry about one-many or many-many stuff for now. Focus.)
 
On the O side:
* class,
* field name
* java data type -> java.util.Set
 
 
On the R side:
* table — the “subordinate” table.
* key column
* other columns — of the table. Usually you want to retrieve all the columns to populate the objects, so no need.

hibernate notes, briefly

Hibernate was designed for multiple usage scenarios including an application server cluster.

Hibernate are usable in Java Swing applications, or J2EE applications using Enterprise Java Bean (EJB) session beans.

Hibernate XML mapping documents can also generate database table and constraint creation scripts. Remember the hbm2ddl.auto (spelling?) element in hibernate.cfg.xml?

cmp -> hibernate

UBS用的基本上都是CMP,现在已经开始用hibernate了。但不是所有项目都用。hibernate 只能说是刚开始使用。一些小的project中用到。以后的趋势肯定是会逐渐用到多数项目中。

hibernate确实不错,现在流行用spring和hibernate结合,spring framework做transaction management. hibernate 做data access object. 搭配使用,效果不错。

记得你说不久前 UBS 改用 Hibernate. 在这之前是不是用 CMP?

我逐渐感觉到, 对于大机构, 数据量庞大, 数据之间关系异常复杂, Normalize 之后可达到几百上千个表. 所以任何与数据处理相关的技术和流程都要求严格, 也不惜重本.

我觉得能达到这种要求的 Object/Relational Mapping 技术, 只有 CMP, Hibernate, JDO, Oracle Toplink 这几种. 恐怕还是 CMP 资格最老, 用的最多吧?

hibernate transaction — a comprehensive intro

— from http://www.hibernate.org/42.html, which also covers ejb, cmt, jdbc, interceptor …

1-sentence intro: Usually you also flush a Session at the end of a
unit of work to execute the SQL DML operations (UPDATE, INSERT,
DELETE) that synchronize the in-memory Session state with the
database.

Hibernate disables or expects the environment (in J2EE/JEE) to disable
auto-commit mode

— sess vs tx
A single Hibernate Session might have the same scope as a single database transaction. DB server probably maintains a session for each connection. I would speculate

1 hib-session – 1 server-session

— hibernate && jta
Hibernate works in any environment that uses JTA, in fact, we recommend to use JTA whenever possible as it is the standard Java transaction interface. You get JTA built-in with all J2EE/JEE application servers, and each Datasource you use in such a container is automatically handled by a JTA TransactionManager.

— no jta@@
If you don’t have JTA and don’t want to deploy it along with your application, you will usually have to fall back to JDBC transaction demarcation. Instead of calling the JDBC API you better use Hibernate’s Transaction interface

j4 (motivation for) persistence frameworks

Andrew Yap asked “Why Hibernate?” My answers:

j4: hide details of database interactions
* increasing ROI as “domain models” complexity increases
* diminishing benefit for simple DB interactions.

j4: java programmers are comfortable with java objects than database entities. Without a persistence framework, “relationship mapping” is non-trvial.
* cost: hard to tune sql

J4: portability across databases

hibernate onjava notes#many-to-one

Q: biz logic implemented where?
A: beans representing db entities.

Q: explain the many-to-one:
<class name="test.hibernate.OrderItem"
table=”order_items”>
<many-to-one name="order"
class=”test.hibernate.Order”
column=”order_id” />
<many-to-one name="product"

A: first many-to-one means many of this (OrderItem) linked to one
order. ANY link like this requires a link-column/join-column, which is
“order_items.order_id”

spring/hibernate transaction AOP interceptor onjava.com

http://www.onjava.com/pub/a/onjava/2005/05/18/swingxactions.html

q 3: which obj intercepts the sql and manages tx propagation, session and thread to enable tx?
A3: “they are intercepted and interpreted by a particular implementation of the org.springframework.transaction.PlatformTransactionManager interface”.

AOP usually requires an interface, as needed by JDK proxy

For example, a hibernate tx mgr is such an interceptor — org.springframework.orm.hibernate.HibernateTransactionManager

Q82: where are “interceptor” and “proxy” mentioned in src?
A82: