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
source share