0

This is my person class.

@Entity
@Table(name = "Person")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "PersonID")
    private Long personID;
    @Column(name = "Name")
    @Convert(converter = NameConverter.class)
    private Name name;
    @Column(name = "Geburtsdatum")
    private LocalDate geburtsdatum;
    @Column(name = "Strasse")
    private String strasse;
    @Column(name = "Hausnummer")
    private String hausnummer;
    @Column(name = "PLZ")
    private String plz;
    @Column(name = "Ort")
    private String ort;
    @Column(name = "EMail")
    private String email;
    @Column(name = "Telefonnummer")
    private String telefonnummer;

    protected Person() {}

    public Person(Name name, LocalDate geburtsdatum, String strasse, String hausnummer, String plz, String ort, String email, String telefonnummer) {
        this.name = name;
        this.geburtsdatum = geburtsdatum;
        this.strasse = strasse;
        this.hausnummer = hausnummer;
        this.plz = plz;
        this.ort = ort;
        this.email = email;
        this.telefonnummer = telefonnummer;
    }

    @Override
    public String toString() {
        return String.format("PersonID: %d\nName: %s\nGeburtsdatum: %s\nE-Mail: %s\nTelefonnummer: %s\nAdresse: %s %s, %s %s\n",personID,name.getFullName(),geburtsdatum,email,telefonnummer,strasse,hausnummer,plz,ort);
    }

    /* Getter & Setters */
}

This is my Mitglied class which should be based of the person.

@Entity
@Table(name = "Mitglied")
public class Mitglied extends Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "MitgliedID")
    private Long mitgliedID;
    @Column(name = "ZPS")
    private String zps;
    @Column(name = "ZPSNummer")
    private String zpsNummer;
    @Column(name = "Eintrittsdatum")
    private LocalDate eintrittsDatum;
    @Column(name = "Austrittsdatum")
    private LocalDate austrittsDatum;
    @Column(name = "DWZ")
    private String dwz;
    @Column(name = "Elo")
    private String elo;
    @Column(name = "FIDETitel")
    private String fideTitel;

    protected Mitglied() {}

    public Mitglied(Name name, LocalDate geburtsdatum, String strasse, String hausnummer, String plz, String ort, String email, String telefonnummer, String zps, LocalDate eintrittsDatum, LocalDate austrittsDatum, String dwz, String elo, String fideTitel) {
        super(name, geburtsdatum, strasse, hausnummer, plz, ort, email, telefonnummer);
        this.zps = zps;
        this.eintrittsDatum = eintrittsDatum;
        this.austrittsDatum = austrittsDatum;
        this.dwz = dwz;
        this.elo = elo;
        this.fideTitel = fideTitel;
    }
    
    /* Getter & Setters */

}

I'm having the problem that if the Mitglied inherits from the Person than i got 2 unique IDs... My guess would be to fix my problem i could change the Person class so it would not be a table anymore, but i don't really know how i would make my Mitglied Table have a column for each value.

itfedorovsa
  • 126
  • 1
  • 3
  • 12
salteax1
  • 67
  • 1
  • 7
  • 1
    This is more likely a database design problem and not a JPA or Spring (or even Java for that matter) problem. My suggestion is you give IDs to entities and if one entity is inheriting other, then look into compound primary keys. – justanotherguy Jun 03 '22 at 06:18

1 Answers1

1

There shouldn't be multiple @Id columns in the entity hierarchy. It's only logical because essentially they describe the same object, although with some differences.

If your intention was to have mitgliedID value autogenerated one solution would be to use Hibernate's @Generated/@ValueGenerationType annotations (https://docs.jboss.org/hibernate/orm/6.0/topical/html_single/generated/GeneratedValues.html#generated-values-guide).

Also, check out this question for other ways to achieve the same result: Hibernate JPA Sequence (non-Id)

dekkard
  • 6,121
  • 1
  • 16
  • 26
  • Yea, my solution is that i got a personID and a mitgliedID and the mitglied class gets the personID – salteax1 Jun 03 '22 at 09:09