Hibernate

Misc

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

Collection Mapping

Set<String>

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>

SortedSet<String>

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>

List<String>

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>

Map<String, String>

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>

List<Planet>

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>
 
write a message
Name


City


Email (won't be displayed)




Antispam: enter the current year (2008)