Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup JPA project with hibernate

I really need your help this time. I'm trying to build a very simple jpa example with hibernate and I just can't figure out why it's not working.

here is the code: main

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!!!");
}
}

the entity: (automatically made)

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 the persistence:

    <?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'm getting 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 someone help? thanks...

like image 439
Mitzi Avatar asked Apr 20 '15 10:04

Mitzi


People also ask

How does JPA relate to Hibernate?

Java Persistence API (JPA) defines the management of relational data in the Java applications. Hibernate is an Object-Relational Mapping (ORM) tool which is used to save the state of Java object into the database. It is just a specification. Various ORM tools implement it for data persistence.


2 Answers

In the context of a Spring and Hibernate but non-Maven project (working in NetBeans), where java.lang.ClassNotFoundException: org.jboss.jandex.IndexView:

Add jandex-2.0.0Final.jar (or whatever version works) to your Library. -- In a Maven project likely similarly - add it as a dependency

like image 98
anvw Avatar answered Sep 28 '22 15:09

anvw


If you want to use Hibernate Provider

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

try this: ClassNotFoundException: org.jboss.jandex.IndexView

and add Hibernate 4.3.x(JPA 2.1) lib

like image 23
Rudi Wijaya Avatar answered Sep 28 '22 16:09

Rudi Wijaya