我不能讓Hibernate與一個非常簡單的代碼在netbeans H2數據庫上工作。請幫忙!我得到這個錯誤:休眠與H2數據庫不工作在netbeans
Testcase: initializationError(Teste): Caused an ERROR
org/hamcrest/SelfDescribing
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
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)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
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)
這裏是文件的內容:
的hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:./testeDB</property>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
</session-factory>
</hibernate-configuration>
的persistence.xml
<?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="HibernateAgoraVaiPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Pessoa</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:./testeDB"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
Pessoa.java
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Pessoa implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column(length=50, nullable=false)
private String nome;
@Temporal(TemporalType.DATE)
private Date aniversario;
public Pessoa(){
}
public Date getAniversario() {
return this.aniversario;
}
public void setAniversario(Date aniversario) {
this.aniversario = aniversario;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNome() {
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
Teste.java
import java.util.Collection;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import org.junit.Test;
public class Teste {
private EntityManager em;
public Teste(){
this.em = Persistence.createEntityManagerFactory("TesteHibernatePU").createEntityManager();
}
@Test
public void inserir(){
// Instanciamos um objeto Pessoa, setando nome e data de nascimento
Pessoa p1 = new Pessoa();
p1.setNome("Jonnas Fonini");
p1.setAniversario(new Date());
Pessoa p2 = new Pessoa();
p2.setNome("Luana Fonini");
p2.setAniversario(new Date());
Pessoa p3 = new Pessoa();
p3.setNome("Fulano de Tal");
p3.setAniversario(new Date());
// Iniciamos a transação
em.getTransaction().begin();
// Aqui persistimos os objetos recém criados
em.persist(p1);
em.persist(p2);
em.persist(p3);
// E aqui efetuamos definitivamente a transação
em.getTransaction().commit();
}
@Test
public void listar(){
Collection <Pessoa> lista = em.createQuery("from Pessoa").getResultList();
for (Pessoa p : lista){
System.out.println(p.getId() + " – " + p.getNome() + " – " + p.getAniversario());
}
}
}
下面是我做的步驟:創建一個新的項目在NetBeans
1)(8.1版本)
2)將Hibernate庫添加到項目中:右鍵單擊庫>添加庫...>導入> Hibernate 4.3.x
3)從這個鏈接 http://repo2.maven.org/maven2/com/h2database/h2/1.3.176/h2-1.3.176.jar下載H2的最新穩定版,並添加了罐子爲庫:右鍵單擊在庫>添加JAR /文件夾>選擇下載JAR文件
4)創建一個新的在服務選項卡中的數據庫連接:右鍵單擊數據庫>新建連接...>選擇新驅動程序選項>選擇步驟(3)下載的JAR文件>確定並且下一步>用戶名和密碼爲空白,剛通知「jdbc:h2 :./ testeDB「(不帶引號)作爲JDBC URL,併成功測試連接>選擇PUBLIC SCHEMA並完成
5)創建一個新的Hibernate配置助理:右鍵點擊「源代碼包」>新建>其他>休眠>助理>接下來,選擇步驟(4)創建的連接
6)創建一個新的持久單元:右鍵單擊「源代碼包」>新建>其它>持久>持久性單元>下一頁>更改持久化庫到Hibernate JPA 2.1,數據庫連接到步(4),表策略創建的連接創建
之後,我編輯的persistence.xml和hibernate.cfg.xml使用org.hibernate.dialect.H2Dialect
最後我創建了一個簡單的Person類和一些測試代碼。還授予了對項目文件夾完全權限與
sudo chmod 777 -R HibernateAgoraVai
我雖然可能是用於創建數據庫文件的權限問題..
起初,它看起來像與hamcrest圖書館的類路徑衝突。請驗證您是否在classpath上沒有超過一個hamcrest庫。 – gauee
你能指點我怎麼做?在庫下,我發現了幾個指向「Hibernate 4.3.x」和「Hibernate 4.3.x(JPA2.1)」的庫,但在刪除「Hibernate 4.3.x」並試圖再次運行測試之後,錯誤繼續發生。 –