EJB 3 PERSISTENCE NO GLASSFISH

O suporte à JPA no glassfish é muito simples de ser utilizado e muito semelhante à outras arquiteturas. Como qualidades podemos citar o grande suporte de informações que podem ser encontradas no site da Sun MicroSystems.

Vamos descrever em cima do post abaixo:

https://glassfish.dev.java.net/javaee5/persistence/entity-persistence-support.html

No exemplo citamos as formas de utolizar TopLink(TM) Essetials, com contribuições da Oracle.

Gerando um pacote JPA no container JavaEE:

Entity bean classes, like any other POJO classes, can be packaged along with the component classes that use them or separately as a library. They are allowed to be packaged in the following ways:

1. In an ejb-jar file along with other EJB classes.
2. In WEB-INF/classes directory along with other web application classes.
3. In any of the jar files directly located in WEB-INF/lib directory.
4. In any of the utility jar files in an ear file including jar files that belong to lib directory in an ear file
5. In an application client jar file.

Note: a rar file does not contain entity bean classes.

Each of the above jar files or directory needs to contain a META-INF/persistence.xml. The schema which defines the structure of this XML document is available here.


Example of a simple persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name ="pu1">
<jta-data-source>jdbc/DataSource1</jta-data-source>
</persistence-unit>
</persistence>


Criando o pacote na plataforma Java SE:

The following code creates EntityManagerFactory, EntityManager, starts a transaction, persists a new Customer, commits the transaction, and then runs a Java Persistence query:

// Create EntityManagerFactory for a persistence unit called em1.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu1");

// create EntityManager
EntityManager em = emf.createEntityManager();

// Get a transaction instance and start the transaction
EntityTransaction tx = em.getTransaction();
tx.begin();

// Create a new customer and persist it.
Customer c = new Customer();
c.setName("Joe Smith");
em.persist(c);

// Commit the transaction
tx.commit();

// run a Java Persistence query selecting a customer by name
String queryString = "SELECT c FROM Customer c WHERE c.name = :name";
Query query = em.createQuery(queryString);
query.setParameter("name", "Joe Smith");
List result = query.getResultList();
System.out.println("Java Persistence query " + queryString + " returns " + result);

The persistence.xml file should list all persistent classes from the application domain, e.g. entity.Customer, etc. (substitute " <...>" text with your actual values for jdbc properties).
Provider class name can be specified either in the
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="pu1">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider <!-- All persistence classes must be listed -->
<class>entity.Customer</class>
<class>entity.Order</class>
<class>entity.Item</class>
<properties>
<!-- Provider-specific connection properties -->
<property name="toplink.jdbc.driver" value="<database driver>"/>
<property name="toplink.jdbc.url" value="<database url>"/>
<property name="toplink.jdbc.user" value="<user>"/>
<property name="toplink.jdbc.password" value="<password>"/>
<!-- Provider-specific settings -->
<property name="toplink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>

Note: If you use build 03/02 or later, you do not need to specify transaction-type="RESOURCE_LOCAL" in Java SE.

To execute the example, add your classes and META-INF/persistence.xml to the classpath. Then run:

java -javaagent:${glassfish.home}/lib/toplink-essentials-agent.jar client.Client

Note: toplink-essentials.jar from {glassfish.home}/lib will be added automatically to the classpath. It contains both, the API and the implementation classes.



Recomenda-se o uso de JPA Tools para auxílio no desenvolvimento das Entidades:

Nenhum comentário: