Hibernate Association Mappings: One – To – Many

In continuation to previous article, here we focus on next association in Unidirectional context. One – to – Many links one entity with many other entities. To explain it better, let us take an example. One customer can have one permanent address, one temporary address and one work address. Thus one customer is linked to many addresses.

In database this can be handled in very unusual way, by providing foreign key reference of customer (primary key) in address table. Thus One customers – is linked to – Many addresses.

Let us put our example in code.
This is how our table’s primary columns look:

TAB_CUSTOMER
	CUSTOMER_ID - Number - Not NULL Primary Key
TAB_ADDRESS
	ADDRESS_ID - Number - Not NULL Primary Key
	CUSTOMER_ID - Number - Not NULL Foreign Key From tab_Customer table

One – to – Many mapping of above scenario will look like this.






   
   






Now the Java code should also map to our this mapping definition.

public class Customer {
    private Long id;
    private Set address;
    // getter/setter for id & address fields
}     

  public class Address {
    private Long id;
    // getter/setter for id field
}

Point(s) to Remember:

This is unusual way of association mapping on foreign key and it is not recommended.

  • Share/Bookmark
Hibernate, Tech Notes

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment

(required)

(required)