0

I have the following DB model (all fields are not nullable):

File            SourceTypes
-----------     -----------
country PK      country PK
filename PK     sourcetype PK
sourcetype      sourcename

The sourcetype field in File references the SourceTypes table, however both tables have a composite PK, so the foreign key is also composite.

Source Type classes:

@Embeddable
class SoutceTypePk {
    String country
    String sourcetype
}

@Entity
class SourceType {
    @Id
    SourceTypePk id;
    
    String sourcename;
}

File classes:

@Embeddable
class FilePk {
    country
    filename
}

@Entity
class File {
    @Id
    FilePk id;

    @MapsId("country")
    @ManyToOne
    @JoinColumn(name="country", referencedColumn="country")
    @JoinColumn(name="sourcetype", referencedColumn="sourcetype")
    SourceType sourceType;
}

With this configuration, JPA excludes the sourcetype field from insert queries to the File table.

If I use MapsId without specifying the "country" field, I get errors that JPA expected a "sourcetype" column in the FilePK class.

If I take away the MapsId completely then I get "country" field is duplicated

How do I represent this scenario in my code with JPA annotations?

---- Update

The scenario here is that both tables have a composite PK, but the FK that exists in the File table only shares 1 field (country) with the PK of the sourcetype table.

The source type table is a catalog that contains country specific values, the file table contains country specific files that must meet a certain sourcetype for their country.

This model exists as described in our PostgreSQL database already and is working:

alter table sourcetype add primary key (country_cod, source_type_cod)
alter table file add primary key (country_cod, file_nam);
alter table file add foreign key (country_cod, source_type_cod) references sourcetype(country_cod, source_type_cod)

Our problem has been implementing that FK in JPA.

4
  • What scenario? A FK is a column list whose values appear elsewhere as PK/UNIQUE. You don't have that. Explain what you are trying to do & why without abusing the term FK. Commented Jun 18, 2024 at 10:05
  • Please ask 1 specific researched non-duplicate question. Either ask re 1 bad query/function with obligatory minimal reproducible example, including why you think it should return something else or are unsure at the 1st subexpression where you don't get what you expect or are stuck, justified by reference to authoritative documentation, or ask about your overall goal giving working parts you can do with justification & a minimal reproducible example--then misunderstood code doesn't belong. But please ask about unexpected behaviour 1st because misconceptions get in the way of your goal. How to Ask Help center Basic questions are faqs. Commented Jun 18, 2024 at 10:07
  • Clearly, when clear this will be a faq. Please before considering posting: Pin down code issues via minimal reproducible example. Read manuals/references & google error messages & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. SO/SE search is unusual, read the help. Google re googling/searching, including Q&A at Meta Stack Overflow & Meta Stack Exchange. How much research effort is expected of SO users? How to Ask Help center Reflect research in posts. Commented Jun 18, 2024 at 10:09
  • Please don't insert "EDIT"s/"UPDATE"s, just make your post the best presentation as of edit time. Commented Jun 19, 2024 at 5:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.