OpenJPA-185 allow optional to be overriden by xml descriptor.

git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@524921 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2007-04-02 21:14:48 +00:00
parent 8167400e51
commit 57f9f3af1d
4 changed files with 108 additions and 1 deletions

View File

@ -0,0 +1,49 @@
package org.apache.openjpa.persistence.xml;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.InvalidStateException;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestXmlOverrideEntity extends SingleEMFTestCase {
public void setUp() {
setUp(XmlOverrideEntity.class);
}
/**
* Tests that the optional attribute on a basic field can be overrided by
* an xml descriptor.
*
* XmlOverrideEntity.name is annotated with optional=false
* XmlOverrideEntity.description is annotated with optional=true.
*
* The optional attributes are reversed in orm.xml.
*/
public void testOptionalAttributeOverride() {
EntityManager em = emf.createEntityManager();
XmlOverrideEntity optional = new XmlOverrideEntity();
optional.setName(null);
optional.setDescription("description");
em.getTransaction().begin();
em.persist(optional);
em.getTransaction().commit();
try {
em.getTransaction().begin();
optional.setDescription(null);
em.getTransaction().commit();
fail("XmlOrverrideEntity.description should not be optional. "
+ "Expecting an InvalidStateException.");
} catch (InvalidStateException e) {
}
em.getTransaction().begin();
em.remove(em.find(XmlOverrideEntity.class, optional.getId()));
em.getTransaction().commit();
}
}

View File

@ -0,0 +1,45 @@
package org.apache.openjpa.persistence.xml;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class XmlOverrideEntity {
@Id
@GeneratedValue
int id;
@Basic(optional=false)
String name;
@Basic(optional=true)
String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -20,4 +20,11 @@
<version name="version"/>
</attributes>
</entity>
</entity-mappings>
<entity name="XmlOverride" class="XmlOverrideEntity">
<attributes>
<basic name="name" optional="true"></basic>
<basic name="description" optional="false"></basic>
</attributes>
</entity>
</entity-mappings>

View File

@ -1196,6 +1196,12 @@ public class XMLPersistenceMetaDataParser
val = attrs.getValue("optional");
if ("false".equals(val))
fmd.setNullValue(FieldMetaData.NULL_EXCEPTION);
else if ("true".equals(val)
&& fmd.getNullValue() == FieldMetaData.NULL_EXCEPTION) {
// Reset value if the field was annotated with optional=false.
// Otherwise leave it alone.
fmd.setNullValue(FieldMetaData.NULL_UNSET);
}
if (isMappingOverrideMode()) {
val = attrs.getValue("mapped-by");
if (val != null)