我看过许多与我的问题有关的问答,但没有一个能解决我的问题。
我为自己写了一个简单的JPA示例,但是我收到了这个错误
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named k
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at Runner.main(Runner.java:13)
这是META-INF / persistence.xml中的持久性文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="k" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>FirstEntity</class>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="myjava123"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
</properties>
</persistence-unit>
这是我的实体类:
@Entity(name = "first")
public class FirstEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
@Column
String name;
@OneToOne(optional=false)
@JoinColumn(name = "Id")
SecondEntity secondEntity;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
最后我的跑步者:
public class Runner {
public static void main(String[] args) {
FirstEntity firstEntity = new FirstEntity();
SecondEntity secondEntity = new SecondEntity();
EntityManager entityManager = Persistence.createEntityManagerFactory("k").createEntityManager();
entityManager.getTransaction().begin();
firstEntity.setName("ali");
entityManager.close();
}
}
问题是什么?
答案 0 :(得分:2)
似乎“hibernate-entitymanager.jar”不在你的类路径中。 只需将其放在/ lib文件夹中或更新依赖项。