title: Java Hibernate get id of entity when insert username and password and delete it
body: <p>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 .Let's say I already inserted the entity USERNAME : 123 , password : asd , and it get autogenerated the ID : 1. Now If i write in the fields username : 123 , password : asd and click delete button it should delete that entity..but it doesen't...so i need to get the ID of it and delete it by ID and I don't know how.</p>
  
  <pre><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 -&gt; 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 -&gt; {
                         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();
  
     }
  }               
  </code></pre>
  
  <p>And the java class employee : </p>
  
  <pre><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;
      }
      }
  </code></pre>