gpt4 book ai didi

Java Hibernate在插入用户名和密码时获取实体ID并将其删除

转载 作者:行者123 更新时间:2023-11-29 05:09:08 24 4
gpt4 key购买 nike

我想获取已插入实体的自动生成的 ID 到 MySQL,我有一个使用 javafx 和 hibernate 制作的应用程序,我不能删除实体,因为 ID 是自动生成的,我必须获取实体的 ID ,所以在我将它保存到数据库之后,然后写入用户名和密码,我希望它给我插入的用户名和密码的 ID。假设我已经插入了实体用户名:1​​23,密码:asd,它会自动生成 ID : 1. 现在,如果我在字段中写入用户名:1​​23,密码:asd 并单击删除按钮,它应该删除该实体..但它没有...所以我需要获取它的 ID 并按 ID 删除它我不知道怎么做。

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();

}
}

Java 类员工:

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;
}
}

最佳答案

如您所说,我会选择一个单独的按钮,而不是混合使用。

因此您的保存操作将是:

save.setOnAction(e -> {
m.setUser(username.getText());
m.setPassword(password.getText());

Session session = sf.openSession();
session.beginTransaction();
session.save(m);
session.getTransaction().commit();

您的删除操作将是:

delete.setOnAction(e -> {
Session session = sf.openSession();
Transaction tx;

try{
tx = session.beginTransaction();

Query query = session.createQuery("from Employee where user = :user and password = :password");
query.setString("user", username.getText());
query.setString("password", password.getText());

Employee userToDelete = (Employee)query.uniqueResult();

session.delete(userToDelete );
tx.commit();
}catch(Exception e){
tx.rollback();
}finally{
session.close();
}

关于Java Hibernate在插入用户名和密码时获取实体ID并将其删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42622589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com