Author: aadamchik
Date: Mon Jan 7 20:23:49 2013
New Revision: 1429993
URL: http://svn.apache.org/viewvc?rev=1429993&view=rev
Log:
CAY-1783 JdbcPkGenerator.longPkFromDatabase would throw an exception if the PK
value exceeds a range of Java int
Added:
cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/dba/JdbcPkGeneratorTest.java
Modified:
cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/JdbcPkGenerator.java
Modified:
cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/JdbcPkGenerator.java
URL:
http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/JdbcPkGenerator.java?rev=1429993&r1=1429992&r2=1429993&view=diff
==============================================================================
---
cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/JdbcPkGenerator.java
(original)
+++
cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/JdbcPkGenerator.java
Mon Jan 7 20:23:49 2013
@@ -50,10 +50,12 @@ import org.apache.cayenne.util.IDUtil;
public class JdbcPkGenerator implements PkGenerator {
public static final int DEFAULT_PK_CACHE_SIZE = 20;
+ private static final long DEFAULT_PK_START_VALUE = 200;
protected JdbcAdapter adapter;
protected Map<String, LongPkRange> pkCache = new HashMap<String,
LongPkRange>();
protected int pkCacheSize = DEFAULT_PK_CACHE_SIZE;
+ protected long pkStartValue = DEFAULT_PK_START_VALUE;
public JdbcPkGenerator(JdbcAdapter adapter) {
this.adapter = adapter;
@@ -142,7 +144,7 @@ public class JdbcPkGenerator implements
.append(" (TABLE_NAME, NEXT_ID)")
.append(" VALUES ('")
.append(entName)
- .append("', 200)");
+ .append("', ").append(pkStartValue).append(")");
return buf.toString();
}
@@ -331,6 +333,14 @@ public class JdbcPkGenerator implements
public void setPkCacheSize(int pkCacheSize) {
this.pkCacheSize = (pkCacheSize < 1) ? 1 : pkCacheSize;
}
+
+ long getPkStartValue() {
+ return pkStartValue;
+ }
+
+ void setPkStartValue(long startValue) {
+ this.pkStartValue = startValue;
+ }
public void reset() {
pkCache.clear();
@@ -352,13 +362,13 @@ public class JdbcPkGenerator implements
return false;
}
- public int getId() {
+ public long getId() {
if (id == null) {
throw new CayenneRuntimeException("No key was retrieved for
entity "
+ entityName);
}
- return id.intValue();
+ return id.longValue();
}
public void nextRows(Query query, List<?> dataRows) {
Added:
cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/dba/JdbcPkGeneratorTest.java
URL:
http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/dba/JdbcPkGeneratorTest.java?rev=1429993&view=auto
==============================================================================
---
cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/dba/JdbcPkGeneratorTest.java
(added)
+++
cayenne/main/branches/STABLE-3.1/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/dba/JdbcPkGeneratorTest.java
Mon Jan 7 20:23:49 2013
@@ -0,0 +1,60 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.dba;
+
+import java.util.Collections;
+
+import org.apache.cayenne.access.DataNode;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.testdo.testmap.Artist;
+import org.apache.cayenne.unit.di.server.ServerCase;
+import org.apache.cayenne.unit.di.server.UseServerRuntime;
+
+@UseServerRuntime(ServerCase.TESTMAP_PROJECT)
+public class JdbcPkGeneratorTest extends ServerCase {
+
+ @Inject
+ private DbAdapter adapter;
+
+ @Inject
+ private DataNode node;
+
+ public void testLongPk() throws Exception {
+
+ if
(!JdbcPkGenerator.class.isAssignableFrom(adapter.getPkGenerator().getClass())) {
+ return;
+ }
+
+ DbEntity artistEntity =
node.getEntityResolver().lookupObjEntity(Artist.class).getDbEntity();
+
+ DbAttribute pkAttribute = (DbAttribute)
artistEntity.getAttribute(Artist.ARTIST_ID_PK_COLUMN);
+
+ JdbcPkGenerator pkGenerator = (JdbcPkGenerator)
adapter.getPkGenerator();
+
+ pkGenerator.setPkStartValue(Integer.MAX_VALUE * 2l);
+ pkGenerator.createAutoPk(node,
Collections.singletonList(artistEntity));
+ pkGenerator.reset();
+
+ Object pk = pkGenerator.generatePk(node, pkAttribute);
+ assertTrue(pk instanceof Long);
+ assertTrue("PK is too small: " + pk, ((Long) pk).longValue() >
Integer.MAX_VALUE);
+ }
+}