mirror of https://github.com/apache/openjpa.git
OPENJPA-416
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@587943 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a282456a91
commit
9e5cc06fcb
|
@ -63,6 +63,8 @@ import org.apache.openjpa.util.MetaDataException;
|
|||
import org.apache.openjpa.util.OpenJPAException;
|
||||
import org.apache.openjpa.util.UnsupportedException;
|
||||
import org.apache.openjpa.util.ImplHelper;
|
||||
import org.apache.openjpa.util.UserException;
|
||||
|
||||
import serp.util.Strings;
|
||||
|
||||
/**
|
||||
|
@ -1232,8 +1234,16 @@ public class FieldMetaData
|
|||
*/
|
||||
public Object getExternalValue(Object val, StoreContext ctx) {
|
||||
Map extValues = getExternalValueMap();
|
||||
if (extValues != null)
|
||||
return extValues.get(val);
|
||||
if (extValues != null) {
|
||||
Object foundVal = extValues.get(val);
|
||||
if (foundVal == null) {
|
||||
throw new UserException(_loc.get("bad-externalized-value",
|
||||
new Object[] { val, extValues.keySet(), this }))
|
||||
.setFatal(true).setFailedObject(val);
|
||||
} else {
|
||||
return foundVal;
|
||||
}
|
||||
}
|
||||
|
||||
Method externalizer = getExternalizerMethod();
|
||||
if (externalizer == null)
|
||||
|
|
|
@ -310,3 +310,5 @@ interface-load: Dynamic implementation of managed "{0}" can not be defined \
|
|||
interface-load2: Enhanced dynamic implementaion of managed "{0}" can not be \
|
||||
defined using classloader "{1}". See nested exception for details.
|
||||
interface-badenhance: Dynamic implementaion of "{0}" can not be enhanced.
|
||||
bad-externalized-value: Value "{0}" was not found in the list of \
|
||||
ExternalValues for field "{2}". Valid values are {1}
|
||||
|
|
69
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/external/EntityA.java
vendored
Normal file
69
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/external/EntityA.java
vendored
Normal file
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* 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.external;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.apache.openjpa.persistence.ExternalValues;
|
||||
import org.apache.openjpa.persistence.Type;
|
||||
|
||||
@Entity
|
||||
@Table(name = "ExternalizationEntityA")
|
||||
public class EntityA implements java.io.Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
@ExternalValues( { "SMALL=SML", "MEDIUM=MID", "LARGE=LRG" })
|
||||
@Column(length = 3)
|
||||
private String s1;
|
||||
|
||||
@ExternalValues( { "SMALL=5", "MEDIUM=8", "LARGE=15" })
|
||||
@Type(int.class)
|
||||
private String s2;
|
||||
|
||||
public EntityA() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getS1() {
|
||||
return s1;
|
||||
}
|
||||
|
||||
public void setS1(String s1) {
|
||||
this.s1 = s1;
|
||||
}
|
||||
|
||||
public String getS2() {
|
||||
return s2;
|
||||
}
|
||||
|
||||
public void setS2(String s2) {
|
||||
this.s2 = s2;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* 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.external;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.apache.openjpa.persistence.PersistenceException;
|
||||
import org.apache.openjpa.persistence.RollbackException;
|
||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||
|
||||
public class TestExternalValues extends SingleEMFTestCase {
|
||||
|
||||
public void setUp() {
|
||||
super.setUp(EntityA.class);
|
||||
}
|
||||
|
||||
public void testUnrecognizedExternalValue() {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
|
||||
em.getTransaction().begin();
|
||||
|
||||
EntityA entity = new EntityA();
|
||||
|
||||
entity.setS1("ABDEF");
|
||||
entity.setS2("NOT_VALID");
|
||||
|
||||
em.persist(entity);
|
||||
|
||||
try {
|
||||
em.getTransaction().commit();
|
||||
fail("Expected an exception at commit time");
|
||||
} catch (RollbackException e) {
|
||||
Throwable t = e;
|
||||
while (t.getCause() != null) {
|
||||
t = t.getCause();
|
||||
}
|
||||
assertTrue(t.getMessage().contains(
|
||||
"was not found in the list of ExternalValues"));
|
||||
} finally {
|
||||
if (em.getTransaction().isActive()) {
|
||||
em.getTransaction().rollback();
|
||||
}
|
||||
}
|
||||
em.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue