mysqlhibernateliferay-6liferay-service-builder

liferay single database for two applications


I have a database which is common for two application one of which is running on Liferay and other on spring boot. I am inserting value in one of the tables ( having primary key set as auto incremented ) from both the application.

I have the following in service builder

<entity name="EntityName" local-service="true" remote-service="false" cache-enabled="false">
<column name="coloumnName" type="long" primary="true" id-type="increment" />

Now the application is giving a Duplicate primary key error when it is attempting to insert the value in the table. Anyone have solution for this?


Solution

  • id-type="increment" is indeed used for auto-increment fields. From the usage instructions in the very well documented service-builder DTD: (emphasis mine)

    The id-type and id-param values are used in order to create an auto-generated, auto-incrementing primary key when inserting records into a table. This can be implemented in 4 different ways, depending on the type of database being used. In all cases, the primary key of the model object should be assigned a value of null, and Hibernate will know to replace the null value with an auto-generated, auto-incremented value. If no id-type value is used, it is assumed that the primary key will be assigned and not auto-generated.

    If you follow any other code sample that manually sets a primary key before inserting a new object into the database, that value will be chosen for the ID. As you'll have to implement some parts of the glue code with Service Builder, there's a good chance you already set a primary key somewhere.

    If you pass null as the key, you shall get the auto-increment value back after insertion.