Star.java
private Set<String> planets = new HashSet<String>();
Star.hbm.xml
<set name="planets" table="star_planet"> <key column="star_id" /> <element type="text"/> </set>
Star.java
private SortedSet<String> planets = new TreeSet<String>();
Star.hbm.xml
<set name="planets" table="star_planet" sort="natural"> <key column="star_id" /> <element type="text"/> </set>
Star.java
private List<String> planets = new LinkedList<String>();
Star.hbm.xml
<list name="planets" table="star_planet"> <key column="star_id" /> <list-index column="index" /> <element type="text"/> </list>
Star.java
private Map<String, String> planets = new HashMap<String, String>();
Star.hbm.xml
<map name="planets" table="star_planet"> <key column="star_id" /> <map-key type="text" column="planet_name" /> <element type="text" column="planet_description"/> </map>
Star.java
private List<Planet> planets = new LinkedList<Planet>();
Star.hbm.xml
<list name="planets" table="star_planet"> <key column="star_id" /> <list-index column="index" /> <many-to-many column="planet_id" class="Planet"/> </list>
In Eclipse:
For each model class (Toto.java):
Note: when using inheritance, subclasses' Hibernate mapping can be done in their superclass' mapping file.
Declare each Hibernate file in hibernate.cfg.xml
For each model class:
For each model class:
Enclose the table or column name in backticks
<class name="User" table="`user`"> </class>
Reference: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#mapping-quotedidentifiers
Then for command line SQL queries (PostGreSQL):
SELECT * FROM "user"; -- or SELECT * FROM public.user;
Configuration cfg = new Configuration();
cfg.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
cfg.configure();
To generate the SQL schema accordingly with ant:
<target name="schemaexport"> <hibernatetool destdir="./"> <configuration namingstrategy="org.hibernate.cfg.ImprovedNamingStrategy" configurationfile="${build.dir}/hibernate.cfg.xml"/> <hbm2ddl export="false" create="true" drop="false" delimiter=";" outputfilename="schema-export.sql"/> </hibernatetool> </target>
Example:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="ca.sfm.entity.characters"> <class name="Toto"> <id name="id"> <generator class="native"/> </id> <version name="version"/> <property name="aPerfectAddress" type="text"/> <property name="mySecretAge" /> </class> </hibernate-mapping>
Notion of automatic dirty checking. Flushing is the process of synchronizing the memory state with the database. Updating makes a detached object persistent again (binds it to a new unit of work): so the actual database data will be updated.
Bi-directional links: it's important to set the link on both sides. All bi-directional associations need one side as inverse.
Sets: a defensive programming method is to set the getter/setter as protected, and add addToAnimals and removeFromAnimals methods.
Use one Hibernate session per request.
setFlushMode(FlushMode.MANUAL) is for read-only transactions. Test if it actually speeds things up.Ref
crit.createAlias(“parent”, “parent”, CriteriaSpecification.LEFT_JOIN); Create an alias without excluding those with parent==null