gpt4 book ai didi

java - JPA 新手 - 可以写入,但无法从数据库读取

转载 作者:行者123 更新时间:2023-11-30 04:00:16 24 4
gpt4 key购买 nike

我正在努力学习JPA。如果我执行写入操作,然后在调用 close 之前立即读取,则它可以工作。但如果我尝试自己阅读,却不会。这是我在用户中的代码:

@Entity
@Table(name = "JPA_Users")
public class User {

@Id @GeneratedValue
@Column(name = "user_id")
private int id;

@Column(name = "username")
private String username;

@Column(name = "password")
private String password;

public User(){}

public User(String username, String password){
this.username = username;
this.password = password;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString(){
return Integer.toString(id) + ":" + username + ":" + password;
}
}

这是 JPA 代码:

public void write(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");

EntityManager em = emf.createEntityManager();
EntityTransaction et = em.getTransaction();
et.begin();

User user = new User("Chris", "123456");
User user2 = new User("Dave", "password");
User user3 = new User("Peter", "asfdfsdf");
em.persist(user);
em.persist(user2);
em.persist(user3);
em.flush();

List users = em.createQuery("SELECT u FROM User u").getResultList();
System.out.println(users.size() + " user(s) found");
for(Object o : users){
User u = (User) o;
System.out.println(u.getId());
System.out.println(u.getUsername());
System.out.println(u.getPassword());
System.out.println("--------------------------");
}

et.commit();
em.close();
emf.close();
}

以上代码有效,并打印出用户详细信息。但如果我单独运行下面的方法,它就不起作用。它打印出“找到 0 个用户”。我已在 SQL Developer 中检查该表是否存在并已填充,确实如此。

public void read(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");
EntityManager em = emf.createEntityManager();
List users = em.createQuery("SELECT u FROM User u").getResultList();
System.out.println(users.size() + " user(s) found");
for(Object o : users){
User u = (User) o;
System.out.println(u.getId());
System.out.println(u.getUsername());
System.out.println(u.getPassword());
System.out.println("--------------------------");
}
em.close();
emf.close();
}

这本身也不起作用:

public void read(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");
EntityManager em = emf.createEntityManager();
User u = em.find(User.class, 1);
System.out.println(u);
em.close();
emf.close();
}

最后,这是 persistence.xml。我不明白下面的大部分内容,所以我猜问题与这里的内容有关。谢谢,我感谢任何帮助。

<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_1_0.xsd"
version="1.0">
<persistence-unit name="helloworld">
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm" />

<!-- SQL stdout logging -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="use_sql_comments" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.connection.url"
value="jdbc:My_Database_location" />
<property name="hibernate.connection.username" value="My_Username" />
<property name="hibernate.connection.password" value="My_Password"/>

<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.c3p0.min_size" value="2" />
<property name="hibernate.c3p0.max_size" value="30" />
<property name="hibernate.c3p0.timeout" value="300" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="3000" />
</properties>
</persistence-unit>

最佳答案

 User u = em.find(User.class, 1);

并且您 100% 确定您的用户表中确实存在 ID =1 的实体,对吧?...

关于java - JPA 新手 - 可以写入,但无法从数据库读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22146618/

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