Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA, Hibernate: OneToOne mapping with foreign key only

Environment:

  • Hibernate 4.1.6.final
  • Spring 3.1.2.release
  • Spring JPA 1.1.0.release
  • PostgreSQL 9.1-901-1.jdbc4

I decided to rephrase the questions.

There are 2 tables:

public company
{
  private Long id;
  private Long name;
  private address table_address;
}
public address
{
  private Long id;
  private String address;
  private Long company_id;
}

Note: both table id is sequential and no related. Except table.address.company_id is foreign key of company.

how to do mapping? what result i expected is:

"company":{
            "id":4,
            "name":"company name",
            "address":{
                         "id":3,
                         "address":"anywhere",
                         "company_id":4
                      }
          }

So can somebody teach me that, how to map this 2 table?

like image 601
Stupidfrog Avatar asked Aug 19 '14 05:08

Stupidfrog


People also ask

How do I map a OneToOne JPA?

The best way to map a @OneToOne relationship is to use @MapsId . This way, you don't even need a bidirectional association since you can always fetch the PostDetails entity by using the Post entity identifier. This way, the id property serves as both Primary Key and Foreign Key.

How do you make a foreign key a primary key in Hibernate?

You can use JPA's @MapsId annotation to tell Hibernate that it shall use the foreign key of an associated entity as the primary key. Let's take a look at a simple example. Each Book has a Manuscript, and each Manuscript belongs to 1 Book. The foreign key of the Book is also the primary key of the Manuscript.

Which annotation is used for foreign key in Hibernate?

7.2. This foreign key is referred to as the collection key column, or columns, of the collection table. The collection key column is mapped by the @JoinColumn annotation respectively the <key> XML element. In annotations the Hibernate specific annotation @OnDelete has to be used.

What is difference between JPA unidirectional OneToOne and ManyToOne?

The main difference between a OneToOne and a ManyToOne relationship in JPA is that a ManyToOne always contains a foreign key from the source object's table to the target object's table, whereas a OneToOne relationship the foreign key may either be in the source object's table or the target object's table.


2 Answers

what you want is One-to-One mapping between Company and Address

just add @OneToOne annotation to table_address field of Company class:

 public class Address {

        @Id
        @GeneratedValue
        private Long id;
        private String address;
        @OneToOne
        @PrimaryKeyJoinColumn
        private Company company;

        //getters and setters
    }

public class Company {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    @OneToOne(mappedBy = "company",cascade = CascadeType.ALL)
    private Address companyAddress;

    //getters and setters
}

Apart from the problem: respect java naming convention, in your case class name should start with capital letter and the next word in the variable name too. i.e. company should be Company and address should be Address, private address table_address; change to private Address companyAddress;

Updated solution

public class Address {

    @Id
    @GeneratedValue
    private Long id;
    private String address;
    @OneToOne
    @JoinColumn(name = "company_id")
    private Company company;

    //getters and setters
}

public class Company {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    @OneToOne(mappedBy = "company",cascade = CascadeType.ALL)
    private Address companyAddress;

    //getters and setters
}

from stupidfrog: if u using @PrimaryKeyJoinColumn, then the id join wrong. the id join with both primary but not address table company_id

here is the reference from hibernate http://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/OneToOne.html the example 1

like image 109
Abhishek Nayak Avatar answered Oct 10 '22 08:10

Abhishek Nayak


You should create two entity classes as follows

COMPANY ENTITY

    @Entity
    @Table(name = "YOUR TABLE NAME")
    public company{

       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       @Basic(optional = false)
       @Column(name = "COMPANY_ID")
       private Long id;
       @Column(name = "NAME")
       private Long name;
       @JoinColumn(name = "ADDRESS_ID")
       @OneToOne(optional = false)
       private address table_address;
    }

ADDRESS ENTITY

   @Entity
   @Table(name = "YOUR TABLE NAME")
   public address{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    private Long id;
    @Column(name = "ADDRESS")
    private String address;
    @JoinColumn(name = "COMPANY_ID")
    @OneToOne(optional = false)
    private Long company_id;
}

Hope this helps

like image 38
Amardeep Avatar answered Oct 10 '22 08:10

Amardeep