To access data from external resources, we will set up connection to the datasource, create an Entity and manage it via an EJB. This will be typically helpful when you have an existing database or your database is populated by an external system, and you want to obtain and manipulate the data from the code of your LSPS Application.
Make sure you have the following ready:
To configure a data source, so it is accessible from the SDK Embedded Server, and you can use its entities do the following:
In <YOUR_APP>-embedded/conf/conf/openejb.xml
, define the data source.
For example:
@Entity @Table(name = "ORDERS_USER") public class User { @Id private Integer id; @Column(name = "FIRST_NAME") private String firstName; public Integer getId() { return id; } public String getFirstName() { return firstName; } }
<YOUR_AP>-ejb/src/main/resources/resources/META-INF/persistence.xml
and define the persistence unit with the external data source. <?xml version="1.0" encoding="UTF-8"?> <persistence 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_2_0.xsd" version="2.0"> <persistence-unit name="<UNIT_NAME>" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source><DATASOURCE_ID></jta-data-source> <mapping-file>META-INF/<PROJECT_NAME>-entities.xml</mapping-file> <validation-mode>NONE</validation-mode> <properties> <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" /> <property name="net.sf.ehcache.configurationResourceName" value="META-INF/lsps-ehcache.xml" /> <!-- JBoss specific parameters --> <property name="jboss.as.jpa.providerModule" value="application" /> <property name="jboss.as.jpa.adapterClass" value="com.whitestein.lsps.common.hibernate.LSPSPersistenceProviderAdaptor" /> </properties> </persistence-unit> </persistence>
<?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_2_0.xsd" version="2.0"> <entity class="org.eko.orderusersapp.entity.User" /> </entity-mappings>
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" name="<UNIT_NAME>" updateCheck="false" monitoring="off" dynamicConfig="false"> <cacheManagerPeerProviderFactory class="com.whitestein.lsps.common.ehcache.JmsCacheManagerPeerProviderFactory"/> <defaultCache eternal="true" maxElementsInMemory="0" overflowToDisk="false" > <cacheEventListenerFactory class="com.whitestein.lsps.common.ehcache.JmsCacheReplicatorFactory"/> </defaultCache> <cache name="org.hibernate.cache.internal.StandardQueryCache" maxBytesLocalHeap="10000000" eternal="true" overflowToDisk="false"> <cacheEventListenerFactory class="com.whitestein.lsps.common.ehcache.JmsCacheReplicatorFactory"/> </cache> <cache name="org.hibernate.cache.spi.UpdateTimestampsCache" maxElementsInMemory="1000" eternal="true" overflowToDisk="false"> <cacheEventListenerFactory class="com.whitestein.lsps.common.ehcache.JmsCacheReplicatorFactory"/> </cache> </ehcache>
If you want to use the entity via an EJB in LSPS modules, do the following:
@Stateless @PermitAll @Interceptors({ LspsFunctionInterceptor.class }) public class UserBean { @PersistenceContext(unitName = "user-unit") private EntityManager em; public String getUsers(ExecutionContext context) { User user = em.find(User.class, 1); System.out.println(user.getFirstName()); return user.getFirstName(); } }
@EJB private UserBean userBean; @Override protected void registerCustomComponents() { register(userBean, UserBean.class); }
Create a function definition file with a function that will use the keyword native to call the EJB method.