From 6ba6ce4e7ea01b1c7d73bcfd01ff0d9bca8d4863 Mon Sep 17 00:00:00 2001 From: Patrick Linskey Date: Wed, 22 Aug 2007 01:33:40 +0000 Subject: [PATCH] OpenJPA used to rely on being able to implicitly specify a value for a @GeneratedValue field when using an IDENTITY generation strategy. Now that we do not allow that behavior, OpenJPA was failing to generate IDs properly in some cases. Fixed. git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/1.0.0@568363 13f79535-47bb-0310-9956-ffa450edef68 --- .../kernel/PreparedStatementManagerImpl.java | 4 ++ ...ataCachingAndUnenhancedPropertyAccess.java | 55 +++++++++++++++++++ .../UnenhancedIdentityIdPropertyAccess.java | 55 +++++++++++++++++++ .../datacache/IdentityIdClass.java | 33 +++++++++++ .../TestAutoIncrementAndDataCaching.java | 53 ++++++++++++++++++ 5 files changed, 200 insertions(+) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/TestDataCachingAndUnenhancedPropertyAccess.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/UnenhancedIdentityIdPropertyAccess.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/IdentityIdClass.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestAutoIncrementAndDataCaching.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java index c2b92570d..a01899ea3 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java @@ -32,8 +32,10 @@ import org.apache.openjpa.jdbc.sql.RowImpl; import org.apache.openjpa.jdbc.sql.SQLExceptions; import org.apache.openjpa.kernel.OpenJPAStateManager; import org.apache.openjpa.lib.util.Localizer; +import org.apache.openjpa.util.ApplicationIds; import org.apache.openjpa.util.OpenJPAException; import org.apache.openjpa.util.OptimisticException; +import org.apache.openjpa.meta.ClassMetaData; /** * Basic prepared statement manager implementation. @@ -119,6 +121,8 @@ public class PreparedStatementManagerImpl mapping.assertJoinable(autoAssign[i]).setAutoAssignedValue(sm, _store, autoAssign[i], val); } + sm.setObjectId( + ApplicationIds.create(sm.getPersistenceCapable(), mapping)); } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/TestDataCachingAndUnenhancedPropertyAccess.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/TestDataCachingAndUnenhancedPropertyAccess.java new file mode 100644 index 000000000..489d2a52d --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/TestDataCachingAndUnenhancedPropertyAccess.java @@ -0,0 +1,55 @@ +/* + * 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.enhance; + +import java.util.List; +import javax.persistence.EntityManager; + +import org.apache.openjpa.persistence.OpenJPAEntityManager; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestDataCachingAndUnenhancedPropertyAccess + extends SingleEMFTestCase { + + @Override + public void setUp() { + setUp(UnenhancedIdentityIdPropertyAccess.class, CLEAR_TABLES, + "openjpa.DataCache", "true", + "openjpa.RemoteCommitProvider", "sjvm"); + } + + public void testSimpleDataCacheOperation() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(new UnenhancedIdentityIdPropertyAccess()); + em.getTransaction().commit(); + em.close(); + } + + public void testAccessIdBeforeCommit() { + OpenJPAEntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + UnenhancedIdentityIdPropertyAccess o = + new UnenhancedIdentityIdPropertyAccess(); + em.persist(o); + em.getObjectId(o); + em.getTransaction().commit(); + em.close(); + } +} \ No newline at end of file diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/UnenhancedIdentityIdPropertyAccess.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/UnenhancedIdentityIdPropertyAccess.java new file mode 100644 index 000000000..c1088af98 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/enhance/UnenhancedIdentityIdPropertyAccess.java @@ -0,0 +1,55 @@ +/* + * 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.enhance; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.PrePersist; + +import org.apache.openjpa.persistence.OpenJPAPersistence; + +@Entity +public class UnenhancedIdentityIdPropertyAccess { + + private int id; + private String stringField; + + public UnenhancedIdentityIdPropertyAccess() { + } + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getStringField() { + return stringField; + } + + public void setStringField(String stringField) { + this.stringField = stringField; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/IdentityIdClass.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/IdentityIdClass.java new file mode 100644 index 000000000..245915e70 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/IdentityIdClass.java @@ -0,0 +1,33 @@ +/* + * 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.datacache; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class IdentityIdClass { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + + private String stringField; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestAutoIncrementAndDataCaching.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestAutoIncrementAndDataCaching.java new file mode 100644 index 000000000..cc1484690 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestAutoIncrementAndDataCaching.java @@ -0,0 +1,53 @@ +/* + * 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.datacache; + +import javax.persistence.EntityManager; + +import org.apache.openjpa.persistence.OpenJPAEntityManager; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestAutoIncrementAndDataCaching + extends SingleEMFTestCase { + + @Override + public void setUp() { + setUp(IdentityIdClass.class, CLEAR_TABLES, + "openjpa.DataCache", "true", + "openjpa.RemoteCommitProvider", "sjvm"); + } + + public void testSimpleDataCacheOperation() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(new IdentityIdClass()); + em.getTransaction().commit(); + em.close(); + } + + public void testAccessIdBeforeCommit() { + OpenJPAEntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + IdentityIdClass o = new IdentityIdClass(); + em.persist(o); + em.getObjectId(o); + em.getTransaction().commit(); + em.close(); + } +} \ No newline at end of file