我正在从事一个javafx+hibernate项目,其中我有一个公司实体,它有一个用户列表。我有一个类naturalperson,它扩展了抽象类user。我在javafx应用程序上创建了一个注册窗口,当我尝试注册一个自然人时,我得到:
`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`))
我对冬眠还很陌生,不知道怎么解决这个问题。有人能帮忙吗?
用户.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;
}
自然人.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;
}
公司.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();
}
1条答案
按热度按时间ogsagwnx1#
你在注册时检查过吗
company = companyHibernateControl.findCompany(company.getCompanyName());
返回非空值?