EntityManager in JNDI - jpa

I need to use different databases in the same Web application, so I can't use a persistent.xml to define the target database. The database changes with the client which is connected.
I found this :
public EntityManager getEntityManager() {
if (em == null}
try{
em = (EntityManager)(new InitialContext())
.lookup("java:comp/ejb/EntityManager");
} catch (Exception e){};
}
return em;
}
at this URL : http://wiki.eclipse.org/EclipseLink/Examples/JPA/EMAPI
My question is now : how recording a EntityManager or a Persistence Unit in the JNDI of GlassFish ?

Suppose that my persistence.xml is:
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ctx-vendor" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
</persistence-unit>
</persistence>
We have two use case:
WAR application WEB-INF/web.xml file:
<persistence-context-ref>
<description>JNDI for lookup EntityManager</description>
<persistence-context-ref-name>persistence/ctx-vendor</persistence-context-ref-name>
<persistence-unit-name>ctx-vendor</persistence-unit-name>
<persistence-context-type>Transaction</persistence-context-type>
</persistence-context-ref>
EAR application META-INF/application.xml file:
<application xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd"
version="7">
<description>My Vendor System</description>
<display-name>vendor-ear</display-name>
<module>
<web>
<web-uri>vendor-rest.war</web-uri>
<context-root>/vendor-rest</context-root>
</web>
</module>
<module>
<ejb>vendor-service.jar</ejb>
</module>
<library-directory>lib</library-directory>
<persistence-context-ref>
<description>JNDI for lookup EntityManager</description>
<persistence-context-ref-name>persistence/ctx-vendor</persistence-context-ref-name>
<persistence-unit-name>ctx-vendor</persistence-unit-name>
<persistence-context-type>Transaction</persistence-context-type>
</persistence-context-ref>
</application>
Stateless Session Bean
#PersistenceContext(name = "persistence/ctx-vendor", unitName = "ctx-vendor")
public class BaseFacade
{ }
#Stateless
#Local(CatalogFacade.class)
public class CatalogFacadeImpl extends BaseFacade implements CatalogFacade
{
}
Tested in Glassfish 4.1

Related

sessionContext.getCallerPrincipal().getName() returns "anonymous"

I am new to EJB.
I am using Wildfly server.
I have session stateless Ejb as below.
#Stateless(name="PrintHandler")
#RunAs("TrustedExternalModule")
public class PrintHandlerBean extends ActivityBean implements PrintHandlerLocal {
The session ejb is packed to a server-ejb.jar and that jar is packed to .ear
I have created ejb-jar.xml and jboss-ejb3.xml inside META-INF folder in server-ejb.jar as below.
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
<enterprise-beans>
<session>
<ejb-name>PrintHandler</ejb-name>
<security-identity>
<run-as>
<role-name>TrustedExternalModule</role-name>
</run-as>
</security-identity>
</session>
</enterprise-beans>
</ejb-jar>
<?xml version="1.1" encoding="UTF-8"?>
<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:s="urn:security:1.1"
xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd http://java.sun.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-spec-2_0.xsd"
version="3.1"
impl-version="2.0">
<jboss:enterprise-beans>
<session>
<ejb-name>PrintHandler</ejb-name>
<session-type>Stateless</session-type>
<security-identity>
<run-as>
<role-name>TrustedExternalModule</role-name>
</run-as>
</security-identity>
</session>
</jboss:enterprise-beans>
<assembly-descriptor>
<s:security>
<ejb-name>PrintHandler</ejb-name>
<s:security-domain>other</s:security-domain>
<s:run-as-principal>TESTCONNECT</s:run-as-principal>
</s:security>
</assembly-descriptor>
</jboss:ejb-jar>
I am injecting SessionContext annotated with Resource in a non ejb class as below.
public abstract class AbstractBean {
protected AbstractBean() {
log = LogMgr.getFrameworkLogger();
clsLog = LogMgr.getClassLogger(FndAbstractBean.class);
if(clsLog.debug) {
clsLog.debug("Created bean [&1]", getClass().getName());
}
}
**#Resource
protected SessionContext sessionContext;**
But when I am calling String user = sessionContext.getCallerPrincipal().getName();
it is returning "anonymous" always.
How can I solve this.
I want to get caller principal as TESTCONNECT.
Hello, this seems to be an expected behavior. The only workaround I found would be to use Interceptor,so then you can propagate the information actually. Interceptors is explained here

JPA not generating ddl files

I'm setting up a JavaEE Webapplication and want to generate ddl files so I can see what they look like
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence"
version="2.1">
<persistence-unit name="contentAggregatorPU">
<class>contentAggregator.model.Item</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="contentAggregatorCreate.ddl"/>
<property name="javax.persistence.schema-generation.scripts.drop-target" value="contentAggregatorDrop.ddl"/>
</properties>
</persistence-unit>
</persistence>
It just does not generate the files.
Honestly I never used the mechanism. While it seems to work for my local-resource minimal JPA playground it gave me errors when attempting to deploy to wildfly. I've always used a small utility class for generating a schema.
public class SchemaTranslator {
public static void main(String[] args) throws IOException {
Class<?>[] entityClasses = {
Class1.class,
Class2.class,
};
MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder()
.applySetting("hibernate.hbm2ddl.auto", "create")
.applySetting("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect")
// .applySetting("hibernate.physical_naming_strategy", "package.MyImprovedNamingStrategy")
.build()
);
// [...] adding annotated classes to metadata here...
for (Class<?> clazz : entityClasses)
metadata.addAnnotatedClass(clazz);
EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.STDOUT, TargetType.SCRIPT);
SchemaExport export = new SchemaExport()
// .setHaltOnError( haltOnError )
.setOutputFile("db-schema.sql")
.setDelimiter(";");
export.create(targetTypes, (MetadataImplementor) metadata.buildMetadata());
}
}

Spring Jpa Repositories - Orm.xml

I'm working with Spring Jpa Repositories.
I don't want to define my "named queries" in java classes (entities or repositories). I would like to define my "named queries" in different xml files (like orm.xml).
.... XML FILE ....
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd">
<named-query name="MyEntity.findByField1">
<query>
<![CDATA[
SELECT m
FROM MyEntity m
WHERE
m.field1 = :field1
]]>
</query>
</named-query>
</entity-mappings>
.... REPOSITORY CODE ....
public interface MyEntityRepository extends JpaRepository<MyEntity, String>
{
public Optional<MyEntity> findByField1(#Param("field1") String field1);
}
But my repositories are not finding the xml files with my named queries. How do I configure spring-data-jpa to use these xml files? I do not have a persistence.xml.
Thanks!
Since your MyEntity already has field1 property inside using findByField1 as the Repository method name might be confusing. Change the orm.xml as following and place it inside META-INF/orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd">
<named-query name="MyEntity.findByField1OrmXml">
<query>
<![CDATA[
SELECT m
FROM MyEntity m
WHERE
m.field1 = :field1
]]>
</query>
</named-query>
</entity-mappings>
And change the Repository as following
public interface MyEntityRepository extends JpaRepository<MyEntity, String>
{
public Optional<MyEntity> findByField1OrmXml(#Param("field1") String field1);
}

How to use EJB 3 with persistence unit on a JBoss AS?

I have a JBoss AS, and on this server there is a standalone.xml file where there several properties, there are my datasources too, so how combine the datasources in the standalone.xml file with a persistence unit that I want to add to an EJB ?
Just add <jta-data-source>java:/ExampleDS</jta-data-source> providing your datasource jndi-name to the persistence.xml.
Example of Datasource:
<datasource jndi-name="java:/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
Example of persistence.xml referencing datasource ExampleDS:
<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="example">
<jta-data-source>java:/ExampleDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
Example injecting Persistence Unit in your EJB3:
#Stateless
public class MyEJB {
#PersistenceContext(unitName="example") protected EntityManager entityManager;
public void createEmployee(String fName, String lName) {
Employee employee = new Employee();
employee.setFirstName(fName);
employee.setLastName(lName);
entityManager.persist(employee);
}
...
}

What is the cause of error "No META-INF/persistence.xml was found in classpath"?

Included all necesaary toplinks lib still getting following error on running example.java
using netbeans 6.8
No META-INF/persistence.xml was found in classpath.
Persistence.xml
<?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="MatdaanPU" transaction-type="JTA">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<jta-data-source>matdaan</jta-data-source>
<class>EntityBeans.Trial</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="toplink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Example.java
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Example {
public Example() {
EntityManagerFactory emf=Persistence.createEntityManagerFactory("MatdaanPU");
EntityManager em=emf.createEntityManager();
List results = em.createNamedQuery("Trial.findById")
.setParameter("id", "1")
.getResultList();
System.out.println("HI");
System.out.println(results.get(1));
}
public static void main(String args[]){
Example e=new Example();
}
}
Directory structure
Root
|
|
|----src
| |
| |----conf
| |
| |-----Manifest.MF
| |-----Persistence.xml
Put your persistence.xml in src/META-INF folder.
Furthermore letter case could matter. Rename Persistence.xml to persistence.xml