JPA Hibernate onetoone mapping with foreign key only -


enviroment: hibernate 4.1.6.final spring 3.1.2.release spring jpa 1.1.0.release postgresql 9.1-901-1.jdbc4

i decided rephrase questions.

there 2 table

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 sequential , no related. except table.address.company_id foreign key of company.

how mapping? result expected is:

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

so can teach me that, how map 2 table

what want one-to-one mapping between company , address

just add @onetoone annotation table_address field of company class:

 public class address {          @id         @generatedvalue         private long id;         private string address;         @onetoone         @primarykeyjoincolumn         private company company;          //getters , setters     }  public class company {      @id     @generatedvalue     private long id;     private string name;     @onetoone(mappedby = "company",cascade = cascadetype.all)     private address companyaddress;      //getters , setters } 

apart problem: respect java naming convention, in case class name should start capital letter , next word in variable name too. i.e. company should company , address should address, private address table_address; change 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 , setters }  public class company {      @id     @generatedvalue     private long id;     private string name;     @onetoone(mappedby = "company",cascade = cascadetype.all)     private address companyaddress;      //getters , setters } 

from stupidfrog: if u using @primarykeyjoincolumn, id join wrong. id join both primary not address table company_id

here reference hibernate http://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/onetoone.html example 1


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -