Java持久性问题 [英] Java Persistence Issue

查看:199
本文介绍了Java持久性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过GlassFish在EJB中使用JPA创建并运行一个简单的示例。我有以下 persistence.xml

 < persistence version = 1.0xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http:/ /java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd\"> 
< persistence-unit name =defaulttransaction-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 />
<属性>
< property name =eclipselink.target-servervalue =SunAS9/>
< property name =eclipselink.logging.levelvalue =FINEST/>
< property name =eclipselink.target-databasevalue =Oracle/>

< property name =eclipselink.jdbc.drivervalue =oracle.jdbc.OracleDriver/>
< property name =eclipselink.jdbc.urlvalue =[dbconnectionstring]/>
< property name =eclipselink.jdbc.uservalue =user/>
< property name =eclipselink.jdbc.passwordvalue =password/>
< / properties>
< / persistence-unit>
< /余辉>

一个简单的实体:

  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;
私有字符串描述;

@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(){
returnKey:+ key +Description:+ description;
}

}

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

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

最终列表< MyTest> list = em.createQuery(从MyTest p中选择p)。getResultList(); (MyTest current:list)

{
final String description = current.getDescription();
log.info(JPA:Desc:+ description);


$ b catch(Exception e){
log.error(JPA错误,e);
}

}

当它作为我的EJB初始化出现以下错误:

$ p $ java.lang.IllegalArgumentException:在EntityManager中创建查询时发生异常:
异常说明:编译查询时出错[从MyTest p中选择p]。未知实体类型[MyTest]。
...
导致:异常[EclipseLink-8034](Eclipse持久性服务 - 2.0.0.v20091127-r5931):org.eclipse.persistence.exceptions.JPQLException
异常说明:编译查询时出错[从MyTest p选择p]。未知实体类型[MyTest]。

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



干杯,

James

解决方案

<因此,正如上面的评论表明,这似乎是一个问题与eclipse插入glassfish。手动部署耳朵时,我没有任何问题。



感谢所有人的帮助。



p>

I am trying to get a simple example up and running using JPA in an EJB through GlassFish. I have the following 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>

A simple entity:

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;
    }

}

And finally the following code to try use the above:

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

    }

When this is run as part of my EJB initialisation I get the following error:

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].

I am not sure what I am doing wrong and would appreciate any help.

Cheers,

James

解决方案

So as the comments above suggest this seems to be an issue with the eclipse plug in for glassfish. I am having no problems when deploying the ear manually.

Thanks all for the help.

James

这篇关于Java持久性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆