V
V
vitya_brodov2022-02-25 10:38:05
PostgreSQL
vitya_brodov, 2022-02-25 10:38:05

How to make a common primary key for multiple tables in Hibernate?

Hello!
How can I make a common primary key for multiple tables in Hibernate?
for example I have Entity classes:

@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(schema = "info")
public class BaseEntity {
    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(
            name="UUID",
            strategy = "org.hibernate.id.UUIDGenerator"
    )
    @JsonIgnore
    private UUID id;

    @OneToOne(mappedBy = "information")
    private UserDto userDto;

    @OneToOne(mappedBy = "information")
    private Address address;
}

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UserDto{

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

    @Column(name = "users_name")
    private String name;
    @Column(name = "users_inn")
    private String inn;
    @Column(name = "users_phone")
    private String phone;
    @Column(name = "users_brand")
    private String brand;
    @Enumerated(EnumType.STRING)
    @Column(name = "users_type")
    private UserTypes userType;
}


@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Address {

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

    @OneToMany(mappedBy = "address")
    private List<Depart> departs;
}


@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Depart{
    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(
            name="UUID",
            strategy = "org.hibernate.id.UUIDGenerator"
    )
    @JsonIgnore
    private UUID id;

    @JsonAlias(value = "CODE")
    private String code;
    @JsonAlias(value = "NAME")
    private String name;
}


PS Thanks in advance!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question