How to set up a JPA project with hibernation

This time I really need your help. I am trying to create a very simple jpa example with hibernation and I just can't figure out why it is not working.

here is the code: Basic

package hi;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class pl {
public static void main(String[] args) {


    EntityManagerFactory emf = Persistence.createEntityManagerFactory("ahhhhhPU");
    EntityManager em = emf.createEntityManager();
    Moon m = new Moon(56);

    em.getTransaction().begin();

    try{
        em.persist(m);
        em.getTransaction().commit();
    }catch (Exception e){
        System.out.println("ERROR!!");
        e.printStackTrace();
        em.getTransaction().rollback();
    }finally {
        em.close();
    }

    System.out.println("done&DONE!!!");
}
}

object: (automatically done)

package hi;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "moon", catalog = "test", schema = "")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Moon.findAll", query = "SELECT m FROM Moon m"),
@NamedQuery(name = "Moon.findByNum", query = "SELECT m FROM Moon m WHERE m.num = :num"),
@NamedQuery(name = "Moon.findByName", query = "SELECT m FROM Moon m WHERE m.name = :name")})
public class Moon implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "num")
private Integer num;
@Size(max = 50)
@Column(name = "Name")
private String name;

public Moon() {
}

public Moon(Integer num) {
    this.num = num;
}

public Integer getNum() {
    return num;
}

public void setNum(Integer num) {
    this.num = num;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (num != null ? num.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Moon)) {
        return false;
    }
    Moon other = (Moon) object;
    if ((this.num == null && other.num != null) || (this.num != null && !this.num.equals(other.num))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "hi.Moon[ num=" + num + " ]";
}

}

and constancy:

    <?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="ahhhhhPU" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>test</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
  <persistence-unit name="ahhhhhPU2" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>test</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

I get this error: (on my EntityManagerFactory)

debug:
Apr 20, 2015 12:28:50 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Apr 20, 2015 12:28:50 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Apr 20, 2015 12:28:50 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/jandex/IndexView
    at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:51)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:129)
    at org.hibernate.ejb.HibernatePersistence.getEntityManagerFactoryBuilderOrNull(HibernatePersistence.java:93)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:86)
    at org.hibernate.ejb.HibernatePersistence.getEntityManagerFactoryBuilderOrNull(HibernatePersistence.java:101)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:67)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at hi.pl.main(pl.java:20)
Caused by: java.lang.ClassNotFoundException: org.jboss.jandex.IndexView
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 10 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

can anyone help? thank...

+4
source share
4 answers

If you want to use the Hibernate Provider

<provider>org.hibernate.ejb.HibernatePersistence</provider>

try the following: ClassNotFoundException: org.jboss.jandex.IndexView

and add Hibernate 4.3.x(JPA 2.1)lib

+2
source

Spring Hibernate, Maven ( NetBeans), java.lang.ClassNotFoundException: org.jboss.jandex.IndexView:

jandex-2.0.0Final.jar( ) . - Maven, , -

+2

Edit

<provider>org.hibernate.ejb.HibernatePersistence</provider>

Wherein:

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

I hope it works

+1
source

The solution in my situation (used hibernate 4.3.1 from jpa 2.1 to netbeans) was to download and add jandex-2.0.0Final.jar and include it in my library, as suggested in another answer.

In addition, I had to ensure that my persistence.xml file had the following under my save unit, which is sleeping: < provider >org.hibernate.ejb.HibernatePersistence < /provider >

0
source

All Articles