view page history | view next version >>
|
Search XAP 8.0
Offline Documentation
Download latest offline documentation
|
Summary: Using JPA with GigaSpaces.
OverviewThe Java Persistency API (JPA) is a Java programming language framework managing relational data in applications using Java Platform. ConfigurationStandard JPA configurationHere'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 SpringIt is possible to inject either an EntityManager or EntityManager factory using Spring. <?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 EnhancementJPA entities are monitored in runtime and therefore requires enhancement. JPA EntitiesThere are a few restrictions when creating a JPA entity for usage with GigaSpaces:
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 |
