Author: kwsutter
Date: Thu Sep 10 21:35:57 2009
New Revision: 813597
URL: http://svn.apache.org/viewvc?rev=813597&view=rev
Log:
OPENJPA-1260. Committing this change for Ravi Palacherla. Mike had previously
reviewed the patch for correctness. Ravi changed the patch slightly and
re-submitted. Based on a request from the mailing list, I reviewed Ravi's
second patch and am committing the changes with a couple of minor
modifications. I updated the comments slightly to make sentences. And, I
moved the testcases to the "generationtype" package to be with similar
testcases.
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/EntityE2.java
(with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java
(with props)
Modified:
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
Modified:
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?rev=813597&r1=813596&r2=813597&view=diff
==============================================================================
---
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
(original)
+++
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
Thu Sep 10 21:35:57 2009
@@ -3670,9 +3670,25 @@
default:
// use store manager for native sequence
if (fmd == null) {
- // this will return a sequence even for app id classes,
- // which is what we want for backwards-compatibility
- return _store.getDataStoreIdSequence(meta);
+ // This will return a sequence even for app id classes,
+ // which is what we want for backwards-compatibility.
+ // Even if user uses Application Identity,
+ // user might use custom sequence information.
+ // So, first, the sequence should be checked.
+ // Trying to get primary key field if it has
+ // sequence meta data.
+ FieldMetaData[] pks = meta.getPrimaryKeyFields();
+ if (pks != null && pks.length == 1) {
+ smd = pks[0].getValueSequenceMetaData();
+ } else {
+ smd = meta.getIdentitySequenceMetaData();
+ }
+
+ if (smd != null) {
+ return smd.getInstance(_loader);
+ } else {
+ return _store.getDataStoreIdSequence(meta);
+ }
}
return _store.getValueSequence(fmd);
}
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/EntityE2.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/EntityE2.java?rev=813597&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/EntityE2.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/EntityE2.java
Thu Sep 10 21:35:57 2009
@@ -0,0 +1,69 @@
+/*
+ * 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.generationtype;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.SequenceGenerator;
+import javax.persistence.Table;
+
+...@entity
+...@table(name="primary_entityE2")
+public class EntityE2 {
+ @Id
+ @SequenceGenerator(name="entityE2_seq_gen_name",
+ sequenceName="entityE2_seq_gen")
+ @GeneratedValue(strategy=GenerationType.SEQUENCE,
+ generator="entityE2_seq_gen_name")
+ private int id;
+ @Column(name="e_name")
+ private String name;
+
+ public EntityE2(String name) {
+ this.name = name;
+ }
+
+ /**
+ * @return the id
+ */
+ public int getId() {
+ return id;
+ }
+ /**
+ * @param id the id to set
+ */
+ public void setId(int id) {
+ this.id = id;
+ }
+ /**
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+ /**
+ * @param name the name to set
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/EntityE2.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java?rev=813597&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java
Thu Sep 10 21:35:57 2009
@@ -0,0 +1,65 @@
+/*
+ * 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.generationtype;
+
+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;
+
+public class TestNativeSeqGenerator extends SQLListenerTestCase {
+ OpenJPAEntityManager em;
+ JDBCConfiguration conf;
+ DBDictionary dict;
+ boolean supportsNativeSequence = false;
+
+ EntityE2 entityE2;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp(EntityE2.class,DROP_TABLES);
+ assertNotNull(emf);
+ conf = (JDBCConfiguration) emf.getConfiguration();
+ dict = conf.getDBDictionaryInstance();
+ supportsNativeSequence = dict.nextSequenceQuery != null;
+ if (supportsNativeSequence) {
+ em = emf.createEntityManager();
+ assertNotNull(em);
+ }
+ }
+
+ public void createEntityE2() {
+ entityE2 = new EntityE2("e name");
+ }
+
+ public void testGetIdGeneratorSeqGen() {
+ if (!supportsNativeSequence) {
+ System.out.println("Does not support native sequence");
+ return;
+ }
+ createEntityE2();
+ em.getTransaction().begin();
+ em.persist(entityE2);
+ em.getTransaction().commit();
+ int genId = entityE2.getId();
+ int nextId =
(int)((Long)em.getIdGenerator(EntityE2.class).next()).longValue();
+ assertTrue("Next value should depend on previous genid", nextId ==
genId + 1);
+ em.close();
+ }
+}
Propchange:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java
------------------------------------------------------------------------------
svn:eol-style = native