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

Posted

技术标签:

【中文标题】使用 EclipseLink 的 H2 数据库出现问题(即使没有错误,程序也不会更改数据库)【英文标题】:Problem with H2 Database using EcipseLink (Program doesnt change database even without a error) 【发布时间】:2018-09-25 08:36:19 【问题描述】:

所以...我刚开始使用数据库,我的兄弟建议我使用带有 EclipseLink 的 H2 来开始。快速谷歌搜索并找到一个教程:

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

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

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

我的代码:

同学

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)

希望有人可以帮助我或者可以链接我一个有用/更好的 JPA 教程 :)

【问题讨论】:

我假设您遇到了异常。请在插入、更新和删除方法中记录异常。 【参考方案1】:

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

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

上遇到异常
 entityManager.createQuery("from Student").getResultList()

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

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

【讨论】:

好吧,我改变了查询,我得到了正确的输出,但是在 H2 接口中没有任何改变 检查您的连接方式,以及它是否与“~/test”相同的数据库是用户特定的。 h2database.com/html/tutorial.html#creating_new_databases

以上是关于使用 EclipseLink 的 H2 数据库出现问题(即使没有错误,程序也不会更改数据库)的主要内容,如果未能解决你的问题,请参考以下文章

EclipseLink 和 H2

在测试环境中使用 Eclipselink 和 h2 生成序列时出现异常

EclipseLink2.7出现java.lang.SecurityException异常

如何使用 EclipseLink 重新连接丢失的连接?

IntegrityException的spring eclipselink问题

JPA + EclipseLink+ HSQLDB 不创建表