You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by di...@apache.org on 2010/07/15 18:26:27 UTC
svn commit: r964480 - in /openjpa/trunk:
openjpa-kernel/src/main/java/org/apache/openjpa/meta/
openjpa-kernel/src/main/java/org/apache/openjpa/util/
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/
Author: dianner
Date: Thu Jul 15 16:26:27 2010
New Revision: 964480
URL: http://svn.apache.org/viewvc?rev=964480&view=rev
Log:
OPENJPA-1554 Support boolean as an id (required by spec)
Added:
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/BooleanId.java (with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/BooleanIdEntity.java (with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanId.java (with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanIdEntity.java (with props)
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestBooleanId.java (with props)
Modified:
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java
Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java?rev=964480&r1=964479&r2=964480&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java Thu Jul 15 16:26:27 2010
@@ -52,6 +52,7 @@ import org.apache.openjpa.lib.util.Local
import org.apache.openjpa.lib.xml.Commentable;
import org.apache.openjpa.util.BigDecimalId;
import org.apache.openjpa.util.BigIntegerId;
+import org.apache.openjpa.util.BooleanId;
import org.apache.openjpa.util.ByteId;
import org.apache.openjpa.util.CharId;
import org.apache.openjpa.util.DateId;
@@ -531,6 +532,10 @@ public class ClassMetaData
case JavaTypes.BIGINTEGER:
_objectId = BigIntegerId.class;
break;
+ case JavaTypes.BOOLEAN:
+ case JavaTypes.BOOLEAN_OBJ:
+ _objectId = BooleanId.class;
+ break;
}
return _objectId;
}
Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java?rev=964480&r1=964479&r2=964480&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ApplicationIds.java Thu Jul 15 16:26:27 2010
@@ -203,6 +203,12 @@ public class ApplicationIds {
"!(x instanceof BigInteger)");
return new BigIntegerId(meta.getDescribedType(),
(BigInteger)val);
+ case JavaTypes.BOOLEAN:
+ case JavaTypes.BOOLEAN_OBJ:
+ if (!convert && !(val instanceof Boolean))
+ throw new ClassCastException("!(x instanceof Boolean)");
+ return new BooleanId(meta.getDescribedType(),
+ val == null ? false : (Boolean)val);
default:
throw new InternalException();
}
Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/BooleanId.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/BooleanId.java?rev=964480&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/BooleanId.java (added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/BooleanId.java Thu Jul 15 16:26:27 2010
@@ -0,0 +1,54 @@
+package org.apache.openjpa.util;
+
+/**
+ * {@link OpenJPAId} subclass appropriate for boolean fields.
+ *
+ * @author Dianne Richards
+ * @since 2.1.0
+ */
+public class BooleanId extends OpenJPAId {
+
+ private final boolean key;
+
+ public BooleanId(Class cls, Boolean key) {
+ this(cls, key.booleanValue());
+ }
+
+ public BooleanId(Class cls, String key) {
+ this(cls, Boolean.parseBoolean(key));
+ }
+
+ public BooleanId(Class cls, boolean key) {
+ super(cls);
+ this.key = key;
+ }
+
+ public BooleanId(Class cls, boolean key, boolean subs) {
+ super(cls, subs);
+ this.key = key;
+ }
+
+ public boolean getId() {
+ return key;
+ }
+
+ @Override
+ public Object getIdObject() {
+ return Boolean.valueOf(key);
+ }
+
+ public String toString() {
+ return Boolean.toString(key);
+ }
+
+ @Override
+ protected boolean idEquals(OpenJPAId other) {
+ return key == ((BooleanId) other).key;
+ }
+
+ @Override
+ protected int idHash() {
+ return Boolean.valueOf(key).hashCode();
+ }
+
+}
Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/BooleanId.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/BooleanIdEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/BooleanIdEntity.java?rev=964480&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/BooleanIdEntity.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/BooleanIdEntity.java Thu Jul 15 16:26:27 2010
@@ -0,0 +1,68 @@
+/*
+ * 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.identity;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * An entity containing a boolean id
+ *
+ * @author Dianne Richards
+ * @since 2.1.0
+ */
+@Entity
+public class BooleanIdEntity {
+
+ @Id
+ private boolean id;
+ private String name;
+ private boolean otherBoolean;
+
+ public BooleanIdEntity() {}
+
+ public BooleanIdEntity(boolean id, String name) {
+ this.id = id;
+ this.name = name;
+ }
+
+ public boolean getId() {
+ return id;
+ }
+
+ public void setId(boolean id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public boolean getOtherBoolean() {
+ return otherBoolean;
+ }
+
+ public void setOtherBoolean(boolean otherBoolean) {
+ this.otherBoolean = otherBoolean;
+ }
+}
Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/BooleanIdEntity.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanId.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanId.java?rev=964480&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanId.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanId.java Thu Jul 15 16:26:27 2010
@@ -0,0 +1,75 @@
+/*
+ * 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.identity;
+
+/**
+ * Compound id containing a boolean value
+ *
+ * @author Dianne Richards
+ * @since 2.1.0
+ */
+public class CompoundBooleanId {
+ public String stringId;
+ public boolean booleanId;
+
+ @Override
+ public int hashCode() {
+ int result = 1;
+ result = result + ((stringId == null) ? 0 : stringId.hashCode());
+ result = result + ((booleanId == false) ? 0 : 1);
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ CompoundBooleanId other = (CompoundBooleanId) obj;
+ if (stringId == null && other.stringId != null)
+ return false;
+ else if (!stringId.equals(other.stringId))
+ return false;
+ if (booleanId != other.booleanId)
+ return false;
+
+
+ return true;
+ }
+
+ public String getStringId() {
+ return stringId;
+ }
+
+ public void setStringId(String stringId) {
+ this.stringId = stringId;
+ }
+
+ public boolean isBooleanId() {
+ return booleanId;
+ }
+
+ public void setBooleanId(boolean booleanId) {
+ this.booleanId = booleanId;
+ }
+
+}
Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanId.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanIdEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanIdEntity.java?rev=964480&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanIdEntity.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanIdEntity.java Thu Jul 15 16:26:27 2010
@@ -0,0 +1,58 @@
+/*
+ * 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.identity;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.IdClass;
+
+/**
+ * An entity containing a compound id with a boolean field
+ *
+ * @author Dianne Richards
+ * @since 2.1.0
+ */
+@Entity
+@IdClass(CompoundBooleanId.class)
+public class CompoundBooleanIdEntity {
+ @Id
+ private String stringId;
+
+ @Id
+ private boolean booleanId;
+
+ public void CompoundIdEntity() {}
+
+ public String getStringId() {
+ return stringId;
+ }
+
+ public void setStringId(String stringId) {
+ this.stringId = stringId;
+ }
+
+ public boolean getBooleanId() {
+ return booleanId;
+ }
+
+ public void setBooleanId(boolean booleanId) {
+ this.booleanId = booleanId;
+ }
+
+}
Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/CompoundBooleanIdEntity.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestBooleanId.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestBooleanId.java?rev=964480&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestBooleanId.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestBooleanId.java Thu Jul 15 16:26:27 2010
@@ -0,0 +1,115 @@
+/*
+ * 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.identity;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+/**
+ * Test that entities can use boolean ids
+ *
+ * @author Dianne Richards
+ * @since 2.1.0
+ */
+public class TestBooleanId extends SQLListenerTestCase {
+ private EntityManager em;
+ private BooleanIdEntity se;
+ private CompoundBooleanIdEntity ce;
+
+ public void setUp() throws Exception {
+ super.setUp(BooleanIdEntity.class,
+ CompoundBooleanIdEntity.class,
+ DROP_TABLES);
+ assertTrue(emf != null);
+ }
+
+ public void testSimpleBooleanIdEntity() {
+ se = new BooleanIdEntity(true,"name");
+
+ em = emf.createEntityManager();
+ em.getTransaction().begin();
+ em.persist(se);
+ em.getTransaction().commit();
+ assertEquals(true, se.getId());
+ em.close();
+
+ em = emf.createEntityManager();
+ se = em.find(BooleanIdEntity.class, true);
+ assertNotNull(se);
+ }
+
+ public void testCompoundIdEntity() {
+ em = emf.createEntityManager();
+ testCompoundIdEntity("string1", true, true);
+ testCompoundIdEntity("string1", true, false); // Expect duplicate key exception
+ testCompoundIdEntity("string1", false, true);
+ testCompoundIdEntity("string2", true, true);
+ testCompoundIdEntity("string2", false, true);
+ createCompoundIdEntityWithoutBoolean("string3");
+ em.close();
+ }
+
+ private void testCompoundIdEntity(String stringId, boolean booleanId, boolean expectSuccess) {
+ // create and persist the entity
+ try {
+ em.getTransaction().begin();
+ ce = new CompoundBooleanIdEntity();
+ ce.setStringId(stringId);
+ ce.setBooleanId(booleanId);
+ em.persist(ce);
+ em.getTransaction().commit();
+ } catch (Throwable e) {
+ assertFalse(expectSuccess);
+ em.getTransaction().rollback();
+ }
+ ce = null;
+ em.clear();
+
+ if (expectSuccess) {
+ // get the entity
+ em.getTransaction().begin();
+ CompoundBooleanId cpdId = new CompoundBooleanId();
+ cpdId.setBooleanId(booleanId);
+ cpdId.setStringId(stringId);
+ ce = em.find(CompoundBooleanIdEntity.class, cpdId);
+ assertNotNull(ce);
+ em.getTransaction().commit();
+ }
+ }
+
+ private void createCompoundIdEntityWithoutBoolean(String stringId) {
+ // create and persist the entity
+ em.getTransaction().begin();
+ ce = new CompoundBooleanIdEntity();
+ ce.setStringId(stringId);
+ em.persist(ce);
+ em.getTransaction().commit();
+ ce = null;
+ em.clear();
+
+ // get the entity
+ em.getTransaction().begin();
+ CompoundBooleanId cpdId = new CompoundBooleanId();
+ cpdId.setStringId(stringId);
+ ce = em.find(CompoundBooleanIdEntity.class, cpdId);
+ assertNotNull(ce);
+ em.getTransaction().commit();
+ }
+}
Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestBooleanId.java
------------------------------------------------------------------------------
svn:eol-style = native