I have a super abstract class User:
package com.Abdelouadoud.MoroccoCraftsAPI.model.User;
import com.Abdelouadoud.MoroccoCraftsAPI.model.Adress;
import com.Abdelouadoud.MoroccoCraftsAPI.model.UserRole;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "users")
public abstract class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
private String email;
private String password;
@Enumerated(EnumType.STRING)
private UserRole userRole;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Adress> adressList = new ArrayList<>();
public User(User user) {
this.id = user.id;
this.firstName = user.firstName;
this.lastName = user.lastName;
this.email = user.email;
this.password = user.password;
this.userRole = user.userRole;
this.adressList = new ArrayList<>(user.adressList);
}
}
and a Customer class that inherits from it:
package com.Abdelouadoud.MoroccoCraftsAPI.model.User;
import com.Abdelouadoud.MoroccoCraftsAPI.model.Order;
import com.Abdelouadoud.MoroccoCraftsAPI.model.Product;
import com.Abdelouadoud.MoroccoCraftsAPI.model.Store;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "customers")
public class Customer extends User{
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Store> favoriteStores = new ArrayList<>();
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Product> favoriteProducts = new ArrayList<>();
@OneToMany //A list of orders contain many orders 1-* relation
private List<Order> orders = new ArrayList<>();
public Customer(User user) {
super(user);
}
}
and finally an other Seller class that inherits from Customer:
package com.Abdelouadoud.MoroccoCraftsAPI.model.User;
import com.Abdelouadoud.MoroccoCraftsAPI.model.Store;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "sellers")
public class Seller extends Customer{
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Store> storeList = new ArrayList<>();
}
the problem is that after executing the code, hibernate doesn't create foreign keys towards the super classes even though I used the annotation @Inheritance(strategy = InheritanceType.JOINED):
Hibernate: create table customers (id bigint not null, primary key (id)) engine=InnoDB
Hibernate: create table sellers (id bigint not null, primary key (id)) engine=InnoDB
I dropped the data base and recreated it, then reran the code but the problem persisted.
I need your help!
Hibernate will not create an additional column with foreign key. Each table will have an id column and all IDs will be equal for the same record. There will be a constraint generated for this reason. You should see it in the logs.