cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrus Adamchik <and...@objectstyle.org>
Subject Re: Query at the time of inserting data using Apache Cayenne
Date Fri, 11 Sep 2015 16:38:46 GMT
Ari's example was idiomatic Cayenne, even though it did not contain the solution. I.e. it is
generally a good idea to create and register a new object in one place with context.newObject(..),
before you start modifying object properties. Anyways... 

>> I have solved the problem by changing my pk generation strategy in cayenne
>> modeler from default to database generated.

Yep, that's a good approach to PK generation in SQLServer.

>> 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. 

When you inserted the second record into ObjectContext, the context was already "dirty", i.e.
contained changes from somewhere else. My guess is you are reusing the same ObjectContext
across multiple web requests. You are probably using CayenneFilter to bootstrap Cayenne, which
provides session-scoped context by default. There are different ways to solve it. Here is
one - you can switch to request-scoped context (if you don't need to preserve uncommitted
changes across requests):


# Create a custom module overriding RequestHandler 
# to use org.apache.cayenne.configuration.web.StatelessContextRequestHandler

package com.foo;
...
public class MyModule implements Module {
    @Override
    public void configure(Binder binder) {
 	binder.bind(RequestHandler.class)
                .to(StatelessContextRequestHandler.class)
                .withoutScope();
    }
}

# load it in CayenneFilter in web.xml:

<filter>
   <filter-name>cayenne-project</filter-name>
   <filter-class>org.apache.cayenne.configuration.web.CayenneFilter</filter-class>
   <init-param>
      <param-name>extra-modules</param-name>
      <param-value>com.foo.MyModule</param-value>
   </init-param> 
</filter>

Hope this helps.

Andrus


> On Sep 11, 2015, at 5:16 PM, Dipesh Jain <dipesh@ivgroup.in> wrote:
> 
> I have tried to implement @Aristedes Maniatis answer but still result was
> same. I want to know why and how apache cayenne is doing this. I do not
> want the solution because I already have the answer.
> 
> On Wed, Sep 9, 2015 at 9:41 PM, Dipesh Jain <dipesh@ivgroup.in> wrote:
> 
>> 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>
>> 
>> --
>> Thanks and Regards
>> Deepesh Jain
>> 
> 
> 
> 
> -- 
> Thanks and Regards
> Deepesh Jain


Mime
View raw message