Dynamically changing query in view object in Oracle ADF

I have also written in my past post that how can you change the query my old post to change query . Here is another way.

Sometime requirements can be dynamically change SQL query where clause in View Object. I found on internet .so for the reference, i am sharing.

To use this functionality, you need to override buildWhereClause(StringBuffer sqlBuffer, int noBindVars) method in view object implementation. You use sqlBuffer parameter in this method (which contains SQL query where in StringBuffer) to change SQL in correlation to you needs. Just replace, remove, add string in this parameter. Just remember to use same bind variables names which will be use in VO SQL call (generic bind variables names are named like :vc_temp_1, :vc_temp_2,…).

    @Override  
        protected boolean buildWhereClause(StringBuffer sqlBuffer, int noBindVars) {  
      
            //call super and see if there is where clause.  
            boolean hasWhereClause = super.buildWhereClause(sqlBuffer, noBindVars);  
      
            if (hasWhereClause) {   
                //modify existing where query.  
            }  
            else {   
                //or if you add where clause then return true;  
                hasWhereClause = false;   
      
            }  
      
            return hasWhereClause;  
        }  


Different Hibernate object states and their lifecycle in Hibernate | Techartifact

There are 3 hibernate object state

1.) Persistent– Persistent object and collections are short lived single threaded objects, which store the persistence state. These objects synchronize their state with database depending on your flush strategy(i.e auto flush where as soon as setXXX() method is called or an item is removed from a set,list etc or define your own synchronization points with session.flush().transaction.commit() calls.) If you remove an item from persistence collections like a Set, it will be removed from database either immediately or when flush() or commit() is called depending on your flush strategy. They are plain old java objects(POJO) and are currently associated with session. As soon as the associated session is closed , persistence objects become detached objects and are free to use directly as data transfer objects in any application layer like business Layers, presentation layer etc.

2.) Detached – These objects and collection are instances of persistence objects that were associated with a session but currently not with associated with session. These objects can be freely used as Data Transfer Objects without having any impact on your database .Detached objects can be later on attached to another session by calling methods like session.update(), session.saveOrUpdate() etc and become persistence objects.

3.) Transient – These objects and collection are instance of persistence object that were never associated with session.These objects can be freely used as Data transfer objects without having any impact on your database. Transient objects become persistent objects when associated to session by calling session.save(), session.persist() etc.

4.) Removed State -A previously persistent object that is deleted from the database session.delete(account).Java instance may still exist, but it is ignored by Hibernate -Any changes made to the object are not saved to the database Picked up for garbage collection once it falls out
of scope
• Hibernate does not null-out the in-memory object

get object of ApplicationModule in managed bean in ADF | Techartifact

Requirment- Need an object of Application module in managed bean

Sometime we required to get an object of ApplicationModule in managed bean.May be we want to play with some VO object which we can get from
ApplicationModuleImpl Object only.

There are two ways.

First way-

        FacesContext context = FacesContext.getCurrentInstance();
        BindingContext bindingContext = BindingContext.getCurrent();
        DCDataControl dc  = bindingContext.findDataControl("AppModuleAMDataControl"); // Name of application module in datacontrolBinding.cpx
        AppModuleAMImpl appM = (AppModuleAMImpl)dc.getDataProvider();
     

Above AppModuleAMDataControl is name of application module in datacontrolBinding.cpx,open datacontrol and check the ID in tag. See below for reference.

 <BC4JDataControl id="AppModuleAMDataControl" Package="oper.model.module"
                     FactoryClass="oracle.adf.model.bc4j.DataControlFactoryImpl" SupportsTransactions="true"
                     SupportsFindMode="true" SupportsRangesize="true" SupportsResetState="true"
                     SupportsSortCollection="true" Configuration="AppModuleAMLocalWeb" syncMode="Immediate"
                     xmlns="http://xmlns.oracle.com/adfm/datacontrol"/>

Now you got the application module you can get the object of any ViewObjectImpl.

Second Way-

You can get the object of applicationModuleImpl by writing this line-

AppModuleAMImpl am = (AppModuleAMImpl)resolvElDC(AppModuleAMDataControl)

resolvElDC method is described below


private Object resolvElDC(String data) {
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();
        ValueExpression valueExp =
            elFactory.createValueExpression(elContext, #{data. + data + .dataProvider}, Object.class);
        return valueExp.getValue(elContext);
    }

Happy coding with Techartifact . 🙂