Message Boards

« Back to 2.- Usage ( From Xml file to CRUD Portlet)

What if the Primary Key is SERIAL?

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
What if the Primary Key is SERIAL?
Answer
11/29/11 4:30 PM
Hola,

I'm working with a table which primary key is a Serial data type.

How can I map a serial field in XMLPortletFactory?.

My problem came when I try to insert a new value in a table with populated data, because the generated "PK Id" cause a ConstraintViolationException ( org.hibernate.exception.ConstraintViolationException)


Details:
  1. Create a "cities" portlet
  2. Populate cities table with (624) values from a insert script:

    1insert into exampleportlet_cities(citid, citdesc, citdescalternative, citcodpost, prvid, companyid, groupid, userid)
    2select cit_id, cit_desc, cit_desc_alternative, cit_codpost, prv_id, 10131, 13036, 10168 from cities;

  3. install the new "Cities portlet" in a Liferay page (see attach image)
  4. Create new city
  5. ERROR:

     115:41:27,281 ERROR [jsp:154] java.sql.BatchUpdateException: Batch entry 0 insert into architectsmaintenance_cities (companyId, groupId, userId, citDesc, citDescAlternative, citCodp
     2ost, prvId, citId) values ('10131', '13036', '10168', 'Boston', 'Boston USA', '007', '11', '[b]4[/b]') was aborted.  Call getNextException to see the cause.
     3        at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2598)
     4        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
     5        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
     6        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2737)
     7        at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
     8        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
     9        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    10        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
    11        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:179)
    12        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    13        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    14        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
    15        at com.liferay.portal.dao.orm.hibernate.SessionImpl.flush(SessionImpl.java:108)
    16        at com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:54)
    17        at com.liferay.portal.service.persistence.BatchSessionImpl.update(BatchSessionImpl.java:66)
    18        at com.liferay.portal.service.persistence.BatchSessionUtil.update(BatchSessionUtil.java:43)
    19        at org.xmlportletfactory.portal.coavnbiz.service.persistence.citiesPersistenceImpl.updateImpl(citiesPersistenceImpl.java:285)
    20        at org.xmlportletfactory.portal.coavnbiz.service.persistence.citiesPersistenceImpl.updateImpl(citiesPersistenceImpl.java:71)
    21        at com.liferay.portal.service.persistence.impl.BasePersistenceImpl.update(BasePersistenceImpl.java:255)
    22        at com.liferay.portal.dao.shard.ShardAdvice.invokePersistence(ShardAdvice.java:205)


    In the above exception you can see an INSERT with citId=4 and in the table exists Ids from 1 to 624.



This is part of the XML description file where I map citId as long:

 1            <fileDef>
 2                <name>cities</name>
 3                <fields>
 4                    <field>
 5                        <name>citId</name>
 6                        <title>Id</title>
 7                        <type>
 8                            <long>
 9                                <length>10</length>
10                                <signed>false</signed>
11                                <nullable>false</nullable>
12                            </long>
13                        </type>
14                        <showFieldInView>false</showFieldInView>
15                    </field>




Saludos,

David.
Attachment

Attachments: cities.JPG (64.3k)

RE: What if the Primary Key is SERIAL?
Answer
12/1/11 9:49 PM as a reply to David Navarro.
Hola David,

I didnt used serial data key fields, mainly because data base compatibility, and used the Liferay's CounterLocalServiceUtil system for incrementing key values instead.

If I wanted to use serial data key fields, I would start by going to the yourportlet-LocalServiceImpl.java and change add_xxxxxx function.

There you have the ".....create(CounterLocalServiceUtil.increment(yourportlet.class.getName()));" and now I dont remember other place where key value is used for adding. This should do.

If this is the normal behaviour you want, you can change "Portlet_XXXXXXLocalServiceImpl_java.vm" velocity template, around line 120 to change this, so next portlets will use serial keys. You will probably will have to change by hand, tables creation sql files.


Saludos, Jack.