i got a quick question. I am trying to make data as following: Auth(email,pass) -> Client(name, surname, ...) Auth(email,pass) -> RepairShop(nip, location, ...)
Auth
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Auth {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long authid;
Client
@Entity
public class Client extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idClient;
...
@ManyToOne
private RepairShop repairShop;
RepairShop
@Entity
public class RepairShop extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idRepairShop;
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "repairShop")
private Set<Client> clients;
With repositories looking like AuthRepository
public interface AuthRepository extends JpaRepository<Long, Auth>{
}
ClientRepository
public interface ClientRepository extends JpaRepository<Client,Long> {
}
RepairShopRepository
public interface RepairShopRepository extends JpaRepository<RepairShop, Long> {
}
Auth cannot be abstract class, this is only table to have good auth in my project (currently i have just manually added table with some triggers to write data from Client and RepairShop to Auth, but i am looking for better solution)
My goal is to have database like Auth idauth email pass role
Client idclient name surname idauth
RepairShop idrepairShop nip location idauth
Is it even possible to do it like follows? Or it is just bad idea and i should just use onetoone relationship and dont even play like this. Or maybe there is some better solution in database structure. Also it is important to let it easily work with Angular App to easily have access from logging with data from auth to managing other properties from Client/RepairShop tables
I think problem here lies in my repository config but im not sure.
What do you think about it?
CodePudding user response:
If you look at the client->auth and repairship -> auth relationship from OOP perspective it is a has-a relationship rather than is-a relatioship. So, onetoone is the best way to map it.
@Entity
public class Client extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idClient;
...
@ManyToOne
private RepairShop repairShop;
@OneToOne
@JoinColumn(name="idauth")
private Auth auth;
repair shop
@Entity
public class RepairShop extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idRepairShop;
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "repairShop")
private Set<Client> clients;
@OneToOne
@JoinColumn(name="idauth")
private Auth auth;