Java Persistence Issue
Asked Answered
L

1

2

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

Landgraviate answered 12/2, 2010 at 13:41 Comment(12)
How do you package all this? Where do you put your persistence.xml?Frigid
It is deployed to glassfish from eclipse. The persistence.xml is in the META-INF directory of the EJB.Landgraviate
I know it is finding the persistence.xml because if i change it to have an invalid provider I get the appropiate exception: <provider>org.eclipse.persistence.jpa.PersistenceProviderjhjh</provider> javax.persistence.PersistenceException: No Persistence provider for EntityManager named defaultLandgraviate
Ok, good. This isn't the problem then :)Frigid
Try the fully-qualified name in the query. i.e. com.ocado.atlas.datamodel.MyTest instead of just MyTest, and tell us the result.Maurice
@Maurice It should work with the unqualified name though. Weird issue.Frigid
James, does MyTest exactly look what you're showing?Frigid
and check for typos in the persistence.xml @Pascal: it should, yes. But if it works qualified, we'll have a direction (I've had a case where qualification was needed, can't recall why, though)Maurice
It won't work with the fully-qualified class name. I had this problem before and it is a Eclipselink limitation: bugs.eclipse.org/bugs/show_bug.cgi?id=243698. Not a bug though, because not part of the specification.Ladew
MyTest is an exact copy of the code as is the persistance.xml, I can't see any typos. I tried the fully qualified path but as @rok suggested this does not seem to be supported: Exception Description: Syntax error parsing the query [select p from com.ocado.atlas.datamodel.MyTest p], line 1, column 17: syntax error at [.]. I tried with /'s instead of .'s as well.Landgraviate
If I build my EAR and copy it to autodeploy rather than deploying through eclipse it works. Any suggestions on why I am only seeing the issue through eclipse?Landgraviate
Hard to say but it really sounds like an issue with the GlassFish Eclipse plugin then.Frigid
L
0

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

Landgraviate answered 17/2, 2010 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.