I'm working on a javafx+hibernate project where I have a Company entity which has a list of users. I have a class NaturalPerson which extends abstract class User. I made a registration window on my javafx app and when I'm trying to register a NaturalPerson I get:
`java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails.`
(`companyhibernate`.`naturalperson`, CONSTRAINT `FK_s5c7plmyohwfq5biwepwg8dqa` FOREIGN KEY (`id`) REFERENCES `company` (`companyName`))
I'm very new to hibernate and can't figure out how to fix this. Could someone help?
User.java:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class User implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
protected String name;
protected String loginName;
protected String password;
@ManyToOne
private Company company;
public User() {
}
public User(String name, String loginName, String password, Company company) {
this.name = name;
this.loginName = loginName;
this.password = password;
this.company = company;
}
NaturalPerson.java
@Entity
public class NaturalPerson extends User implements Serializable {
private String surname;
private String phoneNumber;
private String email;
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE},
mappedBy = "responsibleUsers")
@OrderBy("id ASC")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Category> responsibleForCategories;
@ManyToOne
private Company company;
public NaturalPerson() {
}
public NaturalPerson(String name, String loginName, String password, String surname, String phoneNumber, String email, List<Category> responsibleForCategories, Company company) {
super(name, loginName, password, company);
this.surname = surname;
this.phoneNumber = phoneNumber;
this.email = email;
this.responsibleForCategories = responsibleForCategories;
}
Company.java
@Entity
public class Company implements Serializable {
@Id
private String companyName;
private String compEmail;
private String compPhoneNum;
@OneToMany(mappedBy = "company", cascade= CascadeType.ALL, orphanRemoval=true)
@OrderBy("id ASC")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Category> allCategories;
@OneToMany(mappedBy = "company", cascade= CascadeType.ALL, orphanRemoval=true)
@OrderBy("id ASC")
@LazyCollection(LazyCollectionOption.FALSE)
private List<User> allUsers;
public Company() {
}
public Company(String companyName, String compEmail, String compPhoneNum, ArrayList<Category> allCategories, ArrayList<User> allUsers) {
this.companyName = companyName;
this.compEmail = compEmail;
this.compPhoneNum = compPhoneNum;
this.allCategories = allCategories;
this.allUsers = allUsers;
}
NaturalPersonRegistration.java
public class NaturalPersonRegistration implements Initializable {
@FXML
public TextField nameField;
@FXML
public TextField surnameField;
@FXML
public TextField phoneNumField;
@FXML
public TextField emailField;
@FXML
public TextField usernameField;
@FXML
public PasswordField passwordField;
@FXML
public Button signUpButton;
private Company company;
EntityManagerFactory factory = Persistence.createEntityManagerFactory("CompanyHibernate");
CompanyHibernateControl companyHibernateControl = new CompanyHibernateControl(factory);
UserHibernateControl userHibernateControl = new UserHibernateControl(factory);
public void setCompany(Company company) {
this.company = company;
}
public void createUser(ActionEvent actionEvent) throws Exception {
company = companyHibernateControl.findCompany(company.getCompanyName());
List<Category> responsibleForCategories = new ArrayList<Category>();
User user = new NaturalPerson(nameField.getText(),
usernameField.getText(),
passwordField.getText(),
surnameField.getText(),
phoneNumField.getText(),
emailField.getText(),
responsibleForCategories,
company
);
userHibernateControl.create(user);
loadMainWindow();
}
Have you checked during registration that
company = companyHibernateControl.findCompany(company.getCompanyName());
returned non-empty value?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.