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.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@813597 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Kevin W. Sutter 2009-09-10 21:35:57 +00:00
parent fa49080f8f
commit dac8af35b4
3 changed files with 153 additions and 3 deletions

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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();
}
}