Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Kiran Sai
Liferay 6 is not Updating Custom Table Created in MYSQL
February 22, 2013 11:52 AM
Answer

Kiran Sai

Rank: Junior Member

Posts: 47

Join Date: June 29, 2011

Recent Posts

Hi

I am using Liferay 6.1

I have created a Table in mysql as shown



1create table Book (
2    bookId bigint(10) not null primary key,
3    companyId bigint(10) null ,
4        userId bigint(10) null ,
5    userName VARCHAR(75)
6);


This is my service.xml file

 1<?xml version="1.0" encoding="UTF-8"?>
 2<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
 3<service-builder package-path="com.test">
 4    <author>sai</author>
 5    <namespace>Library</namespace>
 6    <entity name="Book" local-service="true" remote-service="false">
 7        <column name="bookId" type="long" primary="true" />
 8        <column name="companyId" type="long" />
 9        <column name="userId" type="long" />
10        <column name="userName" type="String" />
11    </entity>
12</service-builder>



This is my BookLocalServiceImpl class

 1public class BookLocalServiceImpl extends BookLocalServiceBaseImpl {
 2    public Book addBook(long userId, String userName) throws PortalException,
 3            SystemException {
 4        long bookId = CounterLocalServiceUtil.increment(Book.class.getName());
 5        Book book = bookPersistence.create(bookId);
 6        book.setCompanyId(1126);
 7        book.setUserId(1126);
 8        book.setUserName(title);
 9        book = bookPersistence.update(book, false);
10        return book;
11    }
12
13}


Inside the processAction class , i have added this way

1BookLocalServiceUtil.addBook(userId, userName);

But the problem is that , the Liferay Framework has created a New table by name library_book
and updated that table .


1 mysql> select * from library_book;
2+--------+-----------+--------+----------+
3| bookId | companyId | userId | userName |
4+--------+-----------+--------+----------+
5|      1 |      1126 |   1126 | saibaba  |
6+--------+-----------+--------+----------+
71 row in set (0.00 sec)

Where as my Table Book is empty

1
2mysql> select * from Book;
3Empty set (0.00 sec)

Please let me know how to make the table Book gets Updated , but not library_book
David H Nebinger
RE: Liferay 6 is not Updating Custom Table Created in MYSQL
February 22, 2013 1:34 PM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11056

Join Date: September 1, 2006

Recent Posts

When an entity does not have a defined table name, SB will construct a table name by joining the namespace with the entity name. This is done to avoid table name conflicts w/ Liferay's tables. You can try using the 'table' attribute to indicate the table name to use rather than the entity name, but I'm not sure if SB will still prepend the namespace or not.

And before you ask, no you cannot have a blank or empty namespace.
Sandeep Nair
RE: Liferay 6 is not Updating Custom Table Created in MYSQL
February 22, 2013 1:37 PM
Answer

Sandeep Nair

Rank: Liferay Legend

Posts: 1721

Join Date: November 5, 2008

Recent Posts

Hi,

Liferay adds namespace to table for uniqueness. Your service.xml has a namespace Liferay, and you didnt specify value for "table" attribute in your entity. Hence Liferay automatically generated a table for you called Liferay_Book.

If you already have a table created and you want to map it with the entity, then while specifying entity in service.xml, add the "table" attribute which is having the value of actual table name.

In your case try something like the following
1
2<entity name="Book" local-service="true" table="Book" remote-service="false">


Regards,
Sandeep Nair
Kiran Sai
RE: Liferay 6 is not Updating Custom Table Created in MYSQL
February 22, 2013 8:37 PM
Answer

Kiran Sai

Rank: Junior Member

Posts: 47

Join Date: June 29, 2011

Recent Posts

Thank you very very much Sandeep that was really useful .