i'm a beginner to spring boot and hibernate, i'm doing my first app and i have a problem in designing relations in hibernate I have two users (Advertiser and Student) both have shared properties but differ in one, that advertiser belong to organization but student belong to a university (relation shown in database diagram below)
i have tried to do a user class, and but a reference to Advertiser and Student on it, with Featch-Type-LAZY , so i check the type of user, when it is advertiser, get advertiser object, otherwise student one code below - but i'm confused about is that the right way to implement different user type in hibernate? - is it a good design for code according to design-pattern principle ? - and if i want to make User class @Embaddable and make Advertiser and Student inherit it, so that i get rid of User table in database, then in spring security i have to make to access two table to get the user, will be that be better than before?
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false) @Enumerated(EnumType.STRING)
private AccountState state;
@Column(nullable = false) @Enumerated(EnumType.STRING)
private UserType type;
@Column(nullable = false)
private String firstName;
@Column(nullable = false)
private String lastName;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false) @Temporal(TemporalType.DATE)
private Date birthday;
@Column(nullable = false)
private String password;
@Transient
private String confirmPass;
@OneToOne(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Advertiser advertiser;
@OneToOne(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Student student;
// setters and getters
}
@Entity
public class Advertiser {
@EmbeddedId
private AdvertiserId advertiserId;
@OneToOne
@JoinColumn(name = "user_id", insertable = false, updatable = false)
private User user;
@ManyToOne
@JoinColumn(name = "organization_id", insertable = false, updatable = false)
private Organization organization;
// setter and getter
}
@Entity
public class Student{
@EmbeddedId
private StudentId studentId;
@OneToOne
@JoinColumn(name = "user_id", insertable = false, updatable = false)
private User user;
@ManyToOne
@JoinColumn(name = "university_id", insertable = false, updatable = false)
private University university;
// setter and getter
}
Aucun commentaire:
Enregistrer un commentaire