gpt4 book ai didi

java - Hibernate - 如何检查用户是否存在?

转载 作者:行者123 更新时间:2023-12-02 03:57:24 25 4
gpt4 key购买 nike

我遇到了奇怪的问题,我正在尝试在 JavaFX 应用程序中使用 Hibernate,并且我希望 Hibernate 将检查数据库中是否存在输入的值,但是...当我输入这些值时,Hibernate 会删除其中的所有记录我的 USER 表而不是检查输入的值是否存在...为什么?这是一个代码:

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("clinic");
EntityManager entityManager = entityManagerFactory.createEntityManager();

User user = new User();

user.setUsername(loginField.getText());
user.setPassword(passwordField.getText());


Query query = entityManager.createQuery("FROM User WHERE username= :username AND password= :password", User.class);
query.setParameter("username", user.getUsername());
query.setParameter("password", user.getPassword());

String result = query.getResultList().toString();

System.out.println(result);

// if user exist then main window will appear:
//root = FXMLLoader.load(getClass().getResource("/view/Main.fxml"));
//stage = (Stage) logInButton.getScene().getWindow();
//stage.setTitle("Przychodnia");
//stage.setScene(new Scene(root, 750, 530));
//stage.show();
//return;


entityManager.close();
entityManagerFactory.close();

持久性.xml:

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="clinic" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/przychodniadb" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>

最佳答案

摆脱<property name="hibernate.hbm2ddl.auto" value="create"/>因为它会在每次启动时重新创建您的数据库

关于java - Hibernate - 如何检查用户是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35305704/

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