Retrieving data from 3 tables using dynamic query

I have 3 tables user_, userTracker, userTrackerPath

user_ has userId as Pk. which is Fk in userTracker userTracker has UserTrackerId as pk, which is Fk in userTrackerPath and userTrackerPath has userTrackerPathId as Pk.

user_ tables have fields firstName, LastName, loginIp, lastLoginIp userTracker has fields remoteAddr, remoteHost userTrackerPath has fields path_, pathDate

These are all the ones that I need.

I wrote a sql query and it works for me successfully, but I want to get the result using a dynamic query.

Here is my sql query.

select concat(U.firstName," ",U.lastName) as     FullName,U.loginIp,U.lastLoginIp,UT.remoteAddr,substring(UT.modifiedDate,1,10) as Date,UTP.path_ from demo.User_ U, demo.UserTracker UT, demo.UserTrackerPath UTP where ((U.userId=UT.userId) and (UT.userTrackerId=UTP.userTrackerId));

      

I wrote a dynamic query with projections, I'm confused as to how I will attach them.

//Dynamic Query For User Class

        DynamicQuery dynamicQuery_user = DynamicQueryFactoryUtil.forClass(User.class,PortalClassLoaderUtil.getClassLoader())
                .setProjection(ProjectionFactoryUtil.property("userId"))
                .setProjection(ProjectionFactoryUtil.property("firstName"))
                .setProjection(ProjectionFactoryUtil.property("lastName"))
                .setProjection(ProjectionFactoryUtil.property("loginIp"))
                .setProjection(ProjectionFactoryUtil.property("lastLoginIp"));

        //Dynamic Query For User and UserTracker Class

        DynamicQuery dynamicQuery_userTracker  = DynamicQueryFactoryUtil.forClass(UserTracker.class,PortalClassLoaderUtil.getClassLoader())
                .setProjection(ProjectionFactoryUtil.property("modifiedDate"))
                .setProjection(ProjectionFactoryUtil.property("remoteAddr"));


        //Dynamic Query for UserTracker and UserTrackerPath

        DynamicQuery dynamicQuery_userTrackerPath  = DynamicQueryFactoryUtil.forClass(UserTrackerPath.class,PortalClassLoaderUtil.getClassLoader())
                .setProjection(ProjectionFactoryUtil.property("path_"))
                .setProjection(ProjectionFactoryUtil.property("pathDate"));

      

And I tried ..

        dynamicQuery_userTracker.add(PropertyFactoryUtil.forName("userId").in(dynamicQuery_user));

        dynamicQuery_userTrackerPath.add(PropertyFactoryUtil.forName("userTrackerId").in(dynamicQuery_userTracker));

      

I know my method is wrong. Any kinds or suggestions.

Thank.

+3


source to share


1 answer


Jay I think you cannot join the DynamicQuery API. You can make subqueries using the in and notin methods.



+1


source







All Articles