Author: allee8285
Date: Mon Jun 24 17:05:05 2013
New Revision: 1496124
URL: http://svn.apache.org/r1496124
Log:
OPENJPA-2390 - commit patch contributed by Di Lau.
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee.java
(with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee2.java
(with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/TestHSQLSequence.java
(with props)
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/HSQLDictionary.java
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/HSQLDictionary.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/HSQLDictionary.java?rev=1496124&r1=1496123&r2=1496124&view=diff
==============================================================================
---
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/HSQLDictionary.java
(original)
+++
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/HSQLDictionary.java
Mon Jun 24 17:05:05 2013
@@ -50,6 +50,7 @@ public class HSQLDictionary extends DBDi
public boolean cacheTables = false;
private int dbMajorVersion;
+ private int dbMinorVersion;
private SQLBuffer _oneBuffer = new SQLBuffer(this).append("1");
@@ -114,6 +115,9 @@ public class HSQLDictionary extends DBDi
// but doesn't support it for columns references
("schema.table.column")
useSchemaName = false;
}
+ if (dbMajorVersion > 1 && dbMinorVersion > 0) {
+ nextSequenceQuery += " LIMIT 1";
+ }
}
/**
@@ -126,11 +130,13 @@ public class HSQLDictionary extends DBDi
if (isJDBC3) {
dbMajorVersion = metaData.getDatabaseMajorVersion();
+ dbMinorVersion = metaData.getDatabaseMinorVersion();
} else {
// String is like "2.0.0"
String productVersion = metaData.getDatabaseProductVersion();
String[] version = productVersion.split("\\.") ;
dbMajorVersion = Integer.parseInt(version[0]) ;
+ dbMinorVersion = Integer.parseInt(version[1]);
}
}
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee.java?rev=1496124&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee.java
Mon Jun 24 17:05:05 2013
@@ -0,0 +1,61 @@
+/*
+ * 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.openjpa.persistence.sequence.hsql;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.SequenceGenerator;
+
+@Entity
+@SequenceGenerator(name = "HEmpSeq", sequenceName =
"native(Sequence=HEMP_SEQ)", allocationSize = 1)
+public class HSQLEmployee {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HEmpSeq")
+ private int id;
+
+ private String firstName;
+ private String lastName;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee2.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee2.java?rev=1496124&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee2.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee2.java
Mon Jun 24 17:05:05 2013
@@ -0,0 +1,61 @@
+/*
+ * 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.openjpa.persistence.sequence.hsql;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.SequenceGenerator;
+
+@Entity
+@SequenceGenerator(name = "HEmpSeq2", sequenceName =
"native(Sequence=HEMP_SEQ)", allocationSize = 2)
+public class HSQLEmployee2 {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HEmpSeq2")
+ private int id;
+
+ private String firstName;
+ private String lastName;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/HSQLEmployee2.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/TestHSQLSequence.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/TestHSQLSequence.java?rev=1496124&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/TestHSQLSequence.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/TestHSQLSequence.java
Mon Jun 24 17:05:05 2013
@@ -0,0 +1,135 @@
+/*
+ * 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.openjpa.persistence.sequence.hsql;
+
+import java.util.List;
+
+import javax.persistence.Query;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+/**
+ * The test case demonstrates the expected return id sequence should be:
+ * "1, 2, 3, 4, 5, ..." regardless of the HSQL database version being used.
+ *
+ * In version 2.1.* onward, "SELECT NEXT VALUE ..." query returns two times the
+ * sequence's "INCREMENT BY" value, for example: allocationSize=1 returns
+ * "1, 2". The next call returns "3, 4". Therefore, the id sequence returned
+ * (without "LIMIT 1") are: "1, 3, 5, 7, 9, ...". By the same token, using
+ * allocationSize=2 returns "1, 2, 3, 4". The next call returns "5, 6, 7, 8".
+ * The id sequence returned then (without "LIMIT 1") are: "1, 2, 5, 6, 9, ...".
+ *
+ * For HSQL version 2.1.0 rc4 and beyond, the dictionary needs to append
+ * "LIMIT 1" to the nextSequenceQuery field in HSQLDictionary.java to ensure
+ * single value is returned in "SELECT NEXT VALUE ..." request. This forces
HSQL
+ * to hand back only one value at a time. Therefore, the returned id sequence
is
+ * "1, 2, 3, 4, 5, ..." independent of HSQL version.
+ */
+public class TestHSQLSequence extends SQLListenerTestCase {
+ OpenJPAEntityManager em;
+ JDBCConfiguration conf;
+ DBDictionary dict;
+
+ public void setUp() throws Exception {
+
setSupportedDatabases(org.apache.openjpa.jdbc.sql.HSQLDictionary.class);
+ if (isTestsDisabled()) {
+ return;
+ }
+
+ super.setUp(HSQLEmployee.class, HSQLEmployee2.class, DROP_TABLES,
+ "openjpa.ConnectionFactoryProperties", "PrintParameters=true"
+ );
+ assertNotNull(emf);
+
+ conf = (JDBCConfiguration) emf.getConfiguration();
+ dict = conf.getDBDictionaryInstance();
+ boolean supportsNativeSequence = dict.nextSequenceQuery != null;
+ if (supportsNativeSequence) {
+ em = emf.createEntityManager();
+ // Drop all sequences to eliminate non-consecutive "SELECT NEXT
VALUE FOR ..."
+ assertNotNull(em);
+ Query q = em.createNativeQuery(
+ "SELECT SEQUENCE_SCHEMA, SEQUENCE_NAME FROM
INFORMATION_SCHEMA.SYSTEM_SEQUENCES");
+ List<Object[]> rs = q.getResultList();
+ em.getTransaction().begin();
+ for(Object[] os : rs) {
+ String schemaQualifier = (String)os[0];
+ String schemaName = (String)os[1];
+ if( "PUBLIC".equals(schemaQualifier) &&
!schemaName.equals("HEMP_SEQ")) {
+ q = em.createNativeQuery("DROP SEQUENCE " + schemaName);
+ q.executeUpdate();
+ }
+ }
+ em.getTransaction().commit();
+ em.clear();
+ }
+ }
+
+ // create HSQLEmployee entity and persist it
+ public HSQLEmployee createHSQLEmployee(String first, String last) {
+ HSQLEmployee e = new HSQLEmployee();
+ e.setFirstName(first);
+ e.setLastName(last);
+ em.getTransaction().begin();
+ em.persist(e);
+ em.getTransaction().commit();
+ int id = e.getId();
+ em.clear();
+ return em.find(HSQLEmployee.class, id);
+ }
+
+ public HSQLEmployee2 createHSQLEmployee2(String first, String last) {
+ HSQLEmployee2 e = new HSQLEmployee2();
+ e.setFirstName(first);
+ e.setLastName(last);
+ em.getTransaction().begin();
+ em.persist(e);
+ em.getTransaction().commit();
+ int id = e.getId();
+ em.clear();
+ return em.find(HSQLEmployee2.class, id);
+ }
+
+ public void testId() {
+ int counter = 1;
+ int id = 0;
+ while (counter <= 20) {
+ HSQLEmployee2 e = createHSQLEmployee2("Ferris" + counter, "Erris");
+ assertNotNull(e);
+ id = e.getId();
+ assertEquals(counter, id);
+ ++counter;
+ }
+ }
+
+ public void testId2() {
+ int counter = 1;
+ int id = 0;
+ while (counter <= 20) {
+ HSQLEmployee e = createHSQLEmployee("Ferris" + counter, "Erris");
+ assertNotNull(e);
+ id = e.getId();
+ assertEquals(counter, id);
+ ++counter;
+ }
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/TestHSQLSequence.java
------------------------------------------------------------------------------
svn:eol-style = native