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

时间:2014-03-03 12:19:45

标签: java hibernate jpa

我正在努力学习JPA。如果我写一个然后在调用close之前立即阅读,它就可以了。但如果我试着自己阅读,那就没有了。这是我在User中的代码:

@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>

3 个答案:

答案 0 :(得分:1)

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

并且您100%确定ID = 1的用户表中是否存在实际实体?...

答案 1 :(得分:1)

EntityManagerFactory实例是重量级对象。它应该只创建一次。你应该创建一个单例,而不是在每次读/写中创建一个新单元,至少你只是在应用程序生命周期中执行一次操作。检查此用户发布的第一个代码作为示例,您可以复制并粘贴它:

When should EMF instances be created and closed

答案 2 :(得分:0)

最后一条评论修复了它,将“创建”更改为“验证”,非常感谢。 :)

我刚要张贴:

我摆脱了@GeneratedValue并自己添加了ID 1,2和3。然后我用ID 4,5和6再次运行它。在我的数据库中,我有ID 4,5和6 - 1,2和3刚刚消失。

将create更改为validate已解决此问题以及阅读问题。再说一遍,谢谢!我不明白创建和验证是做什么的。我现在有一个谷歌。