From 86e47000db1e081076ed16ef2873bb71e6e93c70 Mon Sep 17 00:00:00 2001 From: Patrick Linskey Date: Tue, 7 Aug 2007 23:12:56 +0000 Subject: [PATCH] OPENJPA-86 git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@563697 13f79535-47bb-0310-9956-ffa450edef68 --- .../persistence/xml/SimpleXmlEntity.java | 6 +++++- .../persistence/xml/TestSimpleXmlEntity.java | 15 +++++++++++++ .../apache/openjpa/persistence/xml/orm.xml | 4 +++- .../AnnotationPersistenceMetaDataParser.java | 21 +++++++++++++------ .../XMLPersistenceMetaDataParser.java | 20 +++++------------- 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/SimpleXmlEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/SimpleXmlEntity.java index 166a60b42..4e9d47707 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/SimpleXmlEntity.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/SimpleXmlEntity.java @@ -20,10 +20,14 @@ package org.apache.openjpa.persistence.xml; public class SimpleXmlEntity { - private long id; + private String id; private int version; private String stringField; + public String getId() { + return id; + } + public String getStringField() { return stringField; } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/TestSimpleXmlEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/TestSimpleXmlEntity.java index 4534a0a6a..18a5f0374 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/TestSimpleXmlEntity.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xml/TestSimpleXmlEntity.java @@ -28,6 +28,21 @@ public class TestSimpleXmlEntity setUp(SimpleXmlEntity.class); } + public void testId() { + em.getTransaction().begin(); + SimpleXmlEntity e = new SimpleXmlEntity(); + em.persist(e); + em.flush(); + assertNotNull(e.getId()); + try { + Integer.parseInt(e.getId()); + fail("uuid-based id should not be an integer; was " + e.getId()); + } catch (NumberFormatException nfe) { + // expected + } + em.getTransaction().rollback(); + } + public void testNamedQueryInXmlNamedEntity() { em.createNamedQuery("SimpleXml.findAll").getResultList(); } diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/xml/orm.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/xml/orm.xml index bf8d014ea..962b1c5ea 100644 --- a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/xml/orm.xml +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/xml/orm.xml @@ -32,7 +32,9 @@ select o from SimpleXmlEntity o - + + + diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java index a4e07c6dc..b23e83cb3 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java @@ -1110,21 +1110,30 @@ public class AnnotationPersistenceMetaDataParser * Sets value generation information for the given field. */ private void parseGeneratedValue(FieldMetaData fmd, GeneratedValue gen) { - int strat = getGeneratedValueStrategy(fmd, gen.strategy(), - gen.generator()); + GenerationType strategy = gen.strategy(); + String generator = gen.generator(); + parseGeneratedValue(fmd, strategy, generator); + } + + /** + * Sets value generation information for the given field. + */ + static void parseGeneratedValue(FieldMetaData fmd, GenerationType strategy, + String generator) { + int strat = getGeneratedValueStrategy(fmd, strategy, generator); if (strat != -1) fmd.setValueStrategy(strat); else { - switch (gen.strategy()) { + switch (strategy) { case TABLE: case SEQUENCE: // technically we should have separate system table and // sequence generators, but it's easier to just rely on // the system org.apache.openjpa.Sequence setting for both - if (StringUtils.isEmpty(gen.generator())) + if (StringUtils.isEmpty(generator)) fmd.setValueSequenceName(SequenceMetaData.NAME_SYSTEM); else - fmd.setValueSequenceName(gen.generator()); + fmd.setValueSequenceName(generator); break; case AUTO: fmd.setValueSequenceName(SequenceMetaData.NAME_SYSTEM); @@ -1133,7 +1142,7 @@ public class AnnotationPersistenceMetaDataParser fmd.setValueStrategy(ValueStrategies.AUTOASSIGN); break; default: - throw new UnsupportedException(gen.strategy().toString()); + throw new UnsupportedException(strategy.toString()); } } } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java index ed3d9d6e1..ff5e18b29 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/XMLPersistenceMetaDataParser.java @@ -31,6 +31,7 @@ import java.util.Map; import java.util.Set; import java.util.Stack; import javax.persistence.CascadeType; +import javax.persistence.GenerationType; import static javax.persistence.CascadeType.*; import org.apache.commons.lang.StringUtils; @@ -62,12 +63,9 @@ import org.apache.openjpa.meta.Order; import org.apache.openjpa.meta.QueryMetaData; import org.apache.openjpa.meta.SequenceMetaData; import org.apache.openjpa.meta.ValueMetaData; -import org.apache.openjpa.meta.ValueStrategies; import static org.apache.openjpa.persistence.MetaDataTag.*; import static org.apache.openjpa.persistence.PersistenceStrategy.*; import org.apache.openjpa.util.ImplHelper; -import org.apache.openjpa.util.UnsupportedException; -import serp.util.Strings; /** * Custom SAX parser used by the system to quickly parse persistence i @@ -992,20 +990,12 @@ public class XMLPersistenceMetaDataParser String strategy = attrs.getValue("strategy"); String generator = attrs.getValue("generator"); + GenerationType type = StringUtils.isEmpty(strategy) + ? GenerationType.AUTO : GenerationType.valueOf(strategy); - // TODO UUID_HEX / UUID_STRING FieldMetaData fmd = (FieldMetaData) currentElement(); - if (StringUtils.isEmpty(strategy) || "AUTO".equals(strategy)) - fmd.setValueSequenceName(SequenceMetaData.NAME_SYSTEM); - else if ("TABLE".equals(strategy) || "SEQUENCE".equals(strategy)) { - if (StringUtils.isEmpty(generator)) - fmd.setValueSequenceName(SequenceMetaData.NAME_SYSTEM); - else - fmd.setValueSequenceName(generator); - } else if ("IDENTITY".equals(strategy)) - fmd.setValueStrategy(ValueStrategies.AUTOASSIGN); - else - throw new UnsupportedException(strategy); + AnnotationPersistenceMetaDataParser.parseGeneratedValue(fmd, type, + generator); return true; }