1
2
To illustrate different Hibernate association mappings between entities (domain model) we first need an application to build.
3
4
In an application we need to retrieve all the vehicles for a given category, hence we need relationship from Category side.
5
Other Generator elements: •sequence uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int. •hilo uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column as a source of hi values. •uuid uses a 128-bit UUID algorithm to generate identifiers of type string that are unique within a network .The UUID is encoded as a string of 32 hexadecimal digits in length. •native selects identity, sequence or hilo depending upon the capabilities of the underlying database. •assigned lets the application assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified. •foreign uses the identifier of another associated object. It is usually used in conjunction with a <one-to-one> primary key association.
6
Set Configuration: <set name=“propertyNameofSetCollection”> <key column=“FOREIGN_KEY” /> <one-to-many class=“ChildClass” /> </set>
Since the child entities are being stored in collections, it becomes critical that you have appropriately implemented equals() and hashCode() methods for those entities. Adds a foreign key constraint with the name CATEGORY_FK to the database catalog: <key column="CATEGORY_ID" foreign-key="CATEGORY_FK"/>
Note: Apart from <set>, there is also <list>, <map>, <bag>, <array> and <primitivearray> mapping elements.
7
8
9
10
Cascade Options: none, all, save-update, delete, delete-orphan Refer Transitive persistence: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objects tate-transitive Refer: http://www.mkyong.com/hibernate/hibernate-cascade-example-save-updatedelete-and-delete-orphan/
11
Refer:
12
13
14
15
16
As far as the database is concerned, there is no difference between this and the unidirectional one-to-many association One-to-many and many-to-one associations are not independent of each other but rather, one is the inverse of the other and the same foreign key column is used for both associations.
17
18
19
20
Without the inverse attribute, Hibernate tries to execute two different SQL statements, both updating the same foreign key column, when you manipulate the link between two instances. By specifying inverse="true", you explicitly tell Hibernate which end of the link it should not synchronize with the database. In this example, you tell Hibernate that it should propagate changes made at the Vehicle end of the association to the database, ignoring changes made only to the vehicles collection. By specifying inverse="true", you explicitly tell Hibernate which end of the link it should not synchronize with the database. If you only call category.getVehicles().add(vehicle); no changes are made persistent! You get what you want only if the other side, vehicle.setCategory(category); is set correctly.
Note: a)If an association is bidirectional, you have to create the link with pointers on two sides, not just one. b) The <many-to-one> element doesnâ&#x20AC;&#x2122;t have an inverse attribute.
21
22
23
24
25
26
Customer can refer to one entity of type Address. Similarly Address entity can refer to only one entity of type Customer.
27
create table ADDRESS ( ADDRESS_ID integer not null, STREET varchar(255), CITY varchar(255), STATE varchar(255), primary key (ADDRESS_ID) )
28
Hibernate generated DDL for the Customer mapping: create table CUSTOMER ( FIRST_NAME varchar(255) not null, LAST_NAME varchar(255) not null, EMAIL varchar(50), ADD_ID integer unique, primary key (FIRST_NAME, LAST_NAME) ) alter table CUSTOMER add index ADDRESS_FK (ADD_ID), add constraint ADDRESS_FK foreign key (ADD_ID) references ADDRESS (ADDRESS_ID)
29
30
31
32
33
34
35
Hibernate generated DDL: create table VEHICLE_RENTAL ( RENTAL_ID bigint not null, BOOKING_START_DATE date, BOOKING_END_DATE date, PAYMENT_RECEIVED char(1), VEHICLE_ID integer, FIRST_NAME varchar(255), LAST_NAME varchar(255), primary key (RENTAL_ID) ); alter table VEHICLE_RENTAL add index FKED890DB7528573C6 (FIRST_NAME, LAST_NAME), add constraint FKED890DB7528573C6 foreign key (FIRST_NAME, LAST_NAME) references CUSTOMER (FIRST_NAME, LAST_NAME); alter table VEHICLE_RENTAL add index FKED890DB79F1E4060 (VEHICLE_ID), add constraint FKED890DB79F1E4060 foreign key (VEHICLE_ID) references VEHICLE (VEHICLE_ID);
36
37
38
39
The many-to-many association can be modeled as two one-to-many associations to an intervening entity class representing the link table.
40
Java associations between Book and Author represents many-to-many multiplicity.
41
Java associations can have many-to-many multiplicity. Table associations are always one-tomany or one-to-one. If you wish to represent a many-to-many association in a relational database, you must introduce a new table, called a link table. This table doesnâ&#x20AC;&#x2122;t appear anywhere in the domain model and a many-to-many association mapping hides the intermediate association table from the application. Table Schemas: create table AUTHOR ( AUTHOR_ID integer not null, AUTHOR_NAME varchar(50), AUTHOR_EMAIL varchar(50), primary key (AUTHOR_ID) ); create table BOOK ( BOOK_ISBN varchar(255) not null, BOOK_TITLE varchar(255), BOOK_PRICE double precision, primary key (BOOK_ISBN) ); create table BOOK_AUTHOR ( AUTHOR_REF_ID integer not null, BOOK_REF_ID varchar(255) not null,
42
Many-to-many Association using Link table: Sample data illustrates that “Gavin King” has written “Java Persistence with Hibernate” and “Hibernate in Action”. Also the sample data illustrates that “Java Persistence with Hibernate” is written by “Gavin King” and “Christian Bauer”.
43
44
45
46
47
48
49
50
51