Sige VV
Sige VV

Reputation: 68

Many to one relation with inherited objects - Postgres

I'm building an application in which I need a single login page. However, there are two 'sorts' of users: Clients and Trainers. My domain looks like this:

      User
        |
  +-----+-----+
  |           |
Person     Trainer

My user will contain email, password etc. so I can use this table to verify my login requests.

A Trainer should store a list of persons, and a person should have 1 trainer.

The one to many side works, the trainer can store a list of Person class objects, but I can't link back to a trainer via a client.

This is my User class:

package com.example.demo.model;

import javax.persistence.*;

@Entity
@Inheritance
@Table(name="users")
public abstract class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String email;

    public User(){}

    public User(String email) {
        this.email = email;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

The Person class:

package com.example.demo.model;

import javax.persistence.*;

@Entity
public class Person extends User {

    @Column
    private int age;

    @ManyToOne
    @JoinColumn
    private Trainer trainer;

    public Person(){}

    public Person(String email, int age) {
       super(email);
        this.age = age;
    }
}

The Trainer class:

package com.example.demo.model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Trainer extends User {

    @Column
    private String description;

    @OneToMany(mappedBy = "trainer", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Person> clients;

    public Trainer(){}

    public Trainer(String email, String description) {
        super(email);
        this.description = description;
    }

    public void addClient(Person person){
        this.clients = new ArrayList<>();
        this.clients.add(person);
    }
}

How should I access a client's trainer via the ManyToOne annotation? Or are there other options? Thanks in advance!

Upvotes: 1

Views: 56

Answers (1)

Peter Š&#225;ly
Peter Š&#225;ly

Reputation: 2933

Dont initialize list in addClient(). You are overwriting/deleting existing clients. Do it in field declaration.

@OneToMany(...)
private List<Person> clients = new ArrayList<>();

Set both sides of relation:

public void addClient(Person person){
    person.setTrainer(this);
    this.clients.add(person);
}

Upvotes: 1

Related Questions