包含所有必要的topplinks库在运行example.java时仍然出现以下错误
使用netbeans 6.8
在classpath中找不到META-INF/persistence.xml。
persistence . xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="MatdaanPU" transaction-type="JTA">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<jta-data-source>matdaan</jta-data-source>
<class>EntityBeans.Trial</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="toplink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Example.java
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Example {
public Example() {
EntityManagerFactory emf=Persistence.createEntityManagerFactory("MatdaanPU");
EntityManager em=emf.createEntityManager();
List results = em.createNamedQuery("Trial.findById")
.setParameter("id", "1")
.getResultList();
System.out.println("HI");
System.out.println(results.get(1));
}
public static void main(String args[]){
Example e=new Example();
}
}
目录结构
Root
|
|
|----src
| |
| |----conf
| |
| |-----Manifest.MF
| |-----Persistence.xml
将persistence.xml放在src/META-INF文件夹中。
此外,字母大小写可能很重要。将Persistence.xml重命名为Persistence.xml