Skip to content

radostyle/liferay-fluent-query

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation


By using the Liferay service builder, you don't have to write many of the queries you might normally have to write, but there are still many times you need to write your own queries.

The code that is generated by the Service builder, while easy for a computer to write, is more verbose than necessary.  Writing your own queries in that same style can become an exercise in cut and paste which will leave your code in a less manageable state.

I have written some simple code in the fluent style that allows you to write your custom liferay queries in a much simpler way, leading to more maintainable and understandable code.

You can get the code at github.  Feel free to contribute.

Using this, a query like this:

	public List<CustomerAddress> findCustomerAddressesByCustomerCode(String customerCode) {
		try {
			session = openSession();

			StringBuilder query = new StringBuilder();

			query.append("select customerAddress from "
				+ Customer.class.getName() + " as customer, " + CustomerAddress.class.getName()
				+ " as customerAddress "
				+ " where "
				+ " customer.customerId = customerAddress.customerId"
				+ " customer.customerCode = ? ");

			Query q = session.createQuery(query.toString());

			QueryPos qPos = QueryPos.getInstance(q);

			qPos.add(parentCategoryId);

			list = (List<Category>) q.list();
		} catch (Exception e) {
				throw processException(e);
		} finally {
				if (list == null) {
					list = new ArrayList<Category>();
				}

			closeSession(session);
		}
		return list;

	}

becomes

	public List<CustomerAddress> findCustomerAddressesByCustomerCode(String customerCode) {
		return withSessionFactory(this).withQuery(
			"select customer, customerAddress from "
				+ Customer.class.getName() + " as customer, " + CustomerAddress.class.getName()
				+ " as customerAddress "
				+ " where "
				+ " customer.customerId = customerAddress.customerId"
				+ " customer.customerCode = ?").withParameters(customerCode)
			.toList();
	}

and the code completion helps you finish your query.

 

Releases

No releases published

Packages

No packages published

Languages