0

I have seen many Question and Answer that is related my question but none of them solved my problem.

I have written a simple JPA example for myself but I got this error

  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)

this is my persistence file in 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>

this is my entity class:

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



}

and at last my runner:

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();




}

}

What is the problem??

K1A
  • 33
  • 6

1 Answers1

2

It seems like "hibernate-entitymanager.jar" not in your classpath. Just put it in your /lib folder or update dependencies.

njjnex
  • 1,490
  • 13
  • 23