猿问

无法从 persistence.xml 连接到 JDBC

我有普通的 java 课本注释为 jpa 的实体。我配置了持久性 xml,但我有很大的堆栈跟踪,但我不明白。另外,我在 javax.persistence 中找不到 jdbc 包。关我什么事?


这是我的实体


package Java.JPA;


import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;


@Entity

public class Book

{

  @Id @GeneratedValue

  private long id;

  private String title;


  public Book()

  {


  }


  public Book(String title) {

    this.title = title;

  }


  public String getTitle()

  {

    return title;

  }


  public void setTitle(String title) {

    this.title = title;

  }

}

这是我的主要课程


package Java;


import Java.JPA.Book;

import java.sql.SQLException;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.EntityTransaction;

import javax.persistence.Persistence;


public class main

{


  public static void main(String[] args) throws SQLException, ClassNotFoundException {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("example");

    EntityManager em = emf.createEntityManager();

    Book book = new Book("TestJPA");

    EntityTransaction tx = em.getTransaction();

    tx.begin();

    em.persist(book);

    tx.commit();


    em.close();

    emf.close();

  }

}

这是我的persistence.xml


<?xml version="1.0" encoding="UTF-8" ?>

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence

                                 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"

  version="2.1">


  <persistence-unit name="example">

    <!--<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>-->

    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>



大话西游666
浏览 119回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答