留言板

Using Dynamic Query to select rows with null values

Cipriano Teibão,修改在11 年前。

Using Dynamic Query to select rows with null values

New Member 帖子: 10 加入日期: 12-12-14 最近的帖子
Hi,

Can any one please help me out with a Dynamic Query?

I need to select all Journal Articles that have not expired or that have the expire date as null. I do now want to look at the Status, only at the date.


This is the sql query I used to test:
select * from `journalarticle`
where `journalarticle`.`groupId`=10180
and `journalarticle`.`companyId`=10154
and `journalarticle`.`structureId`='18406'

and `journalarticle`.`version` = (
	select max(ja.`version`) from `journalarticle` ja
    where ja.`articleId`=`journalarticle`.`articleId`)

and (`journalarticle`.`expirationDate`>NOW() or `journalarticle`.`expirationDate` is null)



And this is what I have so far

ClassLoader cl = PortalClassLoaderUtil.getClassLoader();
	
DynamicQuery subQuery = DynamicQueryFactoryUtil.forClass(JournalArticle.class, "articleSub", cl )
.add(PropertyFactoryUtil.forName("articleSub.articleId").eqProperty("articleParent.articleId"))
.setProjection(ProjectionFactoryUtil.max("articleSub.version"));

Junction junction = RestrictionsFactoryUtil.disjunction()
		.add(RestrictionsFactoryUtil.gt("expirationDate", now ))
		.add(RestrictionsFactoryUtil.eq("expirationDate", "0000-00-00 00:00:00" )); // <----------- This is where my code fails, i tried with that zeros but doesnt works

DynamicQuery query = DynamicQueryFactoryUtil.forClass(JournalArticle.class, "articleParent", cl )
.add(PropertyFactoryUtil.forName("version").eq(subQuery))
.add(PropertyFactoryUtil.forName("groupId").eq(groupId))
.add(PropertyFactoryUtil.forName("companyId").eq(companyId))
.add(PropertyFactoryUtil.forName("structureId").eq(newsTemplateId))
.add( junction )
.addOrder(OrderFactoryUtil.desc("modifiedDate"));
query.setLimit(from, to);

results = JournalArticleLocalServiceUtil.dynamicQuery(query);


I have searched the web for hours and cant find how to solve my problem, please help me out, I am new to Liferay and this is driving me crazy.


Best regards,
Cipriano
thumbnail
David H Nebinger,修改在11 年前。

RE: Using Dynamic Query to select rows with null values

Liferay Legend 帖子: 14918 加入日期: 06-9-2 最近的帖子
Why not use RestrictionsFactoryUtil.isNull("expirationDate")?
Cipriano Teibão,修改在11 年前。

RE: Using Dynamic Query to select rows with null values

New Member 帖子: 10 加入日期: 12-12-14 最近的帖子
That was it, thank you! emoticon