Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map foreign key relations in hibernate

Tags:

java

hibernate

I have customer class and Address class as below: officeAddressId,homeAddressId, and secondaryAddressId in Customer class are for foreign key mapping in tables.

  public class customer implements serializable
    {
    private static final long serialVersionUID= -5830229553758180137L;
    int age;
    String officeAddressId= null;
    String homeAddressId= null;
    String secondaryAddressId= null;
    }

public class Address implements serializable
{
        private static final long serialVersionUID= -5130229553758180137L;
        private String              addressId           = null;
    private String              addressLine         = null;
    private String              cityName            = null;
    private String              stateName           = null;
    private String              countryName         = null;
    private String              pincode             = null;
}

My database table is straight forward:

CREATE TABLE customer
(
customerID varchar(40) primary key,
officeAddressId varchar(40),
homeAddressId varchar(40),
secondaryAddressId varchar(40),
age int 
);

CREATE TABLE Address
(
addressID varchar(40) primary key,
addressLine varchar(40),
cityName varchar(40),
stateName varchar(40),
countryName varchar(40),
pincode varchar(10),
);

I make address objects (3 objects for address one for home,office and secondarycontact) and customer object at service layer and open transaction. I am not sure how should I give the foreign key relation in hbm mapping files and how do I save these four objects(3 address objects and 1 customer object) and in which order that the foreign key relations are persisted in database correctly.

Thanks in advance....

like image 254
nanosoft Avatar asked Oct 23 '25 12:10

nanosoft


1 Answers

First, change name of your customer class to Customer. Then:

public Class Customer implements Serializable {
    ...

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "office_address_id")
    private Address officeAddress;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "home_address_id")
    private Address homeAddress;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "secondary_address_id")
    private Address secondaryAddress;

    ...
}

and

public Class Address implements Serializable {
    ...

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "officeAddress")
    private Set<Customer> officeCustomers = new HashSet<Customer>(0);

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "homeAddress")
    private Set<Customer> homeCustomers = new HashSet<Customer>(0);

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "secondaryAddress")
    private Set<Customer> secondaryCustomers = new HashSet<Customer>(0);

    ...
}

and of course you can create getter for all customers in Address class.

like image 77
paulek Avatar answered Oct 25 '25 01:10

paulek