concept Service Builder in category liferay

appears as: Service Builder, The Service Builder
Liferay in Action: The Official Guide to Liferay Portal Development

This is an excerpt from Manning's book Liferay in Action: The Official Guide to Liferay Portal Development.

As far as standards go, Liferay is also based on widely used, standard ways of doing things. It adheres to the JSR-286 portlet standard. In addition to that, it includes utilities such as Service Builder to automatically generate interfaces to databases (something not covered by the standard). Under the hood, Service Builder is just Spring and Hibernate—widely used by Java developers everywhere. You get the benefit of using the platform to get your site done more quickly while taking advantage of standards that keep your code free.

But let’s go one step further. What if you could define a database table in an XML file and, from that definition, generate all the Hibernate configuration, all the Spring configuration, finder methods, the model layer, the SQL to create the table on all leading databases, and the entire Data Access Object (DAO) layer in one fell swoop? That’s what Service Builder gives you. Figure 3.4 illustrates this.

Figure 3.4. Service Builder is a tool that sits on top of Hibernate and Spring, automatically generating both configurations for you—along with the Java code necessary to persist your entities to the database.

Just like adding and deleting, getting products out of the database is easy. You probably remember that you had Service Builder generate a finder that queries the database for products by the GroupId. You always want to query by groupId because the portlet is non-instanceable. You implement this in the DTO layer as follows:

public List<PRProduct> getAllProducts(long groupId)
    throws SystemException {

    List<PRProduct> products =
        prProductPersistence.findByGroupId(groupId);
    return products;
}

This code returns a List of PRProducts that can be used in the UI layer to display products for viewing or editing purposes. And as you can see, whatever calls this method (it will be your portlet class) doesn’t need to know anything about JDBC or databases. It’s requesting a List. This frees you to do something like swap out Service Builder for something else without ever touching your portlet code.

You started with Product because it was a simple, standalone table. Now that you see how easy it is to use Service Builder, we can move on to looking at tables with relationships.

3.3. Generating DB code with Service Builder

If you’re an experienced developer, it’s likely that you’ve dealt with a data-driven application before. Liferay ships with a tool called Service Builder that makes the creation of data-driven applications easy. I highly recommend that you use Service Builder when writing applications on Liferay’s platform, because it will help get you going quickly. How? By generating a lot of the database plumbing code for you, so you can concentrate on your application’s functionality.

We’ll look first at the need that Service Builder responds to. Then you’ll see how to configure and run Service Builder to create the layer of code that handles your database transactions.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest