我正在尝试遵循this教程。当他们运行代码时,它是成功的。运行代码时,出现错误消息WARNING: javax.persistence.spi::No valid providers found.
,我查看了文档并明确定义了实体/提供者。我确实为.jpa.HibernatePersistenceProvider
使用了一些红色错误字体,因为它表示无法解析该类。这是主要班级
@PersistenceUnit(unitName = "org.hibernate.tutorial.jpa")
private static EntityManagerFactory entityManagerFactory;
public static void main(String[] args){
entityManagerFactory = Persistence.createEntityManagerFactory("org.hibernate.tutorial.jpa");
Client client = new Client();
client.setId(2);
client.setName("bob");
Bank bank = new Bank();
bank.setName("wells fartgo");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(client);
entityManager.persist(bank);
entityManager.getTransaction().commit();
entityManagerFactory.close();
}
/ resources / WEB_INF中的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="org.hibernate.tutorial.jpa">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/classicmodels"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.CharSet" value="utf8"/>
<property name="hibernate.connection.characterEncoding" value="utf8"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.cache.region.factory_class"
value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
<property name="hibernate.javax.cache.provider" value="org.ehcache.jsr107.EhcacheCachingProvider"/>
<!--use second level cache-->
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<!--use 3rd level cache-->
<property name="hibernate.cache.use_query_cache" value="true"/>
</properties>
</persistence-unit>
也是我的pom.xml
<dependencies>
<dependency>
<groupId>org.clojure</groupId>
<artifactId>java.jdbc</artifactId>
<version>0.7.0-alpha3</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.0.Alpha2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate-core-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
主要(银行/客户)中的类只有getter和setter,所以没有错。如果有人可以让我知道如何成功运行此演示,那将很棒。谢谢。