如何使用hibernate设置JPA项目

时间:2015-04-20 10:08:31

标签: netbeans netbeans-8

这次我真的需要你的帮助。我试图用hibernate构建一个非常简单的jpa示例,但我无法弄清楚它为什么不起作用。

这是代码: 主

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

实体:(自动制作)

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 + " ]";
}

}

和持久性:

    <?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>

我收到此错误:(在我的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)
有人可以帮忙吗? 感谢...

4 个答案:

答案 0 :(得分:5)

在Spring和Hibernate但非Maven项目(在NetBeans中工作)的上下文中,在哪里 java.lang.ClassNotFoundException:org.jboss.jandex.IndexView:

将jandex-2.0.0Final.jar(或任何版本适用)添加到您的库中。 - 在类似的Maven项目中 - 将其添加为依赖项

答案 1 :(得分:2)

如果您想使用Hibernate Provider

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

试试这个: ClassNotFoundException: org.jboss.jandex.IndexView

并添加Hibernate 4.3.x(JPA 2.1) lib

答案 2 :(得分:1)

更改

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

有了这个:

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

我希望它有效

答案 3 :(得分:0)

在我的情况下(使用hibernate 4.3.1和netbeans上的jpa 2.1)的解决方案是下载并添加jandex-2.0.0Final.jar并将其包含在我的库中,如另一个答案中建议的那样。

此外,我必须确保我的persistence.xml文件在我的持久性单元下具有以下内容,这是特定于hibernate的: < provider >org.hibernate.ejb.HibernatePersistence < /provider >