10

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?

Stupidfrog
  • 2,042
  • 6
  • 25
  • 35

2 Answers2

6

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

Stupidfrog
  • 2,042
  • 6
  • 25
  • 35
Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64
  • In address table, the company_id field name can be any,how does those table join? – Stupidfrog Aug 19 '14 at 06:07
  • @Stupidfrog in question you have mentioned **table.address.company_id is foreign key of company.** so the datatype of _company_id_ of `address` and _id_ of `company` class should be same. – Abhishek Nayak Aug 19 '14 at 06:12
  • ya, it is foreign key, but in table entity, there is no any annotation to declare that company_id is company.id foreign key. However, even using method u provided, will prompt this ERROR: column company.address_id does not exist – Stupidfrog Aug 19 '14 at 06:26
  • i just found the solution, which is 90% close to ur solution. the only different is the @PrimaryKeyJoinColumn switch to @JoinColumn(name = "company_id"), for ur solution. i will test later and will update also – Stupidfrog Aug 19 '14 at 08:15
  • @AbhishekNayak If I used your updated solution for One-to-one mapping. The Foreign key `company_id` is not getting inserted inside `Address` table. Can you help. – AshwinK Jan 23 '21 at 15:32
1

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

Amardeep
  • 348
  • 1
  • 6
  • 19