For more context, please see my other question.
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).
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?
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;
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.
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'
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
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).