views:

73

answers:

2

I'm using JBoss 4.2.3 and I deployed two ears called triad-1.0.ear and reportservices-1.0.ear, the thing is that I want to use the entity manager of the project triad in the project reportservices. This is the architecture JBoss follows:

triad-1.0.ear:
             triad-core-1.0.jar:
                                META-INF:
                                         MANIFEST.MF
                                         components.xml
                                         ejb-jar.xml
                                         jboss.xml 
                                         persistence.xml    
reportservices-1.0.ear:
              reportservices-core-1.0.jar:
                                          META-INF:
                                                  MANIFEST.MF
                                                  components.xml
                                                  ejb-jar.xml
                                                  jboss.xml
                                                  persistence.xml

this is my attempt to make the entitymanager global between ear in the persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
     Attention: Generated code! Do not modify by hand!
     Generated by: persistence.xml.vsl in andromda-ejb3-cartridge.
-->
<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_1_0.xsd"
    version="1.0">
    <persistence-unit name="triad">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/jdbc/triad</jta-data-source>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
            <property name="jboss.entity.manager.jndi.name" value="java:/triadFactory"/>
            <property name="jboss.entity.manager.factory.jndi.name" value="java:/triadFactory"/>
        </properties>
    </persistence-unit>
</persistence>
A: 

I'm sorry but the question doesn't make much sense in my opinion (I don't even get what you mean by "make public the persistence.xml"). Just in case, it is possible:

  • to use the same datasource in several persistence units.
  • to (re)use a persistence unit in several applications (provided the packaging make it possible).

My suggestion would be to explain what you would like to do in plain english (forget the technical details for now).

Pascal Thivent
hi, and thank's for the aswer, I just wanted to get the entitymanager from a project call "triad" into a project call "reportservices" to execute queries, the thing is that the project's are in different ears. I tried to configure the persistent.xml. I'm know that's is injection but i don't know how to do it on jboss.Sorry for my english but is not my first language
Jorge
An EntityManager is not meant to be shared at all, it's typically created for just one transaction. Do you mean that you want to share the persistence unit? If so, can you explain WHY you want to do this?
Mike Baranczak
Hi, yes i want to share the persistence unit, because i need to execute queries to the entities in an ear from another ear in the same jboss
Jorge
@Jorge then just share the persistence unit as suggested (package the entities and the persistence.xml in a jar and use it in both EARs).
Pascal Thivent
The real problem is that i need to use two database schema in a single persistence unit. That's because i am using jasperreport 3.7.2 and i want to use ejbl connection for execute EJBQL queries and no matter where the schema is, i need to get the entity manager for pass as a parameter to the report
Jorge
@Jorge: If you need to access two databases, you should use two persistence units (and inject two entity managers, one per persistence unit).
Pascal Thivent
Hi and thanks again for help me in this problem, what you say was my first solution but when i did it i have an error in the jboss console while deployment that say "cannot find the Persistence unit" and the entitymanager was null. Maybe if a show how i inject the persistence unit you could help to see where i'm doing wrong
Jorge
A: 

Hi, i finally solved mi problem with the injection, you need to set this properties in the persistence.xml and check in jboss jmx console, in the option of jdni view if the injection was correct the properties here's an example of the persistence.xml

<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_1_0.xsd"
version="1.0">
<persistence-unit name="example">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jdbc/example</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
<property name="jboss.entity.manager.jndi.name" value="java:/example"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/example"/>
</properties>
</persistence-unit>
</persistence>

the properties that allowing the injection are "jboss.entity.manager.jndi.name" and "jboss.entity.manager.factory.jndi.name"

notes the data have the same name that the data source but called by his jdni name sets in the xml of the datasource project.

Jorge