Tuesday, October 18, 2011

One to Many unidirectional association

In One to many unidirectional association, we will only be able to navigate in one direction. (i.e)
only one side of the association will have the reference to the other side. The one side of the association will implement one of the collection interfaces, if it has the reference to the other entity.

Mapping using Hibernate XML mapping:

Let us go to the package com.demo.oneToManyUnidirectional and execute the class OneToManyUniExecutor.java

Things to look into:

* The ORM classes User.java and Account.java
* The XML mapping file oneToManyUnidirectional.hbm.xml

public class User {

private long id;

private String name;
// getter and setter methods
}

public class Account {

private long id;

private String accountType;

private User user;

// getter and setter methods
}

* Note that only the Account class has the reference to the User and the User class does not have the reference back to Account. This is a unidirectional kind of association.

XML:

<hibernate-mapping auto-import="false">
<class name="com.demo.oneToManyUnidirectional.User" table="USER"
lazy="false">
<id name="id" column="USER_ID"> <generator class="native" /> </id> <property name="name" column="NAME"/>
</class>

<class name="com.demo.oneToManyUnidirectional.Account" table="ACCOUNT" lazy="false">
<id name="id" column="ACCOUNT_ID"> <generator class="native" /> </id>
<property name="accountType" column="ACCOUNT_TYPE"/>

<
many-to-one name="user" column="USER_ID" class="com.demo.oneToManyUnidirectional.User" not-null="true"/>
</class>
</hibernate-mapping>

* <many-to-one> refers the many-to-one association from the Account entity to User entity. * The association explains that an user can have any number of accounts but an account will point only one user.

Execute the class OneToManyUniExecutor.java and verify the results in the HSQL DB manager by executing simple select queries on the tables User and Account as follows.



Mapping by Annotations:

Let us go to the package annotation.com.demo.oneToManyUnidirectional and execute the class OneToManyUniAnnotationExecutor.java

Things to look into:

* The annotated ORM classes User.java and Account.java

@Entity(name="annotation.com.demo.oneToManyUnidirectional.User")
@Table(name="USER")
public class User {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="USER_ID")
private long id;

@Column(name="NAME")
private String name;

// getter and setter methods
}

@Entity
@Table(name="ACCOUNT")
public class Account {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ACCOUNT_ID")
private long id;

@Column(name="ACCOUNT_TYPE")
private String accountType;

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="USER_ID",nullable=false)
private User user;

// getter and setter methods
}

* @
ManyToOne represents the many-to-one association from Account to User
*
@JoinColumn represents that the primary key in the User table is the foreign key column in the Account table.

Note: In some places we use the fully qualified name in the @Entity, because as we use the same entity names in different examples, we should distinguish the entity names by specifying their package name.

Once the OneToManyUniAnnotationExecutor.java is executed, verify the results as described above.