1

getting following exception while inserting 2nd time in database from my liferay portlet.

[JDBCExceptionReporter:76] Duplicate entry '0' for key 'PRIMARY'.(i think its because my primary key value not getting auto increment)

I think have done mistake while auto incrementing the primary key in my custom portlet .but i don't know where i have to make changes for that.

if anyone can guide me about to where make the changes to solve this auto increment issue?

this is the code from auto increment been set try {

        restVar = restaurantPersistence.create(counterLocalService
                .increment(restaurant.class.toString()));
    } catch (SystemException e) {
        e.printStackTrace();
        return restVar = null;
    }

    try {



        resourceLocalService.addResources(0,restParam.getGroupId(), restParam.getUserId(),
                restaurant.class.getName(),restParam.getPrimaryKey(), false,true,true);
    } catch (PortalException e) {
        e.printStackTrace();
        return restVar = null;
    } catch (SystemException e) {
        e.printStackTrace();
        return restVar = null;
    }
BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • What is an example of the query you're using to insert and this is MySQL, correct? – Kirk Oct 20 '12 at 07:07
  • Did you check the database if the primary column has the auto-increment set to True or not? – Raunak Agarwal Oct 20 '12 at 07:09
  • ok..i had solved the problem while manully doing autoincrement option set to ticked.but i never face these problem before with inserting from liferay method.. – BhavikKama Oct 20 '12 at 08:42
  • @krik see my updated question.have added the portion where autoincrement code is wrtitten.. – BhavikKama Oct 20 '12 at 08:43
  • u dont have to increment it will be incremented automatically ,leave it for database. – Arun Killu Oct 20 '12 at 08:53
  • its not about that but whenever i redeploy the portlet the database creates the fresh tables and then i have to manually make it auto increment.and thats not feasible and my other portlet dont ahve this type of issue – BhavikKama Oct 20 '12 at 12:09

1 Answers1

1

Try this one..

long primaryKeyId = CounterLocalServiceUtil.increment(ClassName.class.getName());

XYZDetails XYZDetails = XYZDetailsLocalServiceUtil.createXYZDetails(primaryKeyId);

Add other details using XYZDetails Obj

e.g

XYZDetails.setName("Name");

Then Save the Details..

XYZDetailsLocalServiceUtil.addXYZDetails(XYZDetails);

Hope this may help you !!!

Laxman Rana
  • 2,904
  • 3
  • 25
  • 37