Hibernate - 外键而不是实体 [英] Hibernate - Foreign keys instead of Entities

查看:23
本文介绍了Hibernate - 外键而不是实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,Hibernate 允许我直接使用

Currently, Hibernate allows me to load objects defined by *-to-one relationships directly with

entity1.getEntity2()

是否可以获取外键而不是对象?

Is it possible to get the foreign key instead of the object?

我看到的当前方法是添加到我的映射中:

The current approach which I see is having addint to my mapping:

@JoinColumn(name="message_key")
@ManyToOne(targetEntity=Message.class,fetch=FetchType.LAZY)
private Message message;  //these lines currently exist

@Column(name="message_key")
private Long message_fk; //the idea is to add those 2 lines

有没有更好的方法来获取外键,还是只有这个?

Is there a better approach to get the foreign key, or is this the only one?

推荐答案

是的,您可以这样做.您只需要为 hibernate 明确哪个是它应该维护的映射,如下所示:

Yes, you can do that. You just need to make it clear for hibernate which one is the mapping that it's supposed to maintain, like so:

@Column(name="message_key", updatable=false, insertable=false)
private Long message_fk;

这篇关于Hibernate - 外键而不是实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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