Java Persistence com Struts

Olá pessoal.

Estou com um problema para implementar JPA no Struts, não sei se a maneira como estou fazendo com o JPA, TopLink funciona com o Struts.

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="divida" transaction-type="JTA">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <jta-data-source>Oracle</jta-data-source>
    <class>gov.struts.form.UsuarioForm</class>
    <properties>
      <property name="toplink.logging.level" value="FINE"/>
      <property name="toplink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
      <property name="toplink.jdbc.url" value="jdbc:oracle:thin:@233.230.1.214:1521:GEINFO"/>
      <property name="toplink.jdbc.user" value="teste"/>
      <property name="toplink.jdbc.password" value="teste"/>
    </properties>
  </persistence-unit>
</persistence>

UsuarioForm.java

@Entity
@Table(name = "USUARIO")
@NamedQueries( {
        @NamedQuery(name = "Usuario.findByCodigo", query = "SELECT u FROM Usuario u WHERE u.codigo = :codigo"),
        @NamedQuery(name = "Usuario.findByNome", query = "SELECT u FROM Usuario u WHERE u.nome = :nome"),
        @NamedQuery(name = "Usuario.autenticar", query = "SELECT u FROM Usuario u WHERE u.login = :login AND u.senha = :senha"),
        @NamedQuery(name = "Usuario.findByLogin", query = "SELECT u FROM Usuario u WHERE u.login = :login"),
        @NamedQuery(name = "Usuario.findByAtivo", query = "SELECT u FROM Usuario u WHERE u.ativo = :ativo"),
        @NamedQuery(name = "Usuario.findByDtCadastro", query = "SELECT u FROM Usuario u WHERE u.dtCadastro = :dtCadastro")
    })
public class UsuarioForm extends ActionForm implements Serializable {
    @Id
    @Column(name = "CODIGO", nullable = false)
    private Integer codigo;

    @Column(name = "NOME", nullable = false)
    private String nome;

    @Column(name = "LOGIN", nullable = false)
    private String login;

    @Column(name = "SENHA", nullable = false)
    private String senha;

    @Column(name = "ATIVO", nullable = false)
    private byte ativo;

    @Column(name = "DT_CADASTRO", nullable = false)
    @Temporal(TemporalType.DATE)
    private Date dtCadastro;
    ......

Tabela no Oracle

CREATE TABLE usuario (
   codigo INTEGER,  --PK
   nome VARCHAR2(100) NOT NULL,
   login VARCHAR2(30) NOT NULL,
   senha VARCHAR2(30) NOT NULL,
   ativo NUMBER(1) DEFAULT 1 NOT NULL,
   dt_cadastro DATE NOT NULL,
   PRIMARY KEY(codigo)
);

Na hora dele terminar a configuração do JPA e carregar a classe persistida, da o seguinte erro:

Exception Description: Unknown abstract schema type [Usuario].
Local Exception Stack: 
Exception [TOPLINK-8034] (Oracle TopLink Essentials - 9.1 (Build b24)): oracle.toplink.essentials.exceptions.EJBQLException
Exception Description: Unknown abstract schema type [Usuario].
        at oracle.toplink.essentials.exceptions.EJBQLException.unknownAbstractSchemaType(EJBQLException.java:455)
        at oracle.toplink.essentials.internal.parsing.ParseTreeContext.classForSchemaName(ParseTreeContext.java:137)
        at oracle.toplink.essentials.internal.parsing.SelectNode.getClassOfFirstVariable(SelectNode.java:281)
        at oracle.toplink.essentials.internal.parsing.SelectNode.getReferenceClass(SelectNode.java:269)
        at oracle.toplink.essentials.internal.parsing.ParseTree.getReferenceClass(ParseTree.java:418)
        at oracle.toplink.essentials.internal.parsing.ParseTree.adjustReferenceClassForQuery(ParseTree.java:79)
        at oracle.toplink.essentials.internal.parsing.EJBQLParseTree.populateReadQueryInternal(EJBQLParseTree.java:112)
        at oracle.toplink.essentials.internal.parsing.EJBQLParseTree.populateQuery(EJBQLParseTree.java:93)
        at oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:215)
        at oracle.toplink.essentials.queryframework.EJBQLPlaceHolderQuery.processEjbQLQuery(EJBQLPlaceHolderQuery.java:96)
        at oracle.toplink.essentials.internal.sessions.AbstractSession.processEJBQLQueries(AbstractSession.java:2040)
        at oracle.toplink.essentials.internal.sessions.AbstractSession.processEJBQLQueries(AbstractSession.java:2027)
        at oracle.toplink.essentials.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:679)
        at oracle.toplink.essentials.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:559)
        at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:180)
        at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:191)
        at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:78)
        at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:113)
        at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:107)
        at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:76)
        at gov.sefin.controller.Fabrica.getEntityManager(Fabrica.java:28)
        at gov.sefin.struts.action.LogarAction.execute(LogarAction.java:34)
        at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
        at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
        at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
        at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:397)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:276)
        at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:255)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:586)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:73)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:182)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:586)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:556)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1029)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:137)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:586)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:556)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1029)
        at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:249)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:618)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.processNonBlocked(DefaultProcessorTask.java:549)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:790)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:332)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:254)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:205)
        at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:252)
        at com.sun.enterprise.web.connector.grizzly.WorkerThreadImpl.run(WorkerThreadImpl.java:103)
Unhandled Exception thrown: class java.lang.RuntimeException

O ponto que ocorre o problema é nesta chamanda que fica na Action:

...
EntityManager em = Fabrica.getInstance().getEntityManager();
...

Fabrica.java (Singleton)

package ...
import ...

public class Fabrica {
    private static Fabrica instance = null;
    private EntityManagerFactory factory = null;
    private EntityManager em = null;
    
    private Fabrica() {
        try {
            
            factory = Persistence.createEntityManagerFactory("divida");
            
        } catch (javax.persistence.PersistenceException e) {
            System.err.println("\n---\n===> Persistence Exception: " + e.getMessage() + "\n---\n");
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    public EntityManager getEntityManager() {
        try {
            
            em = factory.createEntityManager();
            return em;
            
        } catch (Exception e) {
            System.err.println("=> Exception: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    public static synchronized Fabrica getInstance() {
        if (instance == null) {
            instance = new Fabrica();
        }
        return instance;
    }
    
}

Posso gerar as annotations na classe que extende um ActionForm ? Não creio que isso seje problema, mas quando começamos a não visualizar a solução, tudo vira motivo para desconfiança. :lol:

Alguém saberia que erro seria esse? Qualquer ajuda será muito bem vinda.

Abraço a todos…

[]´s
Fábio Heleno

Infelizmente eu não sei a resposta para sua pergunta. Mas eu gostaria de perguntar se você tem algum material sobre JPA. Estou querendo aprender mas não sei por onde começar!

É pq no NameQuery vc usou Usuario.
Olha:
@NamedQuery(name = “Usuario.findByCodigo”

E o nome da sua Classe é:
public class UsuarioForm extends

Então deveria se assim:
@NamedQuery(name=“UsuarioForm.findByCodigo”

Tem que mudar o FROM tb ficando assim

"SELECT u FROM UsuarioForm u WHERE…