A java.util.Map can be mapped with
<map>, preserving key and value
pairs. Use a java.util.HashMap to
initialize a property.
A java.util.SortedMap can be mapped
with <map> element, and the sort
attribute can be set to either a
comparator or natural ordering for
in-memory sorting. Initialize the
collection with a java.util.TreeMap
instance.
Granted, Hibernate has a wealth of features, that require some investigation to understand fully. I would suggest that whoever made this comment has not read Java Persistence with Hibernate, which is a great reference.
It's also interesting to note that Grails uses Hibernate under the covers.
Hibernate can help you solve all the three problems you listed.
(1) You need to annotate your entity classes so Hibernate is able to map between classes/objects to tables/rows. Hibernate uses a convention over configuration approach so it is possible to use just a few annotations and have a complete o/r mapping ready for use. You could use the hibernate.hbm2ddl.auto configuration option to instruct Hibernate to automatically validate/export and schema DDL when the session factory is first created.
(2) / (3) Hibernate has enough information about classes, database schema and mappings to allow it generate SQL statements for simple CRUD operations with minimal effort. You can fine tune how Hibernate loads and persists a tree of objects. Association mapping annotations have the fetch and cascade options that let you specify how associated objects are fetched (lazy / eager) and how operations are propagated through the object tree. Please refer to the Hibernate documentations for the details about these options.
In a typical scenario, Hibernate requires just a bit of configuration (one hibernate.cfg.xml file). You can define the mappings using XML files (no good) or annotations (the "default" option for new projects).
From Java Persistence with Hibernate:
<map>
, preserving key and value pairs. Use ajava.util.HashMap
to initialize a property.java.util.SortedMap
can be mapped with<map>
element, and the sort attribute can be set to either a comparator or natural ordering for in-memory sorting. Initialize the collection with ajava.util.TreeMap
instance.The Hibernate online documentation has some very good information on mapping your classes, in particular this section on setting up subclasses:
http://docs.jboss.org/hibernate/stable/core/manual/en/html/mapping.html#mapping-declaration-subclass
You want to pay special attention to using discriminator values and such if you are storing your sub-classed objects in a common table.
You might also consider picking up a copy of Java Persistence with Hibernate, which is considered the "Hibernate Bible" amongst my co-workers.
http://www.amazon.com/Java-Persistence-Hibernate-Christian-Bauer/dp/1932394885/ref=sr_1_1?ie=UTF8&s=books&qid=1272501916&sr=8-1
I would seriously question this comment.
Granted, Hibernate has a wealth of features, that require some investigation to understand fully. I would suggest that whoever made this comment has not read Java Persistence with Hibernate, which is a great reference.
It's also interesting to note that Grails uses Hibernate under the covers.
After a look through the hibernate documentation and This Book i was able to find a solution to my problem. i cannot use an interface for the moment with JPA annotations. so i used an abstract class with mappings that supported inheritance. in this example i use a single table to store all the values. but i wil look in to seperating them.
Page 537 of Java Persistence with Hibernate gives a solution using
ScrollableResults
, but alas it's only for Hibernate.So it seems that using
setFirstResult
/setMaxResults
and manual iteration really is necessary. Here's my solution using JPA:then, use it like this:
Hibernate can help you solve all the three problems you listed.
(1) You need to annotate your entity classes so Hibernate is able to map between classes/objects to tables/rows. Hibernate uses a convention over configuration approach so it is possible to use just a few annotations and have a complete o/r mapping ready for use. You could use the
hibernate.hbm2ddl.auto
configuration option to instruct Hibernate to automatically validate/export and schema DDL when the session factory is first created.(2) / (3) Hibernate has enough information about classes, database schema and mappings to allow it generate SQL statements for simple CRUD operations with minimal effort. You can fine tune how Hibernate loads and persists a tree of objects. Association mapping annotations have the fetch and cascade options that let you specify how associated objects are fetched (lazy / eager) and how operations are propagated through the object tree. Please refer to the Hibernate documentations for the details about these options.
If you are new to Hibernate, I recommend the good Hibernate documentation as reference and the book Java Persistence with Hibernate for the deeper understanding about the framework (it has very good sections about fetching and cascading).
In a typical scenario, Hibernate requires just a bit of configuration (one
hibernate.cfg.xml
file). You can define the mappings using XML files (no good) or annotations (the "default" option for new projects).