JPA: Translating Annotations to Metadata (orm.xml)

View: New views
3 Messages — Rating Filter:   Alert me  

JPA: Translating Annotations to Metadata (orm.xml)

by Asdfawer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everyone,

I'm trying to translate the commented annotations in the next two classes to metadata:

[code]
package pfc.model;
import java.io.Serializable;
import java.util.Date;
//import javax.persistence.*;

//@Entity
public class DadesComentari implements Serializable {

    //@Id
    //@Column(name = "id", nullable = false)
    private Integer id;
    //@Column(name = "accio")
    private Integer accio;
    //@Column(name = "idAutor", nullable = false)
    private String idAutor;
    //@Column(name = "perfilAutor", nullable = false)
    private Integer perfilAutor;
    //@Column(name = "dataCreacio", nullable = false)
    //@Temporal(TemporalType.TIMESTAMP)
    private Date dataCreacio;
    //@Column(name = "text", nullable = false)
    private String text;
    //@Column(name = "idIncidencia", nullable = false)
    private Integer idIncidencia;
       
        ...(Setters and getters)...
}


package pfc.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
//import javax.persistence.*;

//@Entity
public class DadesIncidencia implements Serializable {

    //@Id
    //@Column(name = "id", nullable = false)
    private Integer id;
    //@Column(name = "servAfectats", nullable = false)
    private Integer servAfectats;
    //@Column(name = "idCreador", nullable = false)
    private String idCreador;
    //@Column(name = "idTecnic")
    private String idTecnic;
    //@Column(name = "idTD")
    private String idTD;
    //@Column(name = "visitaSolicitada", nullable = false)
    private Boolean visitaSolicitada;
    //@Column(name = "dataCreacio", nullable = false)
    //@Temporal(TemporalType.TIMESTAMP)
    private Date dataCreacio;
    //@Column(name = "dataTancament")
    //@Temporal(TemporalType.TIMESTAMP)
    private Date dataTancament;
    //@Column(name = "nomClient")
    private String nomClient;
    //@Column(name = "cognomsClient")
    private String cognomsClient;
    //@Transient
    private ArrayList comentaris;
               
        ...(Setters and getters)...
}
[/code]

To do it I have created the next file orm.xml:

[code]
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
                 version="1.0">
    <package>pfc</package>
    <entity class="pfc.model.DadesIncidencia" name="incidencia">      
        <attributes>
            <id name="id">
                <column name="id" nullable="false"/>
            </id>
            <basic name="servAfectats">
                <column name="servAfectats" nullable="false"/>
            </basic>
            <basic name="dataCreacio">
                <column name="dataCreacio" nullable="false"/>
                <temporal>TIMESTAMP</temporal>
            </basic>  
            <basic name="dataTancament">
                <column name="dataTancament"/>
                <temporal>TIMESTAMP</temporal>
            </basic>
            <basic name="idCreador">
                <column name="idCreador" nullable="false"/>
            </basic>
            <basic name="idTecnic">
                <column name="idTecnic"/>
            </basic>
            <basic name="idTD">
                <column name="idTD"/>
            </basic>
            <basic name="visitaSolicitada">
                <column name="visitaSolicitada" nullable="false"/>
            </basic>
            <basic name="nomClient">
                <column name="nomClient"/>
            </basic>  
            <basic name="cognomsClient">
                <column name="cognomsClient"/>
            </basic>  
            <transient name="comentaris"/>
        </attributes>
    </entity>
    <entity class="pfc.model.DadesComentari" name="comentari">
        <attributes>
            <id name="id">
                <column name="id" nullable="false"/>
            </id>            
            <basic name="idIncidencia">
                <column name="idIncidencia" nullable="false"/>
            </basic>
            <basic name="perfilAutor">
                <column name="perfilAutor" nullable="false"/>
            </basic>
            <basic name="idAutor">
                <column name="idAutor" nullable="false"/>
            </basic>
            <basic name="dataCreacio">
                <column name="dataCreacio" nullable="false"/>
                <temporal>TIMESTAMP</temporal>
            </basic>
            <basic name="accio">
                <column name="accio" />
            </basic>
            <basic name="text">
                <column name="text" nullable="false"/>
            </basic>            
        </attributes>
    </entity>
</entity-mappings>
[/code]

Which I reference from the persistence.xml:

[code]
<?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="pfc" transaction-type="RESOURCE_LOCAL">
        <provider>oracle.toplink.essentials.PersistenceProvider</provider>
        <mapping-file>pfc/model/orm.xml</mapping-file>
        <!--<class>pfc.model.DadesIncidencia</class>
    <class>pfc.model.DadesComentari</class> -->
        <properties>
            <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="toplink.jdbc.url" value="jdbc:mysql://localhost/pfc"/>
            <property name="toplink.jdbc.password" value=""/>
            <property name="toplink.jdbc.user" value="root"/>
        </properties>
    </persistence-unit>
</persistence>
[/code]

Having done only these modifications, the next exception appears when trying to execute the application:

[code]
Exception [TOPLINK-30005] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: WebappClassLoader
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@f4f44a

Internal Exception: javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.EntityManagerSetupException
Exception Description: predeploy for PersistenceUnit [pfc] failed.
Internal Exception: java.lang.NullPointerException
        at oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:143)
        at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:169)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
        at pfc.model.DAOFactory.connectarBD(DAOFactory.java:15)
        at pfc.model.FaƧanaDelModel.obtenirDadesIncidencia(FaƧanaDelModel.java:33)
        at pfc.IncidenciaController.handleRequest(IncidenciaController.java:18)
[/code]

Anybody knows how I can solve it? Or where I can search?

Thank you in advance

Re: JPA: Translating Annotations to Metadata (orm.xml)

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Seems like it is having issues finding the file, is it in the same jar as the persistence.xml and in the correct directory?  Perhaps try not linking the orm.xml and just put it in the same dir as the persistence.xml so it can auto-find it.

Is there a caused by to the null-pointer exception?

Re: JPA: Translating Annotations to Metadata (orm.xml)

by Asdfawer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for your answer, but it seems the exception message was a bit confusing, because somebody in this forum found what was missing and it had nothing to do with it. If somebody is interested the problem was:

"Somehow Toplink seems to think that you have PROPERTY based access to
entity and trying to get setMethod for one of the attributes which might
not exist and hence NPE. Try explicitly defining the access as FIELD in
your orm.xml"

So adding <access>FIELD</access> after the package, in the orm.xml file, all worked fine.