Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Apache Cayenne插入数据时的查询_Java_Apache Cayenne - Fatal编程技术网

Java 使用Apache Cayenne插入数据时的查询

Java 使用Apache Cayenne插入数据时的查询,java,apache-cayenne,Java,Apache Cayenne,我用java 8编写了一段代码,使用apache cayenne 3.1在sql server 2012中插入数据。执行代码时,我遇到一个异常:org.apache.cayenne.CayenneRuntimeException:[v.3.1 Sep 20 2014 14:24:57]提交异常,原因是:java.sql.SQLException:找不到存储过程“auto\u pk\u for_table” 通过将cayenne modeler中的pk生成策略从默认更改为数据库生成,我已经解决了这

我用java 8编写了一段代码,使用apache cayenne 3.1在sql server 2012中插入数据。执行代码时,我遇到一个异常:org.apache.cayenne.CayenneRuntimeException:[v.3.1 Sep 20 2014 14:24:57]提交异常,原因是:java.sql.SQLException:找不到存储过程“auto\u pk\u for_table”

通过将cayenne modeler中的pk生成策略从默认更改为数据库生成,我已经解决了这个问题。但当我再次执行java代码时,假设它只在数据库中插入1条记录,但它也插入了我在异常中得到的前一条记录。我尝试过三次创建相同的场景,但得到了相同的结果。在获得异常后,我还尝试重新启动web服务器和SQL server服务,但在插入新数据时,同时也插入了异常记录。 这个问题也可以通过在catch块中插入rollback语句来解决

但我想知道apache cayenne在插入新数据时如何以及为什么插入异常数据

这是我的密码。 Java: 潘松道

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文件: 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>


此问题也发布在Cayenne用户列表中。那里已经有了答案:所以请继续那里的对话。是的,但我对这些答案不满意。我想知道流程。我已经有了解决办法,明白了。我只是在评论一个事实,如果你在一个论坛上与社区接触,你可能会得到更好的结果。