我正在创建一个简单的CRUD JavaFX桌面应用程序,但它无法保存任何记录,也不会抛出错误或任何日志。
使用Hibernate 4.3.8,Netbeans 8.0.2,Java 8.
在Netbeans中生成POJO 参见代码片段
的的HibernateUtil
private static EntityManager entityManager;
public static EntityManager getEntityManager() {
if (entityManager == null) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence");
entityManager = emf.createEntityManager();
}
return entityManager;
}
UserService
@Service
@Configurable
public class UsersService implements IServiceInterface<Users> {
@Override
public Users save(Users entity) {
return usersDAO.save(entity);
}
UserController中
@Controller
public class UserCtrl implements Initializable {
private final IServiceInterface userService = new UsersService();
public void addUser(Users user) {
userService.save(user);
}
}
ViewUsers
public class ViewUsers {
@FXML
private TextField username;
@FXML
private TextField password;
public void addUser(ActionEvent event) {
Users user = new Users();
user.setUserName(username.getText());
user.setUserPass(password.getText());
userCtrl.addUser(user);
}
}
View.fxml
<GridPane fx:controller="com.users.view.users"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10"
styleClass="root">
<TextField fx:id="username" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<PasswordField fx:id="password" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<Button text="Sign In" onAction="#addUser"/>
</GridPane>
更新我正在使用GenericDao
final EntityManager em = HibernateUtil.getEntityManager();
@Override
public T save(T entity) {
final T savedEntity = em.merge(entity);
return savedEntity;
}