实体管理器没有持久性提供程序错误JPA / hibernate

时间:2016-12-09 01:36:47

标签: java mysql hibernate jpa

我想使用JPA / hibernate添加数据库和持久化数据。我在正确的位置使用了persistence.xml,但我没有获得实体管理器的持久性提供程序"错误。数据库在mysql中。任何建议都非常感谢。这是我的代码:

文件夹结构:

enter image description here

的servlet:

@Entity
@Table(name = "reservations_db")
public class ReservationEntity {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id; 
    @Column(name = "pickUpDate")
    private String pickUpDate;

    @Column(name = "totalPass")
    private Integer totalPass;

    public ReservationEntity() 
    {       
    }

    public ReservationEntity(String pickUpDate, Integer totalPass) 
    {
        this.setPickUpDate(pickUpDate);
        this.setTotalPass(totalPass);

    }

    public int getId() {
    return id;
}

 public void setId(int id) {
    this.id = id;
}
    public String getPickUpDate() {
        return pickUpDate;
    }
    public void setPickUpDate(String pickUpDate) {
        this.pickUpDate = pickUpDate;
    }

    public Integer getTotalPass() {
        return totalPass;
    }
    public void setTotalPass(Integer totalPass) {
        this.totalPass = totalPass;
    }
}

实体类:

public class ReservationSevice 
{
    static EntityManagerFactory emf = Persistence.createEntityManagerFactory("ReservationsPU");
    private static EntityManager em = emf.createEntityManager(); 

    public void createReservation (ReservationEntity reservation)
    {
        em.getTransaction().begin();
        em.persist(reservation);
        em.getTransaction().commit();
    }
}

JPA持久性的服务类:

<persistence version="2.1"
    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">
    <persistence-unit name="ReservationsPU">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/world" />
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="password" />
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <property name="hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

的persistence.xml:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named ReservationsPU
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at com.welcomelimo.service.ReservationSevice.<init>(ReservationSevice.java:11)
    at com.welcomelimo.controller.Reservations.doPost(Reservations.java:56)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Exception occured

错误:

sign

更新

我添加了lib文件夹,以便显示我为项目添加的jar文件,因为我没有使用maven。

enter image description here

更新2

我最终使用了maven并且它现在正在工作。对于pom.xml,请参阅下面的@Surace的答案。

2 个答案:

答案 0 :(得分:4)

将persistence.xml更改为

<persistence version="2.1"
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">
<persistence-unit name="ReservationsPU">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/world" />
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.username" value="root" />
        <property name="hibernate.connection.password" value="password" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="hibernate.show_sql" value="true" />
        <property name="use_sql_comments" value="true" />
        <property name="hibernate.format_sql" value="true" />
    </properties>
</persistence-unit>

如果您使用Maven

,请在pom.xml中添加以下依赖项
<dependencies>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.37</version>
    </dependency>

</dependencies>

答案 1 :(得分:0)

我认为问题在于日食。如果将JPA显式设置为JPA项目,Eclipse可能只允许运行JPA。

您可以通过打开资源管理器中的上下文菜单将项目转换为JPA项目。