gpt4 book ai didi

netbeans - 如何使用 Hibernate 设置 JPA 项目

转载 作者:行者123 更新时间:2023-12-04 17:47:18 26 4
gpt4 key购买 nike

这次我真的需要你的帮助。我正在尝试使用 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)

有人可以帮忙吗?
谢谢...

最佳答案

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

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

关于netbeans - 如何使用 Hibernate 设置 JPA 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29744915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com