休眠:如何声明外键而不必在域对象中编写关联方法? [英] Hibernate: How declare foreign key without having to code association methods in the domain object?

查看:38
本文介绍了休眠:如何声明外键而不必在域对象中编写关联方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个相关的表,MANY和ONE. MANY表上的整数ONE_ID列将多对一关系形式化.

I have two related tables, MANY and ONE. An integer ONE_ID column on the MANY table formalises a many-to-one relationship.

我想映射到多个"域对象,并让Hibernate生成DMAN,其中包括对MANY.ONE_ID的外键约束.但是我想在Many域对象上使用一个简单的属性getter和setter对("int getOneId()/void setOneId(int)"),而不是Hibernate通常希望我提供的关联方法("One getOne()/void setOne(One)).这可能吗?

I want to map to Many and One domain objects and to have Hibernate generate the DDL including a foreign key constraint on MANY.ONE_ID. But I want a simple property getter and setter pair ("int getOneId() / void setOneId(int)") on the Many domain object rather than the association methods that Hibernate would ordinarily expect me to provide ("One getOne() / void setOne(One)"). Is this possible?

我将尝试解释为什么要这样做:实际上,我有数百个这样的一对多关联,我需要FK约束,但是我永远不需要导航.休眠映射文件和域对象将通过代码生成来创建.我在运行时需要做的就是能够从许多"侧对象读取FK属性并进行设置.我永远不需要真正地将许多导航到一个关联.由于代码生成问题,如果我可以为FK字段使用基于属性的简单访问器,它将大大简化事情.

I'll try and explain why I want this: I actually have hundreds of such many to one associations for which I want the FK constraint but that I'll never need to navigate. The hibernate mapping file and the domain objects are to be created by code generation. All I need to do at runtime is to be able to read the FK property from the 'many' side object and to set it; I never need to actually navigate the many to one association. Because of the code generation issues it would greatly simplify things if I could have simple property based accessors for the FK field.

推荐答案

AFAIK,您有两个选择:

AFAIK, you have two options:

  • 正常声明映射,并使关联变得懒惰.这样,您就不用为不用的东西付费.您还需要仔细选择协会的所有者.另外,您可以指定Long属性并将其映射到FK.
  • 不要依赖休眠来生成ddl/schema.映射表,就像您原来想要的那样(没有关联,只有长类型).

这篇关于休眠:如何声明外键而不必在域对象中编写关联方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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