mirror of https://github.com/apache/openjpa.git
OPENJPA-665: Sets column i/o conditions accordingly when column is set as not nullable.
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@679487 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
315cc2d155
commit
1111ee16ff
|
@ -527,13 +527,16 @@ public abstract class MappingInfo
|
|||
*/
|
||||
private void setIOFromColumnFlags(Column col, int i) {
|
||||
if (col == null || (!col.getFlag(Column.FLAG_UNINSERTABLE)
|
||||
&& !col.getFlag(Column.FLAG_UNUPDATABLE)))
|
||||
&& !col.getFlag(Column.FLAG_UNUPDATABLE)
|
||||
&& !col.isNotNull()))
|
||||
return;
|
||||
|
||||
if (_io == null)
|
||||
_io = new ColumnIO();
|
||||
_io.setInsertable(i, !col.getFlag(Column.FLAG_UNINSERTABLE));
|
||||
_io.setUpdatable(i, !col.getFlag(Column.FLAG_UNUPDATABLE));
|
||||
_io.setNullInsertable(i, !col.isNotNull());
|
||||
_io.setNullUpdatable(i, !col.isNotNull());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* 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.nullity;
|
||||
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
/**
|
||||
* Persistent entity used to test behavior of null constraint on basic fields.
|
||||
*
|
||||
* @author Pinaki Poddar
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
public class NullValues {
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
@Column(nullable=true)
|
||||
private Integer nullable;
|
||||
|
||||
@Column(nullable=false)
|
||||
private Integer notNullable;
|
||||
|
||||
@Basic(optional=true)
|
||||
private Integer optional;
|
||||
|
||||
@Basic(optional=false)
|
||||
private Integer notOptional;
|
||||
|
||||
/**
|
||||
* Construct with all fields set to non-null values.
|
||||
*/
|
||||
public NullValues() {
|
||||
setOptional(42);
|
||||
setNotOptional(42);
|
||||
setNotNullable(42);
|
||||
setNullable(42);
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Integer getNullable() {
|
||||
return nullable;
|
||||
}
|
||||
|
||||
public void setNullable(Integer nullable) {
|
||||
this.nullable = nullable;
|
||||
}
|
||||
|
||||
public Integer getNotNullable() {
|
||||
return notNullable;
|
||||
}
|
||||
|
||||
public void setNotNullable(Integer notNullable) {
|
||||
this.notNullable = notNullable;
|
||||
}
|
||||
|
||||
public Integer getOptional() {
|
||||
return optional;
|
||||
}
|
||||
|
||||
public void setOptional(Integer optional) {
|
||||
this.optional = optional;
|
||||
}
|
||||
|
||||
public Integer getNotOptional() {
|
||||
return notOptional;
|
||||
}
|
||||
|
||||
public void setNotOptional(Integer notOptional) {
|
||||
this.notOptional = notOptional;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* 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.nullity;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.RollbackException;
|
||||
|
||||
import org.apache.openjpa.persistence.InvalidStateException;
|
||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||
|
||||
|
||||
/**
|
||||
* Test @Basic(optional=true|false) and @Column(nullable=true|false)
|
||||
* specification is honored.
|
||||
* Note: null constraint violation manifests as different exception types
|
||||
* for option and nullable condition.
|
||||
*
|
||||
* @author Pinaki Poddar
|
||||
*/
|
||||
public class TestBasicFieldNullity extends SingleEMFTestCase {
|
||||
|
||||
|
||||
public void setUp() {
|
||||
setUp(NullValues.class);
|
||||
}
|
||||
|
||||
public void testNullOnOptionalFieldIsAllowed() {
|
||||
NullValues pc = new NullValues();
|
||||
pc.setOptional(null);
|
||||
assertCommitSucceeds(pc);
|
||||
}
|
||||
|
||||
public void testNullOnNonOptionalFieldIsDisallowed() {
|
||||
NullValues pc = new NullValues();
|
||||
pc.setNotOptional(null);
|
||||
assertCommitFails(pc, InvalidStateException.class);
|
||||
}
|
||||
|
||||
public void testNotNullOnOptionalFieldIsAllowed() {
|
||||
NullValues pc = new NullValues();
|
||||
assertCommitSucceeds(pc);
|
||||
}
|
||||
|
||||
public void testNotNullOnNonOptionalFieldIsAllowed() {
|
||||
NullValues pc = new NullValues();
|
||||
assertCommitSucceeds(pc);
|
||||
}
|
||||
|
||||
public void testNullOnNullableColumnAllowed() {
|
||||
NullValues pc = new NullValues();
|
||||
pc.setNullable(null);
|
||||
assertCommitSucceeds(pc);
|
||||
}
|
||||
|
||||
public void testNullOnNonNullableColumnIsDisallowed() {
|
||||
NullValues pc = new NullValues();
|
||||
pc.setNotNullable(null);
|
||||
assertCommitFails(pc, RollbackException.class);
|
||||
}
|
||||
|
||||
public void testNotNullOnNullableColumnIsAllowed() {
|
||||
NullValues pc = new NullValues();
|
||||
assertCommitSucceeds(pc);
|
||||
}
|
||||
|
||||
public void testNotNullOnNonNullableColumnIsAllowed() {
|
||||
NullValues pc = new NullValues();
|
||||
assertCommitSucceeds(pc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the given instance can not be committed.
|
||||
*/
|
||||
void assertCommitFails(Object pc, Class expected) {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.persist(pc);
|
||||
try {
|
||||
em.getTransaction().commit();
|
||||
fail();
|
||||
} catch (RuntimeException e) {
|
||||
if (!expected.isAssignableFrom(e.getClass())) {
|
||||
fail("Expected " + expected.getName());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assertCommitSucceeds(Object pc) {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.persist(pc);
|
||||
try {
|
||||
em.getTransaction().commit();
|
||||
} catch (RuntimeException e) {
|
||||
fail();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue