使用 EclipseLink 的 H2 数据库问题(即使没有错误,程序也不会更改数据库)

Problem with H2 Database using EcipseLink (Program doesnt change database even without a error)

所以...我刚开始使用数据库,我的兄弟建议我使用 H2 和 EclipseLink 开始。快速 Google 搜索并找到教程:

  1. https://www.javatips.net/blog/eclipselink-jpa-with-h2-database

  2. https://www.javatips.net/blog/java-persistence-jpa-2-0-tutorial-with-eclipselink

如果我按照教程中描述的方式对其进行编码,当我 运行 它时我不会收到任何错误,但我检查数据库时我的 Table 仍然是空的。我搜索了大约一个星期,但没有找到答案。

我的代码:

Class学生

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "STUDENT")
public class Student implements java.io.Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "STUDENTID")
  private long studentId;

  @Column(name = "STUDENTNAME")
  private String studentName;

  public void setStudentId(long studentId) {
      this.studentId = studentId;
  }

  public long getStudentId() {
      return studentId;
  }

  public void setStudentName(String studentName) {
      this.studentName = studentName;
  }

  public String getStudentName() {
      return studentName;
  }
}

JPA 示例

import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;



public class JPAExample {

    private EntityManager entityManager = EntityManagerUtil.getEntityManager();

    public static void main(String[] args) {
        JPAExample example = new JPAExample();
        System.out.println("After Sucessfully insertion ");
        Student student1 = example.saveStudent("Sumith");
        Student student2 = example.saveStudent("Anoop");
        example.listStudent();
        System.out.println("After Sucessfully modification ");
        example.updateStudent(student1.getStudentId(), "Sumith Honai");
        example.updateStudent(student2.getStudentId(), "Anoop Pavanai");
        example.listStudent();
        System.out.println("After Sucessfully deletion ");
        example.deleteStudent(student2.getStudentId());
        example.listStudent();


    }

    public Student saveStudent(String studentName) {
        Student student = new Student();
        try {
            entityManager.getTransaction().begin();
            student.setStudentName(studentName);
            student = entityManager.merge(student);
            entityManager.getTransaction().commit();
        } catch (Exception e) {
            entityManager.getTransaction().rollback();
        }
        return student;
    }

    public void listStudent() {
        try {
            entityManager.getTransaction().begin();
            @SuppressWarnings("unchecked")
            List<Student> Students = entityManager.createQuery("from Student").getResultList();
            for (Iterator<Student> iterator = Students.iterator(); iterator.hasNext();) {
                Student student = (Student) iterator.next();
                System.out.println(student.getStudentName());
            }
            entityManager.getTransaction().commit();
        } catch (Exception e) {
            entityManager.getTransaction().rollback();
        }
    }

    public void updateStudent(Long studentId, String studentName) {
        try {
            entityManager.getTransaction().begin();
            Student student = (Student) entityManager.find(Student.class, studentId);
            student.setStudentName(studentName);
            entityManager.getTransaction().commit();
        } catch (Exception e) {
            entityManager.getTransaction().rollback();
        }
    }

    public void deleteStudent(Long studentId) {
        try {
            entityManager.getTransaction().begin();
            Student student = (Student) entityManager.find(Student.class, studentId);
            entityManager.remove(student);
            entityManager.getTransaction().commit();
        } catch (Exception e) {
            entityManager.getTransaction().rollback();
        }
    }
}

EntityManagerUtil

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


public class EntityManagerUtil {
    private static final EntityManagerFactory entityManagerFactory;
    static {
        try {
            entityManagerFactory = Persistence.createEntityManagerFactory("test");

        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();

    }
}

Persistence.xml

<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <class>Student</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
        <property name="javax.persistence.jdbc.url"    value="jdbc:h2:~/test" />
        <property name="javax.persistence.jdbc.user" value="sa" />
        <property name="javax.persistence.jdbc.password" value="" />
        <property name="eclipselink.ddl-generation" value="create-tables"/>
        <property name="eclipselink.ddl-generation.output-mode" value="database" />
    </properties>
</persistence-unit>

当我 运行 JPAExample 我得到这个输出: My Output

但是预期的输出应该是这样的: Expected Output

如果我查看 H2 数据库,没有任何变化: H2 Database Interface (German Language)

希望有人可以帮助我或者可以 link 我 usefull/better JPA 教程 :)

您正在捕获异常但未确认或记录它们 - 您应该确保至少记录它。

在这种情况下,您的 listStudent 方法在

上出现异常
 entityManager.createQuery("from Student").getResultList()

因为这是一个无效的 JPQL 查询。你应该使用类似的东西:

entityManager.createQuery("select s from Student s").getResultList()