0

I've been trying to set up inheritance in my Spring boot project, but I failed to do so. I've tried using the superclass mappings, joined table, single table but still I think I'm missing something. Here is how the classes look like:

Person class:

@Entity
@MappedSuperclass
@Table(name = "person")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_person", nullable = false)
    private Integer id;

    @Column(name = "first_name", nullable = false, length = 200)
    private String firstName;

    @Column(name = "last_name", nullable = false, length = 200)
    private String lastName;

    @Column(name = "PESEL", nullable = false)
    private Integer pesel;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "person")
    private Driver driver;

//setters and getters below
}

Driver class:

@Entity
@Table(name = "driver")
public class Driver {
    @Id
    @Column(name = "id_driver", nullable = false)
    private Integer id;
    @MapsId
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_driver")
    private Person person;

    @Column(name = "birth_date", nullable = false)
    private LocalDate birthDate;

    @Column(name = "license", nullable = false, length = 200)
    private String license;

    @OneToMany(mappedBy = "idDriver")
    private Set<Ride> rides = new LinkedHashSet<>();
//setters and getters below
}

Here's how it is joined in the database (mysql):

Pic of the db tables

I would be very thankful if you could at least point me in the right direction (like which inheritance type will suit this simple case the best)

1 Answer 1

2

First remove the @Entity and @Table annotations, as well as the relationship, from your superclass:

@MappedSuperclass
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_person", nullable = false)
    private Integer id;

    @Column(name = "first_name", nullable = false, length = 200)
    private String firstName;

    @Column(name = "last_name", nullable = false, length = 200)
    private String lastName;

    @Column(name = "PESEL", nullable = false)
    private Integer pesel;

//setters and getters below
}

Secondly, remove the duplicated id column and remove the relationship between the mapped superclass and your derived classes, and actually extend the superclass:

@Entity
@Table(name = "driver")
public class Driver extends Person{

    @Column(name = "birth_date", nullable = false)
    private LocalDate birthDate;

    @Column(name = "license", nullable = false, length = 200)
    private String license;

    @OneToMany(mappedBy = "idDriver")
    private Set<Ride> rides = new LinkedHashSet<>();
//setters and getters below
}
Sign up to request clarification or add additional context in comments.

Comments

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.