<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tim Van Wassenhove &#187; Hibernate</title>
	<atom:link href="http://www.timvw.be/category/information-technology/java/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timvw.be</link>
	<description>The journey of a thousand miles begins with one step.</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:07:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>A TableModel for Entity Beans</title>
		<link>http://www.timvw.be/a-tablemodel-for-entity-beans/</link>
		<comments>http://www.timvw.be/a-tablemodel-for-entity-beans/#comments</comments>
		<pubDate>Sun, 18 Jun 2006 00:28:22 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Hibernate]]></category>

		<guid isPermaLink="false">http://www.timvw.be/a-tablemodel-for-entity-beans/</guid>
		<description><![CDATA[For my graduation project i needed a component that could display a list of Entity Beans. I found that JTable is such a component and with the help of custom TableCellRenderer and TableCellEditor components i was able to customize the rendering to my needs. In order to get the data into the JTable i implemented [...]]]></description>
			<content:encoded><![CDATA[<p>For my graduation project i needed a component that could display a list of <a href="http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/EJBConcepts4.html">Entity Beans</a>. I found that <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTable.html">JTable</a> is such a component and with the help of custom <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/table/TableCellRenderer.html">TableCellRenderer</a> and <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/table/TableCellEditor.html">TableCellEditor</a> components i was able to customize the rendering to my needs. In order to get the data into the JTable i implemented a custom <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/table/TableModel.html">TableModel</a>, namely <a href="http://www.timvw.be/wp-content/code/java/EntityTableModel.java.txt">EntityTableModel</a>. Now i can easily generate a JTable that displays Entity Beans: </p>

<pre class="brush: java;">
// fetch the elements we want to display
Object[] elements = employeeController
  .getEntityManager()
  .createNamedQuery(&quot;findEmployees&quot;)
  .getResultList()
  .toArray();

// build map with column name, entity attribute pairs
HashMap&lt;String, String&gt; colAttrs = new LinkedHashMap&lt;String, String&gt;();
colAttrs.put(&quot;First Name&quot;, &quot;firstName&quot;);
colAttrs.put(&quot;Last Name&quot;, &quot;lastName&quot;);

// build collection with editable attributes
Collection&lt;String&gt; editables = new ArrayList&lt;String&gt;();

// initialise the tablemodel
TableModel tableModel = new EntityTableModel(elements, colAttrs, editables);

// initialise the table
JTable table = new JTable(tableModel);
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/a-tablemodel-for-entity-beans/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Couple of screenshots of my graduation project</title>
		<link>http://www.timvw.be/couple-of-screenshots-of-my-graduation-project/</link>
		<comments>http://www.timvw.be/couple-of-screenshots-of-my-graduation-project/#comments</comments>
		<pubDate>Mon, 12 Jun 2006 19:12:28 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Hibernate]]></category>

		<guid isPermaLink="false">http://www.timvw.be/couple-of-screenshots-of-my-graduation-project/</guid>
		<description><![CDATA[Here are a couple of screenshots of my graduation project i&#8217;ve been working on last couple of weeks. The GUI is built with Swing, the bussiness rules make extensive use of the Apache BeanUtils and Hibernate Validators packages and with PostgreSQL we have a solid database. Since it takes a while to load the EntityManagerFactory [...]]]></description>
			<content:encoded><![CDATA[<p>Here are a couple of screenshots of my graduation project i&#8217;ve been working on last couple of weeks. The GUI is built with Swing, the bussiness rules make extensive use of the Apache BeanUtils and Hibernate Validators packages and with PostgreSQL we have a solid database. Since it takes a while to load the EntityManagerFactory we&#8217;ve added a simple splashscreen:</p>

<a href="http://www.timvw.be/wp-content/images/grad01.png"><img src="http://www.timvw.be/wp-content/images/gradtn01.png" alt="splashscreen"/></a>

<p>The base screen displays a map with the tables in the restaurant. The color of the tables depends on the state of the visit (eg: just arrived, ordered some items, eating soup, eating dessert, paid the bill). There is also the possiblity to group tables. Offcourse you have the possibility to rearrange the tables and add/remove the maps:</p>

<a href="http://www.timvw.be/wp-content/images/grad02.png"><img src="http://www.timvw.be/wp-content/images/gradtn02.png" alt="groundplan restaurant"/></a>


<a href="http://www.timvw.be/wp-content/images/grad03.png"><img src="http://www.timvw.be/wp-content/images/gradtn03.png" alt="groundplan terrace"/></a>

<p>As soon as the waiter has choosen a visit he can add orders via an easy to use screen. He also has the possibility to add (pre-configured and/or custom) remarks to a given order. Each order is printed on the printer of the division that is responsible for the handling for the given type of menuitems:</p>

<a href="http://www.timvw.be/wp-content/images/grad04.png"><img src="http://www.timvw.be/wp-content/images/gradtn04.png" alt="order panel"/></a>

<a href="http://www.timvw.be/wp-content/images/grad05.png"><img src="http://www.timvw.be/wp-content/images/gradtn05.png" alt="remarks dialog"/></a>

<p>And least but not least, there is a screen that allows the waiter to generate, print and save one or more bills for each visit. If all ordered items have been paid, the visit goes into the PAID state:</p>

<a href="http://www.timvw.be/wp-content/images/grad06.png"><img src="http://www.timvw.be/wp-content/images/gradtn06.png" alt="billing panel"/></a>

<p>Offcourse we&#8217;ve also written list/detail panels for all the tables this restaurant uses. Here are screenshots of the list and detail panel for the menuitems:</p>

<a href="http://www.timvw.be/wp-content/images/grad07.png"><img src="http://www.timvw.be/wp-content/images/gradtn07.png" alt="menuitem list"/></a>

<a href="http://www.timvw.be/wp-content/images/grad08.png"><img src="http://www.timvw.be/wp-content/images/gradtn08.png" alt="menuitem detail"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/couple-of-screenshots-of-my-graduation-project/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ManyToMany relationships with Java Persistence (JSR-220)</title>
		<link>http://www.timvw.be/manytomany-relationships-with-java-persistence-jsr-220/</link>
		<comments>http://www.timvw.be/manytomany-relationships-with-java-persistence-jsr-220/#comments</comments>
		<pubDate>Sat, 28 Jan 2006 00:27:24 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Hibernate]]></category>

		<guid isPermaLink="false">http://www.timvw.be/manytomany-relationships-with-java-persistence-jsr-220/</guid>
		<description><![CDATA[The scenario is as following: each table can have many reservations, and each reservation can span many tables. Here is our first trial: // Table.java @ManyToMany( targetEntity=Reservation.class ) public List getReservations() {..} // Reservation.java @ManyToMany( targetEntity=Table.class ) public List getTables() {..} We end up with linktables tables_reservations and reservations_tables. This is not what we want. [...]]]></description>
			<content:encoded><![CDATA[<p>The scenario is as following: each table can have many reservations, and each reservation can span many tables. Here is our first trial:</p>
<pre class="brush: java;">
// Table.java
@ManyToMany(
  targetEntity=Reservation.class
)
public List getReservations() {..}

// Reservation.java
@ManyToMany(
  targetEntity=Table.class
)
public List getTables() {..}
</pre>
<p>We end up with linktables tables_reservations and reservations_tables. This is not what we want. It shouldn&#8217;t be possible to remove a table when there are still reservations related to that table. We change our code so that table becomes the owner of the relationship.</p>
<pre class="brush: java;">
// Table.java
@ManyToMany(
  targetEntity=Reservation.class, 
  mappedBy=&quot;tables&quot;
)
public List getReservations() {..}
</pre>
<p>Allright, now we only have the linktable reservations_tables. Exactly the same as we experienced with <a href="http://www.timvw.be/onetomany-relationships-with-java-persistence-jsr220/">OneToMany</a> relationships the cascading persist works when we persist a child (reservation). If we want the cascading persist to work when we persist a parent (table) we have to make sure that the child (reservation) has the parent set. An important difference is that we can&#8217;t enforce this thus i advise not to use the cascade attribute on the owner (table).</p>
<pre class="brush: java;">
// Table.java
@ManyToMany(
  targetEntity=Reservation.class,
  mappedBy=&quot;tables&quot;
) 
public List getReservations() {..}

// Reservation.java
@ManyToMany(
  targetEntity=Table.class, 
  cascade=CascadeType.PERSIST
)
public List getTables() {..}
</pre>
<p>We decide to use the <a href="http://www.hibernate.org/hib_docs/annotations/reference/en/html/validator.html">Hibernate Validator</a> classes to enforce that a reservation is associated with at least one table. For some reason the SizeValidator returns true if the value is null thus we also add a NotNullValidator:</p>
<pre class="brush: java;">
// Reservation.java
import org.hibernate.validator.*;
@ManyToMany(
  targetEntity=Table.class,
  cascade=CascadeType.PERSIST
)
@NotNull 
@Size(min=1) 
public List getTables() { .. }
</pre>
<p>Ok, this allows us to enforce that reservations are always associated with at least one table. We decide to allow cascading persist on the parent.</p>
<pre class="brush: java;">
@ManyToMany(
  targetEntity=Reservation.class, 
  mappedBy=&quot;tables&quot;, 
  cascade=CascadeType.PERSIST
)
public List getReservations() {..}
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/manytomany-relationships-with-java-persistence-jsr-220/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OneToMany relationships with Java Persistence (JSR 220)</title>
		<link>http://www.timvw.be/onetomany-relationships-with-java-persistence-jsr220/</link>
		<comments>http://www.timvw.be/onetomany-relationships-with-java-persistence-jsr220/#comments</comments>
		<pubDate>Wed, 25 Jan 2006 19:36:36 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Hibernate]]></category>

		<guid isPermaLink="false">http://www.timvw.be/onetomany-relationships-with-java-persistence-jsr220/</guid>
		<description><![CDATA[Let&#8217;s experiment a bit with Hibernate. We want to model the tables employee and shift. Each employee has multiple shifts (and each shift belongs to an employee). We want an employee table and a shift table (which has the employee_id as a foreign key). // Employee.java @OneToMany(targetEntity=Shift.class) public List getShifts() {...} // Shift.java @ManyToOne public [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s experiment a bit with <a href="http://www.hibernate.org">Hibernate</a>. We want to model the tables employee and shift. Each employee has multiple shifts (and each shift belongs to an employee). We want an employee table and a shift table (which has the employee_id as a foreign key).</p>
<pre class="brush: java;">
// Employee.java
@OneToMany(targetEntity=Shift.class) public List getShifts() {...}
// Shift.java
@ManyToOne public Employee getEmployee() {...}
</pre>
<p>The code above generates the following tables: employee, employee_shift and shift. Time to read the documentation and discover the mappedBy attribute.</p>
<pre class="brush: java;">
// Employee.java
@OneToMany(targetEntity=Shift.class, mappedBy=&quot;employee&quot;) public List getShifts() {...}
</pre>
<p>Ok, now we get the tables we want. Let&#8217;s remove an employee that has shifts referencing him. An exception is thrown because of a foreign key constraint.</p>
<pre class="brush: java;">
// Employee.java
@OneToMany(targetEntity=Shift.class, mappedBy=&quot;employee&quot;, cascade=CascadeType.REMOVE) public List getShifts() {...}
</pre>
<p>Ok, now we have cascading deletes working. Let&#8217;s try cascading inserts:</p>
<pre class="brush: java;">
// Employee.java
@OneToMany(targetEntity=Shift.class, mappedBy=&quot;employee&quot;, cascade=CascadeType.ALL) public List getShifts() {...}
// Main.java
Employee employee = new Employee();
Shift shift = new Shift();
Vector shifts = new Vector();
shifts.add(shift);
employee.setShifts(shifts);
em.persist(employee);
</pre>
<p>With the code above the foreign key of shift will NULL instead of the employee_id. According to the documentation it is expected behaviour that cascading inserts only work when the children are saved.</p>
<pre class="brush: java;">
// Employee.java
@OneToMany(targetEntity=Shift.class, mappedBy=&quot;employee&quot;, cascade=CascadeType.ALL) public List getShifts() {...}
// Main.java
Employee employee = new Employee();
Shift shift = new Shift();
shift.setEmployee(employee);
Vector shifts = new Vector();
shifts.add(shift);
employee.setShifts(shifts);
em.persist(employee);
</pre>
<p>It appears we can get cascading inserts to work as long as we don&#8217;t forget to set the employee. We add a constraint so employee can&#8217;t be null.</p>
<pre class="brush: java;">
// Shift.java
@ManyToOne @Column(nullable=false) public Employee getEmployee() {...}
// Main.java
Shift shift = new Shift();
em.persist(shift);
</pre>
<p>WTF? I can persist a NULL reference anyway? The value for employee_id is allowed to be NULL? Have we found a bug? Let&#8217;s check the documentation:</p>
<pre class="brush: java;">
// Shift.java
@ManyToOne @JoinColumn(nullable=false) public Employee getEmployee() {...}
// Main.java
Shift shift = new Shift();
em.persist(shift);
</pre>
<p>Ok, now are getting somewhere. And exception is thrown if we try to persist a shift that references to an employee that doesn&#8217;t exist in the database yet.</p>
<pre class="brush: java;">
// Shift.java
@ManyToOne(cascade=CascadeType.PERSIST) @JoinColumn(nullable=false) public Employee getEmployee() {...}
</pre>
<p>CascadeType.ALL means that i want to cascade events on ALL events. My only requirements where that i cascade on PERSIST (insert) and on REMOVE (delete). So i need to change my annotation as following:</p>
<pre class="brush: java;">
// Employee.java
@OneToMany(
  targetEntity=Shift.class, 
  mappedBy=&quot;employee&quot;, 
  cascade={
    CascadeType.PERSIST,
    CascadeType.REMOVE
  }
)
public List getShifts() {return shifts;}
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/onetomany-relationships-with-java-persistence-jsr220/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
