Hibernate – Modeling – Foreign Key Relationships

Background

Wanted to provide a working sample that demonstrates how to use plain old java object ( POJO) files with annotation.

We will simulate a foreign key relationship between city and country.

 

Database Model

location

Model Classes

Outline

Model Class – Country

  1. Table
    • Table Name :- Country
    • Unique Constraint
      • country_id
  2. Columns
    • countryID
      • name=country_ID
      • unique=true
      • nullable=false
      • variable type=short
    • country
      • name=country
      • unique=true
      • nullable=false
      • variable type=short
    • last_update
      • name=last_update
      • unique=false
      • nullable=false
      • variable type=java.sql.Timestamp
  3. Constraint
    • Foreign Key
      • @OneToMany(mappedBy=”country”)
      • private Set<City> cities;

 

Model Class – City

  1. Table
    • Table Name :- City
    • Unique Constraint
      • city_id
  2. Columns
    • city_id
      • name=cityID
      • unique=true
      • nullable=false
      • updatable = false
      • insertable = false
      • variable type=Integer
    • country_id
      • name=country
      • unique=false
      • nullable=false
      • variable type=short
    • city
      • name=city
      • unique=false
      • nullable=false
      • variable type=string
    • last_update
      • name=last_update
      • unique=false
      • nullable=false
      • variable type=java.sql.Timestamp
  3. Constraint
    • Foreign Key
      • @ManyToOne
      • @JoinColumn
        • name=country_id

Classes

Model Class – Country


package sakila;

import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.util.Set;

@Entity
@Table(name = "country"
            , uniqueConstraints =
                {
                    @UniqueConstraint(columnNames = "country_ID")
                }
      )

public class Country
{

    @Id
    @Column(name = "country_ID", unique = true, nullable = false)
    private short CountryID;

    @Column(name = "country", unique = false, nullable = false, length = 60)
    private String Country;

    @Column(name = "last_update", unique = false, nullable = true)
    private  Timestamp tsUpdate;    

    public Country()
    {

    }

    @OneToMany(mappedBy="country")
    private Set cities;

    public Timestamp gettsUpdate()
    {
        return tsUpdate;
    }

    public void settsUpdate(Timestamp tsUpdate)
    {
        this.tsUpdate = tsUpdate;
    }

    public short getCountryID()
    {
        return CountryID;
    }

    public void setCountryID(short countryID)
    {
        CountryID = countryID;
    }

    public String getCountry()
    {
        return Country;
    }

    public void setCountry(String country)
    {
        Country = country;
    }

}

Model Class – City


package sakila;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import java.sql.Timestamp;
//import org.hibernate.annotations.OptimisticLockType;

@Entity
@Table(
              name = "city"
            , uniqueConstraints =
                {
                    @UniqueConstraint
                    (
                        columnNames = "city_ID"
                    )
                }
      )

public class City
implements Serializable
{

    private static final long serialVersionUID = -1798070786993154676L;

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    (
          name          = "city_ID"
        , unique        = true
        , nullable      = false
        , updatable     = false
        , insertable    = false
    )
    private Integer cityID;

    @Id
    @ManyToOne
    @JoinColumn
    (
          name="country_id"
        , nullable=false
    )
    private Country country;

    @Id
    @Column
    (
          name = "city"
        , unique = false
        , nullable = false
        , length = 60
    )
    private String city;

    @Column
    (
          name = "last_update"
        , unique = false
        , nullable = true
    )
    private  Timestamp tsUpdate;

    // Accessors
    public Timestamp gettsUpdate()
    {
        return tsUpdate;
    }

    public void settsUpdate(Timestamp tsUpdate)
    {
        this.tsUpdate = tsUpdate;
    }

    public Country getCountry()
    {
        return country;
    }

    public void setCountry(Country value)
    {
        country = value;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String value)
    {
        city = value;
    }

}

Source Control

GitHub

DanielAdeniji/javaHibernateSQLServerSakila
Link

  1. Model Files
    • Country
    • City

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s