One-to_One
Hibernate one-to-one mapping, this is a simple business. One entity linked to one and only one other entity. Not in the practical world scenario, but if we continue our previous example, then one customer having only one address. This is how the rule will be defined at the three places of our concern.
Database:
Customer needs to be linked with one and only one address and vice versa. Customer table will have the address id column with unique constraint. Our table creating scripts will be like –
Customer needs to be linked with one and only one address and vice versa. Customer table will have the address id column with unique constraint. Our table creating scripts will be like –
TAB_CUSTOMER
CUSTOMER_ID - Number - Not NULL Primary Key
ADDRESS_ID - Number - Not NULL, unique Foreign Key From tab_address table
TAB_ADDRESS
ADDRESS_ID - Number - Not NULL Primary Key
Simple, isn’t it? Let us take this to one level above, i.e. hibernate mapping layer.
Hibernate Mapping:Just check the many-to-one mapping used with unique constraint on reference column.
We cannot do much to implement one to one relationship. See below.
Java:
Only constraint that we can ensure is – the customer is linked to one address but not uniquely.
Only constraint that we can ensure is – the customer is linked to one address but not uniquely.
public class Customer {
private Long id;
private Address address;
// getter/setter for id & address fields
}
public class Address {
private Long id;
//getter/setter for id
}There is one more way to implement this relation, which is using one – to – one mapping on primary keys of two tables.
Database:Scripts may look weird but this will work -
TAB_CUSTOMER
CUSTOMER_ID - Number - Not NULL Primary Key
TAB_ADDRESS
CUSTOMER_ID - Number - Not NULL Primary Key Foreign key from tab_customer
Hope you have noticed the primary key of address table is also customer id.
Hibernate Mapping:
address
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 tableOne – 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.Many-to-One
Many – to -one is a unidirectional relation i.e. it establishes relation amongst many entities with one entity. It will be clear with this example. Suppose there are 5 people staying in a house. Each of them wants to buy a mobile phone and they give same address to the service provider. Now the service provider’s database will have same address linked to these 5 people. This is exactly the scenario where many entities (customers) are linked to one entity (address).
In database this is handled by providing foreign key reference of address (primary key) in customer table. But this column in customer table will not contain unique values of customer.
Thus Many customers – are linked to – one address.
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
ADDRESS_ID Number Not NULL Foreign Key From Address table
TAB_ADDRESS
ADDRESS_ID - Number Not NULL Primary KeyMany – to – one mapping of above scenario will look like this.Point(s) to Remember:
Foreign Key reference column does not contain unique key constraint.
No comments:
Post a Comment