フォーラム

ホーム » Liferay Portal » English » 2. Using Liferay » General

構造的に表示 平面上に表示 ツリー上に表示
スレッド [ 前へ | 次へ ]
toggle
Jay Trivedi
Getting Records from 3 tables using Dynamic Query
2013/02/12 0:58
答え

Jay Trivedi

ランク: Regular Member

投稿: 107

参加年月日: 2012/11/23

最近の投稿

I have 3 tables user_, userTracker, userTrackerPath

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

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

All these are the fileds that i want.

I have written an sql query and it runs successfully for me, but i want the result using Dynamic query .

Here is my sql query.

1
2select 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 dynamic query with projections i am confused how will i be joining them.

 1
 2//Dynamic Query For User Class
 3
 4        DynamicQuery dynamicQuery_user = DynamicQueryFactoryUtil.forClass(User.class,PortalClassLoaderUtil.getClassLoader())
 5                .setProjection(ProjectionFactoryUtil.property("userId"))
 6                .setProjection(ProjectionFactoryUtil.property("firstName"))
 7                .setProjection(ProjectionFactoryUtil.property("lastName"))
 8                .setProjection(ProjectionFactoryUtil.property("loginIp"))
 9                .setProjection(ProjectionFactoryUtil.property("lastLoginIp"));
10
11        //Dynamic Query For User and UserTracker Class
12
13        DynamicQuery dynamicQuery_userTracker  = DynamicQueryFactoryUtil.forClass(UserTracker.class,PortalClassLoaderUtil.getClassLoader())
14                .setProjection(ProjectionFactoryUtil.property("modifiedDate"))
15                .setProjection(ProjectionFactoryUtil.property("remoteAddr"));
16
17
18        //Dynamic Query for UserTracker and UserTrackerPath
19
20        DynamicQuery dynamicQuery_userTrackerPath  = DynamicQueryFactoryUtil.forClass(UserTrackerPath.class,PortalClassLoaderUtil.getClassLoader())
21                .setProjection(ProjectionFactoryUtil.property("path_"))
22                .setProjection(ProjectionFactoryUtil.property("pathDate"));


but i am confused how will i be getting all this fields into single list.

I was successfully able to get User_ records by using

 1
 2 DynamicQuery q = DynamicQueryFactoryUtil.forClass(User.class, PortalClassLoaderUtil.getClassLoader())
 3                 .add(
 4                         PropertyFactoryUtil.forName("userId")
 5                         .in(
 6                                 DynamicQueryFactoryUtil.forClass(UserTracker.class, PortalClassLoaderUtil.getClassLoader())
 7                                 .setProjection(ProjectionFactoryUtil.property("userId"))
 8                                 .add(
 9                                         PropertyFactoryUtil.forName("userTrackerId").in
10                                         (
11                                                 DynamicQueryFactoryUtil.forClass(UserTrackerPath.class, PortalClassLoaderUtil.getClassLoader())
12                                                 .setProjection(ProjectionFactoryUtil.property("userTrackerId"))
13                                        )
14                                     )
15                            )
16                     );


but instead i need records from all the 3 tables...
Any Help ?? emoticon
Juhi Kumari
RE: Getting Records from 3 tables using Dynamic Query
2013/02/12 1:51
答え

Juhi Kumari

ランク: Expert

投稿: 347

参加年月日: 2011/12/12

最近の投稿

Hi,

For this requirement you can go for Custom query. I think using Dynamic Query we can use only one table.

Regards
Juhi
David H Nebinger
RE: Getting Records from 3 tables using Dynamic Query
2013/02/12 5:33
答え

David H Nebinger

Community Moderator

ランク: Liferay Legend

投稿: 11049

参加年月日: 2006/09/01

最近の投稿

Untrue. DQ can do joins with other tables.
Kowbathullah Gnaniyar
RE: Getting Records from 3 tables using Dynamic Query
2013/02/12 6:04
答え

Kowbathullah Gnaniyar

ランク: Liferay Master

投稿: 605

参加年月日: 2007/12/19

最近の投稿

Somewhere I have seen this code given by Jonas : But I didn't check. Please try this :

/* DynamicQuery dq0 = DynamicQueryFactoryUtil.forClass(JournalArticle.class, "journalarticle")
.setProjection(ProjectionFactoryUtil.property("resourcePrimKey"))
.add(PropertyFactoryUtil.forName("journalarticle.companyId").eqProperty("tagsasset.companyId"))
.add(PropertyFactoryUtil.forName("journalarticle.groupId").eqProperty("tagsasset.groupId"))
.add(PropertyFactoryUtil.forName("journalarticle.type").eq("article-content"));
DynamicQuery query = DynamicQueryFactoryUtil.forClass(TagsAsset.class, "tagsasset")
.add(PropertyFactoryUtil.forName("tagsasset.classPK").in(dq0))
.addOrder(OrderFactoryUtil.desc("tagsasset.viewCount"));*/
Jay Trivedi
RE: Getting Records from 3 tables using Dynamic Query
2013/02/12 6:32
答え

Jay Trivedi

ランク: Regular Member

投稿: 107

参加年月日: 2012/11/23

最近の投稿

Thanks a Lot , I will update success trails.
emoticon
Jay Trivedi
RE: Getting Records from 3 tables using Dynamic Query
2013/02/12 8:03
答え

Jay Trivedi

ランク: Regular Member

投稿: 107

参加年月日: 2012/11/23

最近の投稿

I tired Out an example to join 2 tables User_ and UserTracker as userId is common between them.

 1
 2DynamicQuery dq0 = DynamicQueryFactoryUtil.forClass(User.class, "user",PortalClassLoaderUtil.getClassLoader())
 3            .setProjection(ProjectionFactoryUtil.property("user.userId"))
 4            .add(PropertyFactoryUtil.forName("user.userId").eqProperty("usertracker.userId"));
 5            DynamicQuery query = DynamicQueryFactoryUtil.forClass(UserTracker.class, "usertracker",PortalClassLoaderUtil.getClassLoader())
 6            .add(PropertyFactoryUtil.forName("usertracker.userId").in(dq0));
 7            
 8            List temp = new ArrayList();
 9            temp.addAll(UserTrackerLocalServiceUtil.dynamicQuery(query));


it returns all fields of UserTracker but we also want fields of User Table, What if we want all the fields from both table to get displayed.
ex. Select * from User_ u , UserTracker ut where u.userId = ut.userId.

Any suggestions. emoticon
Jay Trivedi
RE: Getting Records from 3 tables using Dynamic Query
2013/02/12 8:06
答え

Jay Trivedi

ランク: Regular Member

投稿: 107

参加年月日: 2012/11/23

最近の投稿

Thanks David. It makes one side of coin clear keeping other side confused. is How?
How can we do that?

can you please project any views on dynamic query written above for two tables. to get all data of both tables.

Thanks Jay.
Jelmer Kuperus
RE: Getting Records from 3 tables using Dynamic Query
2013/02/12 12:43
答え

Jelmer Kuperus

ランク: Liferay Legend

投稿: 1192

参加年月日: 2010/03/10

最近の投稿

You cannot do joins using Liferay I wrote more on why this is in this thread

I don't believe it's possible to write this particular query using a dynamic query but you can probably do it via custom sql. Try something like this :

 1        SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");
 2
 3        Session session = null;
 4        try {
 5            session = sessionFactory.openSession();
 6
 7            SQLQuery query = session.createSQLQuery(
 8                "select {U.*}, {UT .*}, {UTP.*} " +
 9                "from User_ U, UserTracker UT, UserTrackerPath UTP " +
10                "where U.userId=UT.userId and UT.userTrackerId=UTP.userTrackerId;");
11
12            try {
13                query.addEntity("U", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.UserImpl"));
14                query.addEntity("UT", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.UserTrackerImpl"));
15                query.addEntity("UTP", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.UserTrackerPathImpl"));
16            } catch (ClassNotFoundException e) {
17                throw new IllegalStateException(e); // should never happen
18            }
19
20           // probably a list of object arrays containing a user, usertracker and usertrackerpath
21           List results = QueryUtil.list(query, sessionFactory.getDialect(),  QueryUtil.ALL_POS, QueryUtil.ALL_POS);
22
23        } finally {
24            if (session != null) {
25                sessionFactory.closeSession(session);
26            }
27        }
Jay Trivedi
RE: Getting Records from 3 tables using Dynamic Query
2013/02/12 23:11
答え

Jay Trivedi

ランク: Regular Member

投稿: 107

参加年月日: 2012/11/23

最近の投稿

Thanks Jelmer, but i am wondering since here there are three class i am going to use then what should i be writing in
SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");
Jelmer Kuperus
RE: Getting Records from 3 tables using Dynamic Query
2013/02/13 4:48
答え

Jelmer Kuperus

ランク: Liferay Legend

投稿: 1192

参加年月日: 2010/03/10

最近の投稿

but i am wondering since here there are three class i am going to use then what should i be writing in
SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");


Exactly that, liferaySessionFactory is just the name of the spring bean, its the same regardless of what entity you retrieve
Meera Prince
RE: Getting Records from 3 tables using Dynamic Query
2013/02/13 5:00
答え

Meera Prince

ランク: Liferay Legend

投稿: 1110

参加年月日: 2011/02/08

最近の投稿

HI Jay Trivedi
Go through the following link
http://www.liferaysavvy.com/2013/02/getting-data-from-multiple-tables-in.html


Regards,
Meera Prince
http://www.liferaysavvy.com/