3 Replies Latest reply: Mar 19, 2013 12:57 PM by Mr Oracle RSS

    How to Inject an adf Entity into an EJB

    Mr Oracle
      I am using JDev 11.1.1.6 and have an ADF Fusion project.
      I have created all the Entities (from tables) (and Views) which are supposed to be Container Managed.
      All my UI is based on Data Controls.
      But my EJBs use a lot of JDBC and "hard-coded" queries, updates and inserts.
      Most of these are just inserts into tables the same as the Entities. I want to remove the SQL and use the Container Managed Entities as much as possible. (I do not really want to mix the views - this should be done with entities only)

      How do I inject the entity? Or do I have to reinvent the wheel?

      I have searched the web - but while I can find thousands of contradicting information on the Entity Manager I cannot find much on the entities.
      It is not a class - so it cannot be included - I cannot find a resource annotation for them - and what I do find - says I have to create all new beans for the entities.
      Can someone show me how to do this?

      @Resource private EntityManager em;
      ...

      /**
      * @return
      */
      public EntityManager getEntityManger() {
      return this.em;
      }
      /**
      * @param docId
      * @param userName
      * @param actionCode
      * @param docType
      */
      public void createAuditRecord(String docId, String userName, String actionCode, String docType) {
      String insertLogEntry = "insert into egov_owner.system_audit_log (id, action_date, username, document_id, la_code,ldt_code) values (egov_owner.syslog_seq.nextval, sysdate, ?,?,?,?)"; *<- replace with entity*
      ...
        • 1. Re: How to Inject an adf Entity into an EJB
          Frank Nimphius-Oracle
          Hi,

          don't understand your question.
          @Entity
          @NamedQueries({ @NamedQuery(name = "Employees.findAll", query = "select o from Employees o") })
          public class Employees implements Serializable {
              private static final long serialVersionUID = 5771257060871346006L;
              @Column(name = "COMMISSION_PCT")
              private Integer commissionPct;
              @Column(nullable = false, unique = true, length = 25)
              private String email;
              @Id
              @Column(name = "EMPLOYEE_ID", nullable = false)
              private Integer employeeId;
              @Column(name = "FIRST_NAME", length = 20)
              private String firstName;
              @Temporal(TemporalType.DATE)
              @Column(name = "HIRE_DATE", nullable = false)
              private Date hireDate;
              @Column(name = "JOB_ID", nullable = false, length = 10)
              private String jobId;
              @Column(name = "LAST_NAME", nullable = false, length = 25)
              private String lastName;
              @Column(name = "MANAGER_ID")
              private Integer managerId;
              @Column(name = "PHONE_NUMBER", length = 20)
              private String phoneNumber;
              private Integer salary;
              @XmlTransient
              @ManyToOne
              @JoinColumn(name = "DEPARTMENT_ID")
          wouldn't this be enough entity annotations? What do you mean by "injecting entities" and "reinventing the wheel"? So far your EJB model doen't use entities but a questionable design, so what would be the wheel to reinvent? Don't know if your project uses TopLink or EclipseLink, but dependent on these you will have to configure the persistence.xml file with the entity references
            <persistence-unit name="EjbModel">
              <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
              <jta-data-source>java:/app/jdbc/jdbc/hrconnDS</jta-data-source>
              <class>adf.sample.cdi.ejb.Departments</class>
              <class>adf.sample.cdi.ejb.Employees</class>
              <properties>
                <property name="eclipselink.target-server" value="WebLogic_10"/>
                <property name="javax.persistence.jtaDataSource" value="java:/app/jdbc/jdbc/hrconnDS"/>
              </properties>
            </persistence-unit>
          </persistence>
          What i would do is to create a new EJB project and then compare your project with the new project for which settings need to be done.

          Just for the records: There is no such thing than ADF entities
          Frank
          • 2. Re: How to Inject an adf Entity into an EJB
            Mr Oracle
            Sorry Frank,

            I have an Fusion Project that is web based and I use data controls for the UI.

            I have the entities and views generated from the tables. This is BC4J. (the Model layer)

            I wanted to use these entities in my EJBs to avoid a lot of SQL. I was looking at JPA Persistance.

            Is there a better way? This is what I mean - to use my data source I can use annotations (@Resource).

            Is there a way to get access to the container managed entities.

            90% of the SQL in my EJBs are just inserts and updates - but I had to use JDBC and code the SQL. I want to replace the code with the model entities to avoid writing the SQL.
            And take full advantage of the model.

            Is this possible without JPA? and how would one go about doing this. Or would I use the View Objects instead?
            I really did not want to go back and create all the java classes (hundreds of tables) - tried a test run on one by generating the java class (IMPL) but I could never get it to work.

            and all the info on the web about JPA was of little or no help - None of them agreed and none of the solutions worked.

            Edited by: Mr Oracle on Mar 18, 2013 12:23 AM
            • 3. Re: How to Inject an adf Entity into an EJB
              Mr Oracle
              After several days of trial and error, I have found a way to use the Entity Objects.

              There are several steps required.
              To Use the BC4J is not possible - need to change to the JPA model.
              Fortunately this is not difficult in JDeveloper. (I am using 11.1.1.6 and WLS 10.3.5)

              Step 1) Create the JPA Persistance Unit.

              New -> TopLink/JPA -> JPA Persistance Unit
              In the wizard add the name (any name will work - I used "zitasPU")
              In the jpa datasource enter the data source - this MUST be the exact same name as the JNDI name in the Weblogic Server - I used "jdbc/zitasDS"
              Once this is done - we will create the JPA Enitiy Classes and come back to the Persistance Unit and add them (this is critical).
              I use a global Data Source in Weblogic (also critical to know) as the global access does not need any java:/app/ just the JNDI Name like below.
              You will also need to edit the version - it uses version="1.0" - I had to manually change it to version="2.0" to get it to work - it will flag this when you look at the source.

              <?xml version="1.0" encoding="US-ASCII" ?>
              <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="2.0">
              <persistence-unit name="zitasPU">
              <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
              <jta-data-source>jdbc/zitasDS</jta-data-source>
              <class>model.sysaudit.entities.SystemAuditLog</class>
              <class>model.sysaudit.entities.TaxpayerActionHistories</class>
              <properties>
              <property name="eclipselink.target-server" value="WebLogic_10"/>
              <property name="javax.persistence.jtaDataSource" value="*jdbc/zitasDS*"/>
              </properties>
              </persistence-unit>
              </persistence>

              Step 2) Create the JPA Enitiy Class.

              New -> TopLink/JPA -> Entities from Tables
              Follow the wizard for the tables you want to use. (I used 2 tables SystemAuditLog and TaxpayerActionHistories).
              Check the accessors box (and if you use sequences - maybe the create box)

              This will result in the addition of 2 java classes named for the tables.
              I use a naming structure inside the model - so these classes belong in the "entities" (model.sysaudit.entities).
              That is all that is required for this step.

              Step 3) Add the classes to the Persistance Unit

              Open the Persistance Unit.xml
              In the Overview tab you shoud see the new Persistance Unit. Double click the unit to open it.
              Go to the General location and add the new classes to the Mapped Classes (green +).

              Step 4)
              add the annotation to the EJB.

              @PersistenceContext(unitName = "zitasPU")
              private EntityManager em;

              The rest is easy - the Entity is used just like any other class.

              SystemAuditLog sysLog = new SystemAuditLog();
              sysLog.setId(Long.parseLong(getSeqNo.findSequenceNo("syslog_seq")));
                        .
                        .
              em.persist(sysLog);

              This allows me to use the Entities in my EJBs without having to write SQL and use JDBC connections.
              The sequence numbers for the primary keys is a simple class (had to use JDBC here - but very limited)
              This could be placed inside the create function of the Entity as well.
              I use data controls for my Web UI - but complex processing and logic is required - so EJBs handle this part.

              Edited by: Mr Oracle on Mar 19, 2013 10:57 AM