79665727

Date: 2025-06-14 09:38:06
Score: 0.5
Natty:
Report link

It is an older question, and Spring JPA has evolved a lot since then, but I am putting this information here in case someone runs into this from the search engine.

The details are described in https://www.baeldung.com/jpa-mapping-single-entity-to-multiple-tables, but in short you can create a single entity specifying that we have columns in different tables using the @SecondaryTable annotation. However for my personal project that is not appropriate, although it may work for others. There is also multiple entities using the @PrimaryKeyJoinColumn annotation:

public abstract class LogRelation implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    @OneToOne
    @PrimaryKeyJoinColumn(name = "id")
    Log log;
// ...
}

Another small update ... under the current version of JPA, you are best to make the fields protected/private and access them only by Getter/Setter, which ensures the population of lazy fields.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @SecondaryTable
  • User mentioned (0): @PrimaryKeyJoinColumn
  • Low reputation (0.5):
Posted by: Jennifer