mirror of https://github.com/apache/openjpa.git
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
This commit is contained in:
parent
7bd70d36db
commit
008b249c24
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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 <a href="mailto:mnachev@gmail.com">Miroslav Nachev</a>
|
||||
* @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);
|
||||
}
|
||||
}
|
|
@ -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 <a href="mailto:mnachev@gmail.com">Miroslav Nachev</a>
|
||||
* @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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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 <a href="mailto:mnachev@gmail.com">Miroslav Nachev</a>
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -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 <a href="mailto:mnachev@gmail.com">Miroslav Nachev</a>
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -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 <a href="mailto:mnachev@gmail.com">Miroslav Nachev</a>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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 <a href="mailto:mnachev@gmail.com">Miroslav Nachev</a>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -302,6 +310,10 @@ public class JPAFacadeHelper {
|
|||
return Short.class;
|
||||
if (oidClass == StringId.class)
|
||||
return String.class;
|
||||
if (oidClass == BigDecimalId.class)
|
||||
return BigDecimal.class;
|
||||
if (oidClass == BigIntegerId.class)
|
||||
return BigInteger.class;
|
||||
return oidClass;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue