JPA hibernate OneToOne mapping

Posted by Stupidfrog on Stack Overflow See other posts from Stack Overflow or by Stupidfrog
Published on 2014-08-19T02:21:38Z Indexed on 2014/08/19 4:20 UTC
Read the original article Hit count: 195

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

there is 2 table

public A
{
  private Long id;
  private Long name;
}
public B
{
  private Long id;
  private Long table_a_id;
}

the A.id and B.id is sequential, unique , but no related.(means they are separately id for their own table).

how to do mapping? i have tried some method, however the result is not i wanted, because it bind wrong. for example:

public A
{
....
  @OneToOne
  @JoinColumn(name = "id")
  private B table_b
}

public B
{
...
  @JsonIgnore
  @OneToOne(mappedBy = "table_b")
  private A table_a;
}

when i query A the result is

{
"id":5,
"table_b":{
    "id":5,
    "table_a_id":4
    }
}

obviously the data join by using their id but not joining using table_a_id.

what i expect is

{
"id":4,
"table_b":{
    "id":5,
    "table_a_id":4
    }
}

so can somebody teach me that, how to map this 2 table by using table b table_a_id(foregin key)

© Stack Overflow or respective owner

Related posts about spring

Related posts about hibernate