XAP8 == "XAP9NET" || XAP8 == "XAP9" || XAP8 == "XAP9NET")

JPA Support (non-jdbc)

You are viewing an old version (v. 1) of this page.
The latest version is v. 56, last edited on Feb 03, 2012 (view differences | )
view page history | view next version >>


Search XAP 8.0
Searching XAP 8.0 Documentation
Browse XAP 8.0
Offline Documentation

Download latest offline documentation
in HTML format:

xap-8.0-documentation.zip (15MB)

                                                              

Overview

The Java Persistency API (JPA) is a Java programming language framework managing relational data in applications using Java Platform.
GigaSpaces JPA implementation is based on OpenJPA 2.0.0.

Configuration

Standard JPA configuration

Here's an example of a persistence.xml configuration file:

<persistence-unit name="gigaspaces" transaction-type="RESOURCE_LOCAL">
	<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <class>org.openspaces.objects.Owner</class>
	<properties>
           <property name="openjpa.BrokerFactory" value="abstractstore"/>
           <property name="openjpa.abstractstore.AbstractStoreManager" value="org.openspaces.jpa.StoreManager"/>
           <property name="openjpa.LockManager" value="none"/>
    	   <property name="openjpa.ConnectionURL" value="/./jpaSpace"/>
	</properties>	
</persistence-unit>

An alternative for using the "ConnectionURL" property is to specify a space instance when creating the entity manager factory through the "ConnectionFactory" property:

GigaSpace gigaSpace;
Properties properties = new Properties();
properties.put("ConnectionFactory", gigaSpace.getSpace());
EntityManagerFactory emf = Persistence.createEntityManagerFactory("gs_store", properties);

Configuration using Spring

It is possible to inject either an EntityManager or EntityManager factory using Spring.
In the following example we'll see how to inject a space and an EntityManagerFactory.
In the following Spring xml configuration file we'll declare a space, an entity manager factory, a transaction manager and a JPA service bean:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:os-core="http://www.openspaces.org/schema/core"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.openspaces.org/schema/core http://www.openspaces.org/schema/core/openspaces-core.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
       
	<!-- space definition -->
        <os-core:space id="space" url="/./jpaSpace" lookup-groups="test"/>

        <!-- entity manager factory definition -->
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="spring"/>
		<property name="jpaVendorAdapter">
			<bean class="org.openspaces.jpa.OpenSpacesJpaVendorAdapter">
				<property name="space" ref="space"/>
			</bean>
		</property>
	</bean>	

        <!-- transaction manager definition -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>		

        <!-- support annotations -->
	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />	
	<context:annotation-config/>	
	<tx:annotation-driven transaction-manager="transactionManager"/>

        <!-- our JPA service definition -->
	<bean id="jpaService" class="org.openspaces.jpa.JpaService" />
</beans>

Here's our JpaService implementation:

@Repository
@Transactional
public class JpaService {
    @PersistenceContext
    private EntityManager em;
    
    public JpaService() {
    }
               
    @Transactional
    public void persistObject() {            
        em.persist(...);            
    }
}

Entities Enhancement

JPA entities are monitored in runtime and therefore requires enhancement.
Information regarding how to enhance your entities can be found on OpenJPA's Entity Enhancement page.

JPA Entities

There are a few restrictions when creating a JPA entity for usage with GigaSpaces:

  1. JPA annotations are only allowed for getters (not fields).
  2. @Id annotated getter should also have a @SpaceId annotation.
  3. Other GigaSpaces annotations such as @SpaceRouting are allowed.

Here's an example of a JPA Owner entity:

@Entity
public class Owner {
    //
    private Integer id;
    private String name;
    private List<Pet> pets;    
    //    
    public Owner() {
    }
    public Owner(Integer id, String name, List<Pet> pets) {
        super();
        this.id = id;
        this.name = name;
        this.pets = pets;
    }
    //   
    @Id
    @SpaceId
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @Basic
    @SpaceRouting
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @OneToMany(cascade = CascadeType.ALL)
    public List<Pet> getPets() {
        return pets;
    }
    public void setPets(List<Pet> pets) {
        this.pets = pets;
    }
}

JPA Query Language (JPQL)

Unsupported features of JPA

Labels

 
(None)