From 09e1c209ea49556ae585c27bff63b6a83f9b749f Mon Sep 17 00:00:00 2001 From: "Dianne E. Richards" Date: Fri, 8 Jun 2012 14:09:12 +0000 Subject: [PATCH] OPENJPA-2207 Fix single numeric db identifier problem git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.1.x@1348080 13f79535-47bb-0310-9956-ffa450edef68 --- .../lib/identifier/IdentifierUtilImpl.java | 2 +- .../results/cls/ResultClsAnnotation.java | 82 ++++++++++++++++ .../persistence/results/cls/ResultClsXml.java | 44 +++++++++ .../results/cls/TestResultClsAnnotation.java | 87 +++++++++++++++++ .../results/cls/TestResultClsXml.java | 94 +++++++++++++++++++ .../test/resources/META-INF/persistence.xml | 11 +++ .../resources/META-INF/query-result-orm.xml | 57 +++++++++++ 7 files changed, 376 insertions(+), 1 deletion(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/ResultClsAnnotation.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/ResultClsXml.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/TestResultClsAnnotation.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/TestResultClsXml.java create mode 100644 openjpa-persistence-jdbc/src/test/resources/META-INF/query-result-orm.xml diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/identifier/IdentifierUtilImpl.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/identifier/IdentifierUtilImpl.java index 237717916..c4d72442a 100644 --- a/openjpa-lib/src/main/java/org/apache/openjpa/lib/identifier/IdentifierUtilImpl.java +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/identifier/IdentifierUtilImpl.java @@ -323,7 +323,7 @@ public class IdentifierUtilImpl implements IdentifierUtil, Configurable { } public boolean isDelimited(IdentifierConfiguration config, IdentifierRule rule, String name) { - if (name == null || name.length() <= 3) { + if (name == null || name.length() < 3) { return false; } return name.startsWith(config.getLeadingDelimiter()) && diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/ResultClsAnnotation.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/ResultClsAnnotation.java new file mode 100644 index 000000000..c30b3f56e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/ResultClsAnnotation.java @@ -0,0 +1,82 @@ +/* + * 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.results.cls; + +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EntityResult; +import javax.persistence.FieldResult; +import javax.persistence.Id; +import javax.persistence.NamedNativeQueries; +import javax.persistence.NamedNativeQuery; +import javax.persistence.SqlResultSetMapping; + +@NamedNativeQueries({ +@NamedNativeQuery(name = "ResultClsQueryDoubleQuotes", + query = "select \"1\",\"2\" FROM ResultClsAnnoEntity", + resultSetMapping = "ResultClsRSMapping", + resultClass = ResultClsAnnotation.class), +@NamedNativeQuery(name = "ResultClsQueryBackTicks", + query = "select `1`,`2` FROM ResultClsAnnoEntity", + resultSetMapping = "ResultClsRSMapping", + resultClass = ResultClsAnnotation.class), +@NamedNativeQuery(name = "ResultClsQueryBrackets", + query = "select [1],[2] FROM ResultClsAnnoEntity", + resultSetMapping = "ResultClsRSMapping", + resultClass = ResultClsAnnotation.class), +@NamedNativeQuery(name = "ResultClsQueryDefault", + query = "select * FROM ResultClsAnnoEntity", + resultSetMapping = "ResultClsRSMapping", + resultClass = ResultClsAnnotation.class) +}) + +@SqlResultSetMapping(name = "ResultClsRSMapping", + entities = @EntityResult(entityClass = ResultClsAnnotation.class, fields = { + @FieldResult(name = "id", column = "1"), + @FieldResult(name = "description", column = "2") })) +@Entity(name = "ResultClsAnnoEntity") +public class ResultClsAnnotation { + public ResultClsAnnotation() { + } + + @Id + @Column(name = "1") + public String id; + @Basic + @Column(name = "2") + public String description; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/ResultClsXml.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/ResultClsXml.java new file mode 100644 index 000000000..25dc3dc77 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/ResultClsXml.java @@ -0,0 +1,44 @@ +/* + * 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.results.cls; + +public class ResultClsXml { + public ResultClsXml() { + } + + public String id; + public String description; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/TestResultClsAnnotation.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/TestResultClsAnnotation.java new file mode 100644 index 000000000..54edc509d --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/TestResultClsAnnotation.java @@ -0,0 +1,87 @@ +/* + * 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.results.cls; + +import java.util.Iterator; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; + +import org.apache.openjpa.jdbc.sql.DBDictionary; +import org.apache.openjpa.persistence.test.SQLListenerTestCase; + +public class TestResultClsAnnotation extends SQLListenerTestCase { + public void setUp() { + setUp(ResultClsAnnotation.class, CLEAR_TABLES); + assertNotNull(emf); + + populate(); + } + + public void testIt() { + EntityManager em = emf.createEntityManager(); + + try { + Query q = getQuery(em); + List result = q.getResultList(); + assertEquals(1, result.size()); + + for (Iterator it = result.iterator(); it.hasNext();) { + Object obj = (Object) it.next(); + ResultClsAnnotation ct = (ResultClsAnnotation) obj; + assertEquals("id1", ct.getId()); + assertEquals("description1", ct.getDescription()); + } + } catch (Exception ex) { + fail("unexpected exception: " + ex.getMessage()); + ex.printStackTrace(); + } finally { + em.close(); + } + } + + private void populate() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + + ResultClsAnnotation ct = new ResultClsAnnotation(); + ct.setId("id1"); + ct.setDescription("description1"); + em.persist(ct); + + em.getTransaction().commit(); + em.close(); + } + + private Query getQuery(EntityManager em) { + DBDictionary dict = getDBDictionary(); + Query query = null; + if (dict.getLeadingDelimiter().equals("\"") && dict.getTrailingDelimiter().equals("\"")) { + query = em.createNamedQuery("ResultClsQueryDoubleQuotes"); + } else if (dict.getLeadingDelimiter().equals("`") && dict.getTrailingDelimiter().equals("`")) { + query = em.createNamedQuery("ResultClsQueryBackTicks"); + } else if (dict.getLeadingDelimiter().equals("[") && dict.getTrailingDelimiter().equals("]")) { + query = em.createNamedQuery("ResultClsQueryBrackets"); + } else { + query = em.createNamedQuery("ResultClsQueryDefault"); + } + return query; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/TestResultClsXml.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/TestResultClsXml.java new file mode 100644 index 000000000..918b74945 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/results/cls/TestResultClsXml.java @@ -0,0 +1,94 @@ +/* + * 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.results.cls; + +import java.util.Iterator; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; + +import org.apache.openjpa.jdbc.sql.DBDictionary; +import org.apache.openjpa.persistence.test.SQLListenerTestCase; + +public class TestResultClsXml extends SQLListenerTestCase { + public void setUp() { + setUp(ResultClsXml.class, CLEAR_TABLES); + assertNotNull(emf); + + populate(); + } + + public void testIt() { + EntityManager em = emf.createEntityManager(); + + try { + Query q = getQuery(em); + + List result = q.getResultList(); + assertEquals(1, result.size()); + + for (Iterator it = result.iterator(); it.hasNext();) { + Object obj = (Object) it.next(); + ResultClsXml ct = (ResultClsXml) obj; + assertEquals("id1", ct.getId()); + assertEquals("description1", ct.getDescription()); + } + + } catch (Exception ex) { + fail("unexpected exception: " + ex.getMessage()); + ex.printStackTrace(); + + } finally { + em.close(); + } + } + + protected String getPersistenceUnitName() { + return "query-result"; + } + + private void populate() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + + ResultClsXml ct = new ResultClsXml(); + ct.setId("id1"); + ct.setDescription("description1"); + em.persist(ct); + + em.getTransaction().commit(); + em.close(); + } + + private Query getQuery(EntityManager em) { + DBDictionary dict = getDBDictionary(); + Query query = null; + if (dict.getLeadingDelimiter().equals("\"") && dict.getTrailingDelimiter().equals("\"")) { + query = em.createNamedQuery("ResultClsQueryDoubleQuotes"); + } else if (dict.getLeadingDelimiter().equals("`") && dict.getTrailingDelimiter().equals("`")) { + query = em.createNamedQuery("ResultClsQueryBackTicks"); + } else if (dict.getLeadingDelimiter().equals("[") && dict.getTrailingDelimiter().equals("]")) { + query = em.createNamedQuery("ResultClsQueryBrackets"); + } else { + query = em.createNamedQuery("ResultClsQueryDefault"); + } + return query; + } +} diff --git a/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml b/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml index 2198b556d..a3d1cba12 100644 --- a/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml +++ b/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml @@ -55,6 +55,7 @@ org/apache/openjpa/persistence/enhance/identity/mapsId-orm.xml org/apache/openjpa/persistence/entity/orm.xml META-INF/arrays-orm.xml + META-INF/query-result-orm.xml @@ -392,4 +393,14 @@ + + + META-INF/query-result-orm.xml + org.apache.openjpa.persistence.results.cls.ResultClsXml + + + + + diff --git a/openjpa-persistence-jdbc/src/test/resources/META-INF/query-result-orm.xml b/openjpa-persistence-jdbc/src/test/resources/META-INF/query-result-orm.xml new file mode 100644 index 000000000..b8dacf5d7 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/resources/META-INF/query-result-orm.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + ResultQueryDesc + + select "1","2" FROM ResultClsXmlEntity + + + select `1`,`2` FROM ResultClsXmlEntity + + + select [1],[2]" FROM ResultClsXmlEntity + + + + + + + + + + + + \ No newline at end of file