休眠。通过id进行外键映射 [英] Hibernate. Foreign key mapping by id

查看:198
本文介绍了休眠。通过id进行外键映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Acount和Record之间创建@ManyToOne映射。一个帐户可以有很多记录。但是我不想在Record类中添加Account域,反之亦然。你可以帮我在注释中描述这个吗?

I want to create @ManyToOne mapping between Acount and Record. One account can have a lot of records. But i don't want to add Account field in Record class or vice versa. Could you please help me to describe this in annotations?

@Entity
public class Account {

  @Id
  ... getId();
}

@Entity
public class Record {

  @Id
  ... getId();

  @?????
  ... getAccountId();

}


推荐答案

如果你不想在Account中使用Account类,你可以简单地将accountId定义为long(或int,无论哪个都可以),除非你需要一个不同的列名。
但我建议不要这样做。

Mapping entities to tables is the way Hibernate usually works, if you don't want the Account class in Record you can simply define accountId as long (or int, whichever is ok) and not annotate it unless you need a different column name. But I would suggest not to do it.

这篇关于休眠。通过id进行外键映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆