Search This Blog

Wednesday 30 November 2011

One To One Association Using Shared Primary Keys

Consider the example of an Order. The Order includes various details such as items brought, billing information, mode of payment, delivery address etc.
One way to represent all these information is to have a single order table and then implement the different data as components. This would be great from the point of performance and ease of management.
However there may be cases wherein the data may be needed in separate tables.For example the Accounts module might need reference to the Billing Information. The Shipping module might need information related to the delivery address and so on.
The point is that we may be needed to implement these as individual entities with one-to-one relations among them. For example

Monday 28 November 2011

Collections and The Inverse Attribute

In a previous post I successfully created a bidirectional relationship between Shelf and Books. We also saw that hibernate treats these as two individual unidirectional relationships in the application.

Saturday 26 November 2011

cascade = "delete-orphan"

In an earlier post we have seen the various settings supported by cascade property. In a parent-child relationship we are provided with three different type of cascades:
  • save-update
  • delete
  • delete-orphan

Thursday 24 November 2011

Bi Directional Associations

In earlier posts I created associations between Parent and Entity from both sides in an unidirectional manner. Either I created a reference to parent from within the child element or I added a set of children inside parent.
Here I shall use a bidirectional relationship.

Tuesday 15 November 2011

Simplest UniDirectional Association in Hibernate

I decided to implement an unidirectional many-to-one association.
A Shelf contains many books. Each book can exist on only one shelf. No book can exist without being assigned a shelf.
The above would be represented in database by two tables - Shelf and a Book.

Uni Directional Collections

In this example I created the relation between shelf and book and set the direction of relation from Book to Shelf. i.e. the Book is aware of the shelf. Which is the same case as the db schema. The foreign key is in the Book table and not vice-versa.

Saturday 12 November 2011

Collections: Entities vs Values

We have seen that Hibernate supports both collection of Entities and collection of values / components. Here is a short summary of the points of note when working with the two.

Wednesday 9 November 2011

Creating A Custom Id Generator

We have already seen the different identifier generators supported by Hibernate. However this does not constrain us to use these generators only. Hibernate very magnanimously allows us to create our own custom generator.

Sunday 6 November 2011

Deleting from Collections

In the previous examples I created and used collections that represented simple value elements or components. However one thing that I wanted to try is deleting from collections. Removing the value from the collection for a session bound

Thursday 3 November 2011

Collections of Components

Earlier I created varied collections using the Java String class. In this example I will replace that with a Component class.
public class Chocolate {
    private String name;
    private String brand;