0

I have written a code in java 8 to insert data in sql server 2012 using apache cayenne 3.1. While executing code I got an exception : org.apache.cayenne.CayenneRuntimeException: [v.3.1 Sep 20 2014 14:24:57] Commit Exception Caused by: java.sql.SQLException: Could not find stored procedure 'auto_pk_for_table'.

I have solved the problem by changing my pk generation strategy in cayenne modeler from default to database generated. But when I executed my java code again, it was suppose to insert only 1 record in database but it also inserted previous record which I got in exception. I have tried creating the same scenario thrice but I got the same result. I also tried restarting my web server and SQL server service after getting an exception, but at the time of inserting new data exceptional record was also been inserted at the same time. This Problem is also solved by inserting a rollback statement in my catch block.

But I want to know that how and why is apache cayenne inserting exceptional data at the time of inserting new data.

This is my code. Java : PersonDao

try {
    Person person = new Person();
    person.setFirstName("John");
    person.setLastName("Cross");
    ObjectContext context = BaseContext.getThreadObjectContext();
    context.registerNewObject(person);
    context.commitChanges();
} catch (CayenneRuntimeException e) {
    context.rollbackChanges();
    throw e;
} 

XML files : cayenne-test.xml

<?xml version="1.0" encoding="utf-8"?>
    <domain project-version="6">
    <property name="cayenne.DataDomain.usingExternalTransactions" value="true"/>
    <map name="MastersDataMap"/>
    <node name="MastersDataNode" factory="org.apache.cayenne.configuration.server.XMLPoolingDataSourceFactory" schema-update-strategy="org.apache.cayenne.access.dbsync.CreateIfNoSchemaStrategy">
        <map-ref name="MastersDataMap"/>
        <data-source>
            <driver value="net.sourceforge.jtds.jdbc.Driver"/>
            <url value="jdbc:jtds:sqlserver://localhost:1433/test"/>
            <connectionPool min="1" max="30"/>
            <login userName="sa" password="admin@123"/>
        </data-source>
    </node>
</domain>

TestDataMap.map.xml

<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/3.0/modelMap"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://cayenne.apache.org/schema/3.0/modelMap http://cayenne.apache.org/schema/3.0/modelMap.xsd"
 project-version="6">
<property name="defaultPackage" value="com.org.ivcargo.platform.dto"/>
<db-entity name="PersonTemp" schema="dbo" catalog="test">
    <db-attribute name="personId" type="NUMERIC" isPrimaryKey="true" isGenerated="true" isMandatory="true" length="10"/>
    <db-attribute name="firstname" type="VARCHAR" length="100"/>
    <db-attribute name="lastname" type="VARCHAR" length="100"/>
</db-entity>
<obj-entity name="PersonTemp" className="com.org.ivcargo.platform.dto.PersonTemp" dbEntityName="PersonTemp">
    <obj-attribute name="personId" type="java.lang.Long" lock="true" db-attribute-path="personId"/>
    <obj-attribute name="firstname" type="java.lang.String" db-attribute-path="firstname"/>
    <obj-attribute name="lastname" type="java.lang.String" db-attribute-path="lastname"/>
</obj-entity>
</data-map>
  • This question was also posted on Cayenne user list. And there were answers there already: http://www.mail-archive.com/user@cayenne.apache.org/msg08944.html So feel free to continue the conversation there. – andrus_a Sep 10 '15 at 12:24
  • yeah but I am not satisfied with those answers. I want to know the flow. I already have the solution. – Dipesh Jain Sep 11 '15 at 07:05
  • Understood. I am just commenting on the fact that you'd probably get a better outcome if you engage with community on a single forum. – andrus_a Sep 11 '15 at 11:01

0 Answers0