diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java index e46be9450..40ef4a00b 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java @@ -3670,9 +3670,25 @@ public class BrokerImpl 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); } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/EntityE2.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/EntityE2.java new file mode 100644 index 000000000..8bc2ddb12 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/EntityE2.java @@ -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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java new file mode 100644 index 000000000..f58194b3f --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestNativeSeqGenerator.java @@ -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(); + } +}