From 2765b756bc471d7aae762131d3353bc5587db04f Mon Sep 17 00:00:00 2001 From: "Richard G. Curtis" Date: Mon, 10 May 2010 19:46:21 +0000 Subject: [PATCH] OPENJPA-1612: Check for having a relationship to a type that is mapped to a blob field. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@942871 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/jdbc/meta/MappingRepository.java | 7 ++++ .../openjpa/jdbc/meta/localizer.properties | 3 +- .../jdbc/common/apps/ConstantJoinPC4.java | 2 +- .../kernel/common/apps/BlobTest.java | 7 ++-- .../meta/InvalidMappingFieldEntity.java | 37 ++++++++++++++++ .../meta/TestMappingRepository.java | 42 +++++++++++++++++++ 6 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/InvalidMappingFieldEntity.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMappingRepository.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java index 61d348143..29db54608 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java @@ -883,6 +883,13 @@ public class MappingRepository extends MetaDataRepository { // default to blob if (installHandlers) { + int type = field.getAssociationType(); + // Having a ONE_TO_ONE, ONE_TO_MANY, MANY_TO_ONE, or MANY_TO_MANY relationship + // type as a blob is incorrect + if (type >= FieldMetaData.ONE_TO_ONE && type <= FieldMetaData.MANY_TO_MANY) { + throw new UserException(_loc.get("invalid-mapping", field)); + } + if (getLog().isWarnEnabled()) getLog().warn(_loc.get("no-field-strategy", field)); field.setSerialized(true); diff --git a/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties b/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties index d74dfca82..1aa0f6156 100644 --- a/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties +++ b/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/meta/localizer.properties @@ -417,4 +417,5 @@ unique-no-table: A unique constraint on table "{0}" can not be added to \ table "{2}" nor any of its secondary table(s) "{3}". bad-version-column-table: One of the version column "{0}" has been associated \ with table "{1}", but no primary or secondary table of such name exists. -version-type-unsupported: Version field "{0}" of {1} is not supported. \ No newline at end of file +version-type-unsupported: Version field "{0}" of {1} is not supported. +invalid-mapping: The field "{0}" is an invalid type. Relationship fields must be a valid Entity type. diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/common/apps/ConstantJoinPC4.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/common/apps/ConstantJoinPC4.java index 71f79b9bb..0eb27a412 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/common/apps/ConstantJoinPC4.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/common/apps/ConstantJoinPC4.java @@ -34,7 +34,7 @@ public class ConstantJoinPC4 @OneToOne(cascade={CascadeType.PERSIST, CascadeType.REMOVE}) private ConstantJoinPC5 oneToOne1; - @ManyToMany + private Set manyToMany = new HashSet (); public ConstantJoinPC4() diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/BlobTest.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/BlobTest.java index e2716409e..e188cdbd1 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/BlobTest.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/BlobTest.java @@ -18,12 +18,14 @@ */ package org.apache.openjpa.persistence.kernel.common.apps; +import javax.persistence.Basic; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.Lob; import javax.persistence.OneToOne; @Entity @@ -34,13 +36,12 @@ public class BlobTest { private int id; @Column(name = "blobtab") - @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.REMOVE }) - private Object blob; + private byte[] blob; public BlobTest() { } - public void setBlob(Object blob) { + public void setBlob(byte[] blob) { this.blob = blob; } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/InvalidMappingFieldEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/InvalidMappingFieldEntity.java new file mode 100644 index 000000000..4d3c3a8cb --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/InvalidMappingFieldEntity.java @@ -0,0 +1,37 @@ +/* + * 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.meta; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.OneToOne; + +@Entity +public class InvalidMappingFieldEntity { + + @Id + int id; + + @ManyToOne + Object o; + + @OneToOne + InvalidMappingFieldEntity other; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMappingRepository.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMappingRepository.java new file mode 100644 index 000000000..be697237c --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/TestMappingRepository.java @@ -0,0 +1,42 @@ +/* + * 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.meta; + +import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; +import org.apache.openjpa.util.UserException; + +public class TestMappingRepository extends SingleEMFTestCase { + @Override + protected void setUp(Object... props) { + super.setUp(InvalidMappingFieldEntity.class, "openjpa.jdbc.SynchronizeMappings", "false"); + } + + public void test() { + try { + OpenJPAEntityManagerSPI em = emf.createEntityManager(); + emf.getConfiguration().getMetaDataRepositoryInstance().getMetaData(InvalidMappingFieldEntity.class, null, + true); + fail("Shouldn't be able to create an em with this Entity type."); + } catch (UserException e) { + // expected + } + + } +}