The configuration files hibernate.cfg.xml (or hibernate.properties) and mapping files *.hbm.xml are used by the Configuration class to create (i.e. configure and bootstrap hibernate) the SessionFactory, which in turn creates the Session instances. Session instances are the primary interface for the persistence service.
" hibernate.cfg.xml (alternatively can use hibernate.properties): These two files are used to configure the hibernate sevice (connection driver class, connection URL, connection username, connection password, dialect etc). If both files are present in the classpath then hibernate.cfg.xml file overrides the settings found in the hibernate.properties file.
" Mapping files (*.hbm.xml): These files are used to map persistent objects to a relational database. It is the best practice to store each object in an individual mapping file (i.e mapping file per class) because storing large number of persistent classes into one mapping file can be difficult to manage and maintain. The naming convention is to use the same name as the persistent (POJO) class name. For example Account.class will have a mapping file named Account.hbm.xml. Alternatively hibernate annotations can be used as part of your persistent class code instead of the *.hbm.xml files.
Hibernate can make certain optimizations all the time:
Caching objects. The session is a transaction-level cache of persistent objects. You may also enable a JVM-level/cluster cache to memory and/or local disk. Executing SQL statements later, when needed. The session never issues an INSERT or UPDATE until it is actually needed. So if an exception occurs and you need to abort the transaction, some statements will never actually be issued. Furthermore, this keeps lock times in the database as short as possible (from the late UPDATE to the transaction end). Never updating unmodified objects. It is very common in hand-coded JDBC to see the persistent state of an object updated, just in case it changed.....for example, the user pressed the save button but may not have edited any fields. Hibernate always knows if an object's state actually changed, as long as you are inside the same (possibly very long) unit of work. Efficient Collection Handling. Likewise, Hibernate only ever inserts/updates/deletes collection rows that actually changed. Rolling two updates into one. As a corollary to (1) and (3), Hibernate can roll two seemingly unrelated updates of the same object into one UPDATE statement. Updating only the modified columns. Hibernate knows exactly which columns need updating and, if you choose, will update only those columns. Outer join fetching. Hibernate implements a very efficient outer-join fetching algorithm! In addition, you can use subselect and batch pre-fetch optimizations. Lazy collection initialization. Lazy object initialization. Hibernate can use runtime-generated proxies (CGLIB) or interception injected through bytecode instrumentation at build-time. A few more (optional) features of Hibernate that your handcoded JDBC may or may not currently benefit from
second-level caching of arbitrary query results, from HQL, Criteria, and even native SQL queries efficient PreparedStatement caching (Hibernate always uses PreparedStatement for calls to the database) JDBC 2 style batch updates Pluggable connection pooling Hopefully you will agree that Hibernate approaches the parsimony of the best hand-coded JDBC object persistence. As a subscript I would add that I have rarely seen JDBC code that approaches the efficiency of the "best possible" code. By co
1. Look up the SessionFactory in JNDI. 2. Call getCurrentSession() to get a Session for the current transaction.
3. Do your work.
4. Don't commit or close anything, let the container manage the transaction.
There are three different approaches:
1. Use a SortedSet or SortedMap, specifying a comparator class in the sort attribute or <set> or <map>. This solution does a sort in memory.
2. Specify an order-by attribute of <set>, <map> or <bag>, naming a list of table columns to sort by. This solution works only in JDK 1.4+.
3. Use a filter session.createFilter( collection, "order by ...." ).list()
How can I order by the size of a collection?
Use a left join, together with group by
select user from User userleft join user.messages msggroup by user order by count(msg)
from Box boxwhere box.balls is empty Or, try this:
select boxfrom Box box left join box.balls ballwhere ball is null
Hibernate is an 'Object-Relational Mapping' library. Basically it allows you to define objects in Java that map to tables or views (or other structures) of a relational database. In Java you work with the objects defined, and they provide the service of interacting with the database - CRUD (create/read/update/delete).
Hibernate is an Object Relational mapping tool. One of the things it does is convert data from relational tables to objects. I think what you are implying is why just Java objects, why not objects in other languages. For .Net, I believe there is another framework: nhibernate. However, I have not used nHibernate
This is an example of how to handle a major 'impedance mismatch' between two separate worlds. The traditional RDBMS technology precedes Java, and is independent of it. It is oriented toward a storage approach that is not object oriented, although BLOBs and similary binary type content CAN be stored in most mainstream databases now. The Hibernate project does translation between the two worlds.
1.You must have a default no-argument constructor for your persistent classes and there should be getXXX() (i.e accessor/getter) and setXXX( i.e. mutator/setter) methods for all your persistable instance variables.
2.You should implement the equals() and hashCode() methods based on your business key and it is important not to use the id field in your equals() and hashCode() definition if the id field is a surrogate key (i.e. Hibernate managed identifier). This is because the Hibernate only generates and sets the field when saving the object.
3. It is recommended to implement the Serializable interface. This is potentially useful if you want to migrate around a multi-processor cluster.
4.The persistent class should not be final because if it is final then lazy loading cannot be used by creating proxy objects.
5.Use XDoclet tags for generating your *.hbm.xml files or Annotations (JDK 1.5 onwards), which are less verbose than *.hbm.xml files.
A Great Tutorials Portal
Aquarian Infotech System
Copyright © 2008 interviewmaterial.com. All rights reserved.