JPA, Hibernate: displaying OneToOne with a 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 identifiers are consecutive and not related to each other. Except table.address.company_id is the foreign key of the company.

How to make a comparison? what result i expected:

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

So can someone teach me this how to map this 2 table?

+6
source share
2 answers

what you want is a one-on-one mapping between Company and Address

just add the @OneToOne annotation in the table_address field of the 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 } 

<sub> In addition to the problem: respect the java naming convention, in your case the class name should start with a capital letter and the next word in the variable name. that is, the company must Company , and the address must be Address , private address table_address; change to private Address companyAddress; Sub>

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 you use @PrimaryKeyJoinColumn then id is connecting incorrectly. the identifier is connected to both the primary and the address table of company_id

here is a link from sleep mode http://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/OneToOne.html example 1

+5
source

You must 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

+1
source

Source: https://habr.com/ru/post/974014/


All Articles