From 008b249c241dae1cc91a2c74f0621afc8947a5fa Mon Sep 17 00:00:00 2001 From: Patrick Linskey Date: Sun, 16 Dec 2007 12:30:15 +0000 Subject: [PATCH] OPENJPA-331. Checking in on behalf of Miroslav Nachev. Includes minor whitespace reformatting and @since tags for new source classes. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@604621 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/enhance/PCEnhancer.java | 15 +++- .../apache/openjpa/meta/ClassMetaData.java | 8 ++ .../apache/openjpa/util/ApplicationIds.java | 16 ++++ .../org/apache/openjpa/util/BigDecimalId.java | 72 ++++++++++++++++++ .../org/apache/openjpa/util/BigIntegerId.java | 73 +++++++++++++++++++ .../identity/SQLBigDecimalIdEntity.java | 48 ++++++++++++ .../identity/SQLBigIntegerIdEntity.java | 48 ++++++++++++ .../identity/TestSQLBigDecimalId.java | 59 +++++++++++++++ .../identity/TestSQLBigIntegerId.java | 60 +++++++++++++++ .../openjpa/persistence/JPAFacadeHelper.java | 16 +++- 10 files changed, 412 insertions(+), 3 deletions(-) create mode 100644 openjpa-kernel/src/main/java/org/apache/openjpa/util/BigDecimalId.java create mode 100644 openjpa-kernel/src/main/java/org/apache/openjpa/util/BigIntegerId.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/SQLBigDecimalIdEntity.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/SQLBigIntegerIdEntity.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestSQLBigDecimalId.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestSQLBigIntegerId.java diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java index aa45abee0..485e8b09d 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java @@ -47,7 +47,6 @@ import java.util.Set; import org.apache.commons.lang.StringUtils; import org.apache.openjpa.conf.OpenJPAConfiguration; import org.apache.openjpa.conf.OpenJPAConfigurationImpl; -import org.apache.openjpa.lib.conf.Configuration; import org.apache.openjpa.lib.conf.Configurations; import org.apache.openjpa.lib.log.Log; import org.apache.openjpa.lib.meta.ClassArgParser; @@ -65,6 +64,8 @@ import org.apache.openjpa.meta.MetaDataRepository; import org.apache.openjpa.meta.ValueStrategies; import org.apache.openjpa.util.GeneralException; import org.apache.openjpa.util.InternalException; +import org.apache.openjpa.util.BigDecimalId; +import org.apache.openjpa.util.BigIntegerId; import org.apache.openjpa.util.ByteId; import org.apache.openjpa.util.CharId; import org.apache.openjpa.util.DateId; @@ -2101,6 +2102,18 @@ public class PCEnhancer { code.invokevirtual().setMethod(StringId.class, "getId", String.class, null); break; + case JavaTypes.BIGDECIMAL: + code.aload().setLocal(oid); + code.checkcast().setType(BigDecimalId.class); + code.invokevirtual().setMethod(BigDecimalId.class, "getId", + BigDecimalId.class, null); + break; + case JavaTypes.BIGINTEGER: + code.aload().setLocal(oid); + code.checkcast().setType(BigIntegerId.class); + code.invokevirtual().setMethod(BigIntegerId.class, "getId", + BigIntegerId.class, null); + break; default: code.aload().setLocal(oid); code.checkcast().setType(ObjectId.class); diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java index ed9cbaf74..b746af8c0 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java @@ -46,6 +46,8 @@ import org.apache.openjpa.lib.meta.SourceTracker; import org.apache.openjpa.lib.util.J2DoPrivHelper; import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.lib.xml.Commentable; +import org.apache.openjpa.util.BigDecimalId; +import org.apache.openjpa.util.BigIntegerId; import org.apache.openjpa.util.ByteId; import org.apache.openjpa.util.CharId; import org.apache.openjpa.util.DateId; @@ -482,6 +484,12 @@ public class ClassMetaData case JavaTypes.OBJECT: _objectId = ObjectId.class; break; + case JavaTypes.BIGDECIMAL: + _objectId = BigDecimalId.class; + break; + case JavaTypes.BIGINTEGER: + _objectId = BigIntegerId.class; + break; } return _objectId; } diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java b/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java index f0776424f..b0314014d 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java @@ -20,6 +20,8 @@ package org.apache.openjpa.util; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.math.BigDecimal; +import java.math.BigInteger; import java.security.AccessController; import java.security.PrivilegedActionException; import java.util.Date; @@ -170,6 +172,14 @@ public class ApplicationIds { case JavaTypes.OID: case JavaTypes.OBJECT: return new ObjectId(meta.getDescribedType(), val); + case JavaTypes.BIGDECIMAL: + if (!convert && !(val instanceof BigDecimal)) + throw new ClassCastException("!(x instanceof BigDecimal)"); + return new BigDecimalId(meta.getDescribedType(), (BigDecimal)val); + case JavaTypes.BIGINTEGER: + if (!convert && !(val instanceof BigInteger)) + throw new ClassCastException("!(x instanceof BigInteger)"); + return new BigIntegerId(meta.getDescribedType(), (BigInteger)val); default: throw new InternalException(); } @@ -275,6 +285,12 @@ public class ApplicationIds { case JavaTypes.DATE: return new DateId(cls, ((DateId) oid).getId(), koid.hasSubclasses()); + case JavaTypes.BIGDECIMAL: + return new BigDecimalId(cls, ((BigDecimalId) oid).getId(), + koid.hasSubclasses()); + case JavaTypes.BIGINTEGER: + return new BigIntegerId(cls, ((BigIntegerId) oid).getId(), + koid.hasSubclasses()); default: throw new InternalException(); } diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/util/BigDecimalId.java b/openjpa-kernel/src/main/java/org/apache/openjpa/util/BigDecimalId.java new file mode 100644 index 000000000..299e2e615 --- /dev/null +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/util/BigDecimalId.java @@ -0,0 +1,72 @@ +/* + * Copyright 2007 Miroslav Nachev. + * + * Licensed 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. + * under the License. + */ +package org.apache.openjpa.util; + +import java.math.BigDecimal; + +/** + * @author Miroslav Nachev + * @since 1.1.0 + */ +public class BigDecimalId + extends OpenJPAId { + + private final BigDecimal key; + + public BigDecimalId(Class cls, String key) { + this(cls, (key == null) ? null : new BigDecimal(key)); + } + + public BigDecimalId(Class cls, BigDecimal key) { + super(cls); + this.key = key; + } + + public BigDecimalId(Class cls, BigDecimal key, boolean subs) { + super(cls, subs); + this.key = key; + } + + public BigDecimal getId() { + return key; + } + + public Object getIdObject() { + return key; + } + + public String toString() { + if (key == null) + return "NULL"; + + return key.toString(); + } + + protected int idHash() { + if (key != null) + return key.hashCode(); + + return 0; + } + + protected boolean idEquals(OpenJPAId other) { + if(key == null) + return false; + + return key.equals(other); + } +} diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/util/BigIntegerId.java b/openjpa-kernel/src/main/java/org/apache/openjpa/util/BigIntegerId.java new file mode 100644 index 000000000..33d9705ce --- /dev/null +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/util/BigIntegerId.java @@ -0,0 +1,73 @@ +/* + * Copyright 2007 Miroslav Nachev. + * + * Licensed 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. + * under the License. + */ +package org.apache.openjpa.util; + +import java.math.BigInteger; + +/** + * @author Miroslav Nachev + * @since 1.1.0 + */ +public class BigIntegerId + extends OpenJPAId { + + private final BigInteger key; + + public BigIntegerId(Class cls, String key) { + this(cls, (key == null) ? null : new BigInteger(key)); + } + + public BigIntegerId(Class cls, BigInteger key) { + super(cls); + this.key = key; + } + + public BigIntegerId(Class cls, BigInteger key, boolean subs) { + super(cls, subs); + this.key = key; + } + + public BigInteger getId() { + return key; + } + + public Object getIdObject() { + return key; + } + + public String toString() { + if (key == null) + return "NULL"; + + return key.toString(); + } + + protected int idHash() { + if (key != null) + return key.hashCode(); + + return 0; + } + + protected boolean idEquals(OpenJPAId other) { + if (key == null) + return false; + + return key.equals(other); + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/SQLBigDecimalIdEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/SQLBigDecimalIdEntity.java new file mode 100644 index 000000000..580d91627 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/SQLBigDecimalIdEntity.java @@ -0,0 +1,48 @@ +/* + * Copyright 2007 Miroslav Nachev. + * + * Licensed 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. + * under the License. + */ +package org.apache.openjpa.persistence.identity; + +import java.math.BigDecimal; +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * @author Miroslav Nachev + */ +@Entity +public class SQLBigDecimalIdEntity { + + @Id + private BigDecimal id; + private int data; + + public BigDecimal getId() { + return id; + } + + public void setId(BigDecimal id) { + this.id = id; + } + + public int getData() { + return data; + } + + public void setData(int data) { + this.data = data; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/SQLBigIntegerIdEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/SQLBigIntegerIdEntity.java new file mode 100644 index 000000000..a03479cef --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/SQLBigIntegerIdEntity.java @@ -0,0 +1,48 @@ +/* + * Copyright 2007 Miroslav Nachev. + * + * Licensed 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. + * under the License. + */ +package org.apache.openjpa.persistence.identity; + +import java.math.BigInteger; +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * @author Miroslav Nachev + */ +@Entity +public class SQLBigIntegerIdEntity { + + @Id + private BigInteger id; + private int data; + + public BigInteger getId() { + return id; + } + + public void setId(BigInteger id) { + this.id = id; + } + + public int getData() { + return data; + } + + public void setData(int data) { + this.data = data; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestSQLBigDecimalId.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestSQLBigDecimalId.java new file mode 100644 index 000000000..f392eebdd --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestSQLBigDecimalId.java @@ -0,0 +1,59 @@ +/* + * Copyright 2007 Miroslav Nachev. + * + * Licensed 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. + * under the License. + */ +package org.apache.openjpa.persistence.identity; + +import java.math.BigDecimal; +import javax.persistence.EntityManager; + +import junit.textui.TestRunner; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +/** + * @author Miroslav Nachev + */ +public class TestSQLBigDecimalId + extends SingleEMFTestCase { + + public void setUp() { + setUp(SQLBigDecimalIdEntity.class, CLEAR_TABLES); + } + + public void testPersist() { + long time = ((long) (System.currentTimeMillis() / 1000)) * 1000; + BigDecimal decimal = new BigDecimal(time); + + SQLBigDecimalIdEntity e = new SQLBigDecimalIdEntity(); + e.setId(decimal); + e.setData(1); + + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(e); + em.getTransaction().commit(); + assertEquals(time, e.getId().longValue()); + em.close(); + + em = emf.createEntityManager(); + e = em.find(SQLBigDecimalIdEntity.class, decimal); + assertEquals(1, e.getData()); + em.close(); + } + + public static void main(String[] args) { + TestRunner.run(SQLBigDecimalIdEntity.class); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestSQLBigIntegerId.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestSQLBigIntegerId.java new file mode 100644 index 000000000..228de098c --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestSQLBigIntegerId.java @@ -0,0 +1,60 @@ +/* + * Copyright 2007 Miroslav Nachev. + * + * Licensed 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. + * under the License. + */ +package org.apache.openjpa.persistence.identity; + +import java.math.BigDecimal; +import java.math.BigInteger; +import javax.persistence.EntityManager; + +import junit.textui.TestRunner; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +/** + * @author Miroslav Nachev + */ +public class TestSQLBigIntegerId + extends SingleEMFTestCase { + + public void setUp() { + setUp(SQLBigIntegerIdEntity.class, CLEAR_TABLES); + } + + public void testPersist() { + long time = ((long) (System.currentTimeMillis() / 1000)) * 1000; + BigInteger integer = new BigDecimal(time).toBigInteger(); + + SQLBigIntegerIdEntity e = new SQLBigIntegerIdEntity(); + e.setId(integer); + e.setData(1); + + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(e); + em.getTransaction().commit(); + assertEquals(time, e.getId().longValue()); + em.close(); + + em = emf.createEntityManager(); + e = em.find(SQLBigIntegerIdEntity.class, integer); + assertEquals(1, e.getData()); + em.close(); + } + + public static void main(String[] args) { + TestRunner.run(SQLBigIntegerIdEntity.class); + } +} diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/JPAFacadeHelper.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/JPAFacadeHelper.java index 2ccb875ff..f03f20817 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/JPAFacadeHelper.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/JPAFacadeHelper.java @@ -18,6 +18,8 @@ */ package org.apache.openjpa.persistence; +import java.math.BigDecimal; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -27,6 +29,8 @@ import javax.persistence.EntityManagerFactory; import org.apache.openjpa.kernel.Broker; import org.apache.openjpa.kernel.BrokerFactory; import org.apache.openjpa.meta.ClassMetaData; +import org.apache.openjpa.util.BigDecimalId; +import org.apache.openjpa.util.BigIntegerId; import org.apache.openjpa.util.ByteId; import org.apache.openjpa.util.CharId; import org.apache.openjpa.util.DoubleId; @@ -231,6 +235,10 @@ public class JPAFacadeHelper { return new ShortId(cls, (Short) oid); if (oid instanceof String) return new StringId(cls, (String) oid); + if (oid instanceof BigDecimal) + return new BigDecimalId(cls, (BigDecimal) oid); + if (oid instanceof BigInteger) + return new BigIntegerId(cls, (BigInteger) oid); return new ObjectId(cls, oid); } @@ -301,7 +309,11 @@ public class JPAFacadeHelper { if (oidClass == ShortId.class) return Short.class; if (oidClass == StringId.class) - return String.class; - return oidClass; + return String.class; + if (oidClass == BigDecimalId.class) + return BigDecimal.class; + if (oidClass == BigIntegerId.class) + return BigInteger.class; + return oidClass; } }