Adding Liferay Persistance Classes Print
Written by Josh B   
Tuesday, 10 January 2012 13:24

Tags: java | liferay | maven | persistence

Although I last wrote about Liferay nearly a year ago, I've been using Liferay in various different projects. It's a great way to get code into onto the web. With Liferay there is no need to reinvent the wheel and keep on implementing users, groups and basic security. There's also little point in writing your own persistence classes. So read on for a quick overview on how to get Liferay to write these for you.

1) Add a services.xml file to src/main/webapp/WEB-INF/service.xml. See http://www.liferay.com/community/wiki/-/wiki/Main/Service+Builder for the format / structure.

2) Add the following to the pom :

        <resources>
            <!--  This is required to make sure the service.properties file
            is copied over. It points to the Spring init items -->
            <resource>
                <directory>./src/main/java</directory>
                <includes>
                    <include>*.properties</include>
                </includes>
            </resource>
            <!--  This is required to copy over the Sprint init items that
            will be used -->
            <resource>
                <directory>./src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

3) Generate all the files with:

mvn liferay:build-service

4) Update the Finders. Your Finders will be generated in a file like service/persistence/NHKeywordPersistenceImpl. Copy and paste the method, modifying in the following way:

    public List<ABExample> findByMyExamples(long userId)
        throws SystemException {
        return findByMyExamples
(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
            null);
    }

Becomes in NHKeywordLocalServiceImpl:

   public List<ABExample> getByMyExamples(long userId) throws SystemException {
        return abExamplePersistence.findByMyExamples(userId);
    }

NB: The code won't compile at this point...!

5) Regenerate the files with:

mvn liferay:build-service

Now the code will compile...

6) Also need to make a folder in src/main/webapp/WEB-INF called sql. This will then hold the sql. Need to change the sql so it looks like this:

create table AB_ABExample (
    pk int not null primary key autoincrement,
    exampleStr VARCHAR(75) null,
    weighting int,
    groupId int,
    companyId int,
    userId int,
    label VARCHAR(75) null
);

Now you will be able to retrieve a list of examples with:

List<ABExample> keywords = ABExampleLocalServiceUtil.getByMyExamples (userId);

And inserting with:

ABExample example = ABExampleLocalServiceUtil.createABExample(CounterLocalServiceUtil.increment (ABExample.class.getName()));
example.setExampleStr(exampleStr);
ABExampleLocalServiceUtil.addABExample(keyword);

Of course, you will need to handle all the Exceptions this can cause...!

Last Updated on Tuesday, 10 January 2012 13:43