开发者

How to mapping foreign key as just ID (a long) rather than the entity with Hibernate annotations

开发者 https://www.devze.com 2022-12-08 05:46 出处:网络
I would like an entit开发者_运维百科y \"A\" to map only to the primary key over a foriegn key.

I would like an entit开发者_运维百科y "A" to map only to the primary key over a foriegn key.

class A {
   long id;
   long bId;
}

class B {
   long bId;
   ...
}

How do i do this ?


From your post it's not eaxctly clear what you want, so I'm going with what I think it is you want...

@Entity
class A {
   @Id
   long id;
   @OneTo???
   B bId;
}


@Entity
class B {
   @Id
   long bId;
   ...
}

You will need to specify the relationship between A and B and annotate the link as such. Hibernate will than link the two by generating a FK link to B's indentifier. Your tables will look something like this:

  |---------|     
  | A       |       |---------|
  |---------|       | B       |
PK|long id  |       |---------|
FK|long bId | --> PK|long bId |
  |---------|       |---------|

You could look into annotating bId with One-to-One, or One-to-Many or any of the other mappings, depending on what's applicable in you case.

0

精彩评论

暂无评论...
验证码 换一张
取 消