gpt4 book ai didi

Java 持久性问题

转载 作者:行者123 更新时间:2023-11-29 09:25:41 25 4
gpt4 key购买 nike

我正在尝试通过 GlassFish 在 EJB 中使用 JPA 启动并运行一个简单示例。我有以下 persistence.xml

<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="default" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/wms</jta-data-source>
<class>com.xxx.xxx.datamodel.MyTest</class>
<exclude-unlisted-classes />
<properties>
<property name="eclipselink.target-server" value="SunAS9"/>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.target-database" value="Oracle"/>

<property name="eclipselink.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="eclipselink.jdbc.url" value="[dbconnectionstring]" />
<property name="eclipselink.jdbc.user" value="user" />
<property name="eclipselink.jdbc.password" value="password" />
</properties>
</persistence-unit>
</persistence>

一个简单的实体:

package com.xxx.xxx.datamodel;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "move_task")
public class MyTest {

private int key;
private String description;

@Id
public int getKey(){
return key;
}

public void setKey(int key){
this.key = key;
}

public String getDescription(){
return this.description;
}

public void setDescription(String description){
this.description = description;
}

@Override
public String toString(){
return "Key: " + key + " Description: " + description;
}

}

最后是下面的代码来尝试使用上面的代码:

private void jpaCall() {
try{
emf = Persistence.createEntityManagerFactory("default");
em = emf.createEntityManager();
log.info("JPA init complete");

final List<MyTest> list = em.createQuery("select p from MyTest p").getResultList();

for (MyTest current : list) {
final String description = current.getDescription();
log.info("JPA: Desc: " + description);
}

}
catch(Exception e){
log.error("Error on JPA", e);
}

}

当它作为我的 EJB 初始化的一部分运行时,我收到以下错误:

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Error compiling the query [select p from MyTest p]. Unknown entity type [MyTest].
...
Caused by: Exception [EclipseLink-8034] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Error compiling the query [select p from MyTest p]. Unknown entity type [MyTest].

我不确定自己做错了什么,希望得到任何帮助。

干杯,

詹姆斯

最佳答案

正如上面的评论所暗示的,这似乎是 glassfish 的 eclipse 插件的问题。手动部署耳朵时我没有遇到任何问题。

感谢大家的帮助。

詹姆斯

关于Java 持久性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2252115/

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