如何用硬编码值替换@JoinColumn? [英] How to replace a @JoinColumn with a hardcoded value?

查看:81
本文介绍了如何用硬编码值替换@JoinColumn?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关更多背景信息,请参阅我的另一个问题

For more context, please see my other question.

我想将 Employee 实体类加入代码实体类,但代码PK是复合的(DOMAIN,CODE),因为它列出了许多不同的代码域,但是Employee类/表中的代码都来自单个域,无需在Employee类/表中拥有域字段(因为它总是相同的)。

I want to join my Employee entity class to Code entity class but the Code PK is composite (DOMAIN, CODE) because it lists many different code domains, yet the codes that go in the Employee class/table are all from within a single domain, eliminating the need to have a domain field in the Employee class/table (because it would always be the same).

我可以加入员工使用 Employee 表中的CODE字段和硬编码值(例如 EMP_TYPE )而不是冗余列来代码

Can I join Employee to Code by using the CODE field in the Employee table and a hardcoded value (e.g. EMP_TYPE) instead of a redundant column?

如果我的 Employee 类/表确实有那个冗余列,我会加入它:

If my Employee class/table did indeed have that redundant column, I would join it like this:

@JoinColumns({
        @JoinColumn(name = "EMP_TYPE_CODE", referencedColumnName = "CODE"),
        @JoinColumn(name = "DOMAIN", referencedColumnName = "DOMAIN)})
    @ManyToOne(optional = false)
    private Code sharedStatute;

但我真的不希望在DB和类中有额外的列,因为它将永远是相同的。

but I REALLY don't want to have that extra column in the DB and in the class because it will always be the same.

我想要完成的将等同于SQL中的以下内容:

What I am trying to accomplish would be equivalent to the following in SQL:

SELECT e.emp_id, e.first_name, e.last_name, c.description as emp_type
FROM Employee e JOIN Code c
ON e.emp_type_code = c.code
WHERE c.domain = 'EMP_TYPE'

而非添加字段<$ c在Employee表中使用$ c> domain 并使用相同的值('EMP_TYPE')填充每个单个记录,然后执行:

as opposed to adding a field domain in the Employee table and populating EVERY SINGLE RECORD with the same value ('EMP_TYPE') and then doing:

SELECT e.emp_id, e.first_name, e.last_name, c.description as emp_type
FROM Employee e JOIN Code c
ON e.emp_type_code = c.code
AND e.domain = c.domain

前者效率更高,因为它使我不必有一个冗余的领域。所以我想做的是同样的事情,但在 JPA

The former is more efficient because it saves me from having to have a redundant field. So what I am trying to do is the same thing but in JPA.

你们中的一些人可能会说些什么为什么没有为每个代码单独的查找表的影响,但我认为这是一个可怕的想法,并导致数据库表和相应的应用程序实体混乱。通过代码类型(或域)划分单个代码查找表要好得多。

Some of you may say something to the effect of "why not have a separate lookup table for each code" but I think that's a terrible idea and causes cluttering of DB tables and corresponding application entities. It is much better to have a single code lookup table partitioned by code type (or domain).

推荐答案

这应该有效吗? / p>

This should work?

@JoinColumn(name = "EMP_TYPE_CODE", referencedColumnName = "CODE")
@Where(clause = "domain = 'EMP_TYPE'")
@ManyToOne(optional = false)
private Code sharedStatute;

这篇关于如何用硬编码值替换@JoinColumn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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