I want to get the auto-generated ID of already inserted entity to MySQL , I have an application made in javafx with hibernate , and I can't delete entity because the ID is autogenerated and I have to get the id of entity , so after I save it to database , then write the username and password I want it to give me the Id of inserted username and password I write in the fields. So..I write in fields the username and password and then click a delete button and it should return me the id of the pas and username inserted and then delete the entity from database.
Or..how can I delete an entity by auto generated ID? Because if I just try the normal way it gives me this in the console and doesen't delete the entity so I want to delete it by that auto generated ID:
Mar 06, 2017 12:10:23 PM org.hibernate.event.internal.DefaultDeleteEventListener deleteTransientEntity
INFO: HHH000114: Handling transient entity in delete processing
This is the main class :
Code:
public class TestMain extends Application {
static Employee m = new Employee();
public static void main(String[] args) { launch(args); }
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Stage window = primaryStage;
window.setTitle("WELCOME");
Label l1 = new Label();
// l1.setFont(new Font(48));
l1.setText("SMS");
l1.setTranslateY(-180);
Button closeBut = new Button("Exit.");
closeBut.setTooltip(new Tooltip("Click to exit application"));
closeBut.setOnAction(e -> window.close());
Button save = new Button ("Save");
save.setTooltip(new Tooltip("Click to save to database"));
Label l2 = new Label("Username ");
TextField username = new TextField();
username.setTooltip(new Tooltip("Enter your new username"));
username.setPromptText("username");
GridPane.setConstraints(l2,0,0);
GridPane.setConstraints(username,1,0);
Label l3 = new Label("Password ");
PasswordField password = new PasswordField();
password.setTooltip(new Tooltip("Enter your new password"));
password.setPromptText("password");
GridPane.setConstraints(l3,0,1);
GridPane.setConstraints(password,1,1);
Label l4 = new Label();
GridPane.setConstraints(l4,0,4);
GridPane.setConstraints(save,0,2);
GridPane.setConstraints(closeBut,1,2);
//Configureation Hibernate
// Configuration config = new Configuration();
//config.configure("hibernate.cfg.xml");
SessionFactory sf = new Configuration().configure().buildSessionFactory();
//
save.setOnAction(e -> {
m.setUser(username.getText());
m.setPassword(password.getText());
Session session = sf.openSession();
session.beginTransaction();
session.delete(m);
session.getTransaction().commit();
});
GridPane grid = new GridPane();
grid.setPadding(new Insets(10,10,10,10));
grid.setVgap(8);
grid.setHgap(0);
grid.getChildren().addAll(l1,l2,username,l3,password,save,l4,closeBut);
grid.setAlignment(Pos.CENTER);
Scene scene = new Scene(grid,300,400);
window.setScene(scene);
window.show();
}
}
And this is the Employee class :
Code:
package mypackage;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.*;
import javax.persistence.Id;
@Entity(name="EMPLOYEE")
public class Employee {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private int Id;
@Column(name="USERNAME")
private String user;
@Column(name="PASSWORD")
private String password;
public int getId() {
return Id;
}
public void setId(int Id) {
this.Id = Id;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}