liferayliferay-service-builder

auto increment value in liferay


I have 2 modules say module 1 aand module 2 , module 1 is using liferay and module 2 is not using liferay.

I have a database say myDatabase.

Both module 1 and module 2 are using the same database for the read and write operations .

module 1 is using the service builder and on a particular table XYZ to insert a value it uses the below mentioned code.

XYZ = XYZLocalServiceUtil.createXYZ(0);
XYZ.setX(x);
XYZ.setY(y);
XYZ.setZ(z);
XYZ = XYZLocalServiceUtil.addXYZ(XYZ);

and the module 2 is using the query :

insert into XYZ(x,y,z) values (x,y,z);

Now the problem occurs when the module 2 inserts a record first.

After a insertion by the module 2 , when the module 1(uses service builder) inserts a record it throws the below mentioned exception.

> 06:04:02,824 ERROR [http-bio-8443-exec-38][JDBCExceptionReporter:82]
> Duplicate entry '143' for key 'PRIMARY' 06:04:02,847 ERROR
> [http-bio-8443-exec-38][ClpSerializer:3279]
> java.lang.ClassNotFoundException:
> org.hibernate.exception.ConstraintViolationException
> java.lang.ClassNotFoundException:
> org.hibernate.exception.ConstraintViolationException  at
> org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
>   at
> org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
>   at java.lang.Class.forName0(Native Method)  at
> java.lang.Class.forName(Class.java:278)   at
> com.liferay.portal.kernel.util.ClassResolverUtil.resolve(ClassResolverUtil.java:29)
>   at
> com.liferay.portal.kernel.util.ClassLoaderObjectInputStream.resolveClass(ClassLoaderObjectInputStream.java:43)
>   at
> java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1861)
>   at
> java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1744)
>   at
> java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2033)
>   at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1566)
>   at
> java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2278)
>   at
> java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:556)
>   at java.lang.Throwable.readObject(Throwable.java:914)   at
> sun.reflect.GeneratedMethodAccessor1940.invoke(Unknown Source)    at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:606)     at
> java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1160)
>   at
> java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2169)
>   at
> java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2060)
>   at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1566)
>   at java.io.ObjectInputStream.readObject(ObjectInputStream.java:426)
>   at
> com.myproject.ClpSerializer.translateThrowable(ClpSerializer.java:3265)
>   at com.myproject.XYZLocalServiceClp.addXYZ(XYZLocalServiceClp.java:1)
>   at
> com.myproject.XYZLocalServiceUtil.addXYZ(XYZLocalServiceUtil.java:5)

The database has the auto_increment as 144 at that time , still it shows Duplicate entry '143' for key 'PRIMARY'.

Scenario 2 : When the module 2 inserts two records , and now when module 1 tries to insert it again throws the same excetion for 144 . And on again insert operation for 145 and on third attempt it inserts for 146 as it is the current auto increment value now.

The problem is that it is(module 1 - service builder ) is not picking up the latest auto-increment value. How can it pick the latest auto_increment value .

here is the structure in service.xml of the module 1 :

<entity name="XYZ" local-service="true" remote-service="false" cache-enabled="false" >
        <column name="xyzId" type="long" primary="true"
            id-type="increment"></column>
        <column name="x" type="long"></column>
        <column name="y" type="long"></column>
        <column name="z" type="long"></column>
</entity>

Solution

  • You can define id-type as identity. If you have a column having auto incrementing primary key. There are 4 different id types. Normally we use increment. But in your case you have to use identity. Hope this will help. Thanks.

    <column name="id" type="Integer" primary="true" id-type="identity" />