Return-Path: X-Original-To: apmail-openjpa-commits-archive@www.apache.org Delivered-To: apmail-openjpa-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E67CCCA36 for ; Mon, 24 Jun 2013 17:05:33 +0000 (UTC) Received: (qmail 42115 invoked by uid 500); 24 Jun 2013 17:05:33 -0000 Delivered-To: apmail-openjpa-commits-archive@openjpa.apache.org Received: (qmail 42048 invoked by uid 500); 24 Jun 2013 17:05:29 -0000 Mailing-List: contact commits-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openjpa.apache.org Delivered-To: mailing list commits@openjpa.apache.org Received: (qmail 42041 invoked by uid 99); 24 Jun 2013 17:05:27 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 24 Jun 2013 17:05:27 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 24 Jun 2013 17:05:25 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id A21C523888E3; Mon, 24 Jun 2013 17:05:06 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1496124 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/sequence/hsql/ Date: Mon, 24 Jun 2013 17:05:06 -0000 To: commits@openjpa.apache.org From: allee8285@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130624170506.A21C523888E3@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 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