Hibernate Relationship

hibernate

// @JoinTable

@JoinTable(name="USER_ADDRESS") 

The above code tell Hibernate the name of the table that it should use to store collection.  
This is a field-level annotation.  This should be added to member variables that are 
collections below the @ElementCollection annotation.

@JoinTable(name="USER_ADDRESS", joinColumns=@JoinColumn(name="USER_ID"))

The above code specifies the name of the join column.

// @JoinColumn

The @JoinColumn annotation tells Hibernate to use specific column name for the join column

@OneToOne
@JoinColumn(name="VEHICLE_ID")
private Vehicle vehicle;

// @OneToOne

The @OneToOne annotation indicates a one-to-one relationship.  we have a UserDetails class and a 
Vehicle class, and each user in our system can only have one vehicle, so we add the below code to 
the UserDetails class.

@OneToOne
@JoinColumn(name="VEHICLE_ID")
private Vehicle vehicle;

// @OneToMany:

The @OneToMany annotation indicates a one-to-many relationship.  This is a field-level annotation and
goes together with a collection.

@OneToMany
private Collection<Vehicle> vehicle = new ArrayList<Vehicle>();

We had a UserDetails class, and a Vehicle class. When we add the @OneToMany  annotation, Hibernate 
creates the UserDetails_Vehicle table. We can control the name that Hibernate uses for the table, and the 
names that Hibernate uses for the columns

@OneToMany
@JoinTable(
  name="USER_VEHICLE",
  joinColumns=@JoinColumn(name="USER_ID"),
  inverseJoinColumn=@JoinColumn(name="VEHICLE_ID")
)
private Collection<Vehicle> vehicle = new ArrayList<Vehicle>();

// @ManyToOne:

The @ManyToOne annotation indicates a many-to-one relationship.  We had the UserDetails class, and a 
Vehicle class. We added the @OneToMany annotation to the vehicle member variable inside the UserDetails 
class. We also added the @ManyToOne annotation to the "user" member variable in the Vehicle class. The 
Vehicle table can have a column name USER_ID to store the ID of the user. Each vehicle, in our example, can 
only have one owner. We do not really need to create the join table. We can tell Hibernate not to create the join 
table, and just use this USER_ID column as the join column. To do this, we modified the @OneToMany 
annotation in the UserDetails table:

@OneToMany(mappedBy="user")

And we modified the Vehicle class:

@ManyToOne
@JoinColumn(name="USER_ID")
private UserDetails user;

// @ManyToMany:

Indicate a many-to-many relationship.  In the Vehicle class, modify the @ManyToMany annotation to look like:

@ManyToMany(mappedBy="vehicle")
private Collection<UserDetails> userList = new ArrayList();

This tell Hibernate to create only one join table instead of two join tables when implementing a many-to-many relationship.

// CascadeType.PERSIST

The cascade=CascadeType.PERSIST part tells Hibernate to automatically save all contained objects.

@OneToMany(cascade=CascadeType.PERSIST)
private Collection<Vehicle> vehicle = new ArrayList<Vehicle>();

In the above code, we instruct Hibernate so that every time that we save the user object, Hibernate should also save 
all the vehicles that this user owns, so that we do not have to explicitly save each individual vehicle. Now, instead  of using 
session.save to save the user, we have to use session.persist to save the user.

// Tell Hibernate not to create a join table for a many-to-one relationship
@OneToMany(mappedBy="user")
private Collection<Vehicle> vehicle = new ArrayList<Vehicle>();
...and in the other class:
@ManyToOne
@JoinColumn(name="USER_ID")
private UserDetails user;

// Tell Hibernate to create only one join table instead of two join tables when implementing a many-to-many relationship
@ManyToMany(mappedBy="vehicle")
private Collection<UserDetails> userList = new ArrayList();
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License