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

时间:2018-09-25 08:36:19

标签: database eclipselink h2

所以...我刚刚开始使用数据库,而我的兄弟建议我将H2与EclipseLink一起使用来启动。在Google上进行了快速搜索并找到了Tutuorial:

  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

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

我的代码:

班级学生

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;
  }
}

JPAExample

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 个答案:

答案 0 :(得分:0)

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

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

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

,因为这是无效的JPQL查询。您应该使用类似这样的东西:

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