F
F
Fedor unknown2022-04-15 06:00:58
Java
Fedor unknown, 2022-04-15 06:00:58

Can one table in Hibernate inherit the ID of a second table?

I have the entities described below (PartnerInformation, Activity, Address), how can I make the Activity and Address entities inherit id from PartnerInformation?

@Data
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonNaming(PropertyNamingStrategies.UpperSnakeCaseStrategy.class)
public class PartnerInformation {

    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(
            name="UUID",
            strategy = "org.hibernate.id.UUIDGenerator"
    )
    @JsonIgnore
    private UUID id;

    @JsonProperty("ID")
    public  String externalId;
    private boolean isActive = true;

    @OneToOne(cascade=CascadeType.ALL)
    private Activity activity;

    @OneToMany(cascade=CascadeType.ALL)
    private List<Address> address;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2022-04-15
Hasanly @azerphoenix

Good afternoon.
So maybe you do not need a OneToOne connection, but you need @Embeddableit for an Activity and instead of a OneToMany connection for a List, use @ElementCollectionor @CollectionOfElementsfor List<Address>
https://stackoverflow.com/questions/8969059/differ...
https://www.baeldung.com/jpa- embedded-embeddable
If you want to embed an ID, you can probably look at @EmbeddedId https://www.baeldung.com/spring-jpa-embedded-metho...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question