OPENJPA-1837: fix the MaxEmbeddedLobFieldStrategy when there is limitation of max lob size and when the field is in an embeddable which is contained in an element collection.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1024306 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2010-10-19 16:14:51 +00:00
parent e1a9859689
commit 75bbc90899
10 changed files with 96 additions and 20 deletions

View File

@ -842,8 +842,14 @@ public class MappingRepository extends MetaDataRepository {
} }
if (field.isSerialized()) { if (field.isSerialized()) {
if (_dict.maxEmbeddedBlobSize != -1) if (_dict.maxEmbeddedBlobSize != -1) {
handler = defaultHandler(field, adapting);
if (handler != null) {
if (installHandlers)
field.setHandler(handler);
}
return new MaxEmbeddedBlobFieldStrategy(); return new MaxEmbeddedBlobFieldStrategy();
}
} else { } else {
// check for mapped strategy // check for mapped strategy
Object strat = mappedStrategy(field, field.getType(), adapting); Object strat = mappedStrategy(field, field.getType(), adapting);
@ -859,18 +865,36 @@ public class MappingRepository extends MetaDataRepository {
// check for known field strategies // check for known field strategies
if (!field.isSerialized() && (field.getType() == byte[].class if (!field.isSerialized() && (field.getType() == byte[].class
|| field.getType() == Byte[].class)) { || field.getType() == Byte[].class)) {
if (_dict.maxEmbeddedBlobSize != -1) if (_dict.maxEmbeddedBlobSize != -1) {
handler = defaultHandler(field, adapting);
if (handler != null) {
if (installHandlers)
field.setHandler(handler);
}
return new MaxEmbeddedByteArrayFieldStrategy(); return new MaxEmbeddedByteArrayFieldStrategy();
}
} else if (!field.isSerialized() } else if (!field.isSerialized()
&& (field.getType() == char[].class && (field.getType() == char[].class
|| field.getType() == Character[].class)) { || field.getType() == Character[].class)) {
if (_dict.maxEmbeddedClobSize != -1 && isClob(field, false)) if (_dict.maxEmbeddedClobSize != -1 && isClob(field, false)) {
handler = defaultHandler(field, adapting);
if (handler != null) {
if (installHandlers)
field.setHandler(handler);
}
return new MaxEmbeddedCharArrayFieldStrategy(); return new MaxEmbeddedCharArrayFieldStrategy();
}
} else if (!field.isSerialized()) { } else if (!field.isSerialized()) {
FieldStrategy strat = defaultTypeStrategy(field, installHandlers, FieldStrategy strat = defaultTypeStrategy(field, installHandlers,
adapting); adapting);
if (strat != null) if (strat != null) {
handler = defaultHandler(field, adapting);
if (handler != null) {
if (installHandlers)
field.setHandler(handler);
}
return strat; return strat;
}
} }
// check for default handler // check for default handler

View File

@ -180,7 +180,7 @@ public abstract class EmbedValueHandler
if (ecols.length == 0) if (ecols.length == 0)
continue; continue;
cval = (em == null) ? null : em.fetch(i); cval = (em == null) ? null : getValue(embed, em, i);
cval = embed.toEmbeddedDataStoreValue(cval, store); cval = embed.toEmbeddedDataStoreValue(cval, store);
if (cols.length == 1) { if (cols.length == 1) {
// rvals is empty // rvals is empty
@ -196,6 +196,13 @@ public abstract class EmbedValueHandler
} }
return idx; return idx;
} }
private Object getValue(Embeddable embed, OpenJPAStateManager sm, int idx) {
if (embed instanceof MaxEmbeddedLobFieldStrategy) {
return ((MaxEmbeddedLobFieldStrategy)embed).getValue(sm);
}
return sm.fetch(idx);
}
/** /**
* Helper to convert a datastore value to its object equivalent. * Helper to convert a datastore value to its object equivalent.

View File

@ -60,11 +60,11 @@ public class HandlerFieldStrategy
private static final Localizer _loc = Localizer.forPackage private static final Localizer _loc = Localizer.forPackage
(HandlerFieldStrategy.class); (HandlerFieldStrategy.class);
private Column[] _cols = null; protected Column[] _cols = null;
private ColumnIO _io = null; protected ColumnIO _io = null;
private Object[] _args = null; protected Object[] _args = null;
private boolean _load = false; protected boolean _load = false;
private boolean _lob = false; protected boolean _lob = false;
public void map(boolean adapt) { public void map(boolean adapt) {
if (field.getHandler() == null) if (field.getHandler() == null)

View File

@ -105,4 +105,13 @@ public class MaxEmbeddedBlobFieldStrategy
_maxSize = dict.maxEmbeddedBlobSize; _maxSize = dict.maxEmbeddedBlobSize;
field.setUsesImplData(Boolean.TRUE); field.setUsesImplData(Boolean.TRUE);
} }
protected Object getValue(OpenJPAStateManager sm) {
byte[] b = (byte[]) sm.getImplData(field.getIndex());
if (b == null || (b.length > _maxSize && !field.getColumns()[0].isNotNull()))
return null;
sm.setImplData(field.getIndex(), null);
DBDictionary.SerializedData dat = new DBDictionary.SerializedData(b);
return dat;
}
} }

View File

@ -56,9 +56,8 @@ public class MaxEmbeddedByteArrayFieldStrategy
protected void update(OpenJPAStateManager sm, Row row) protected void update(OpenJPAStateManager sm, Row row)
throws SQLException { throws SQLException {
byte[] b = PrimitiveWrapperArrays.toByteArray(sm.fetchObject byte[] b = (byte[]) getValue(sm);
(field.getIndex())); if (b == null)
if (b == null || (b.length > _maxSize && !field.getColumns()[0].isNotNull()))
row.setBytes(field.getColumns()[0], null); row.setBytes(field.getColumns()[0], null);
else else
row.setBytes(field.getColumns()[0], b); row.setBytes(field.getColumns()[0], b);
@ -94,4 +93,12 @@ public class MaxEmbeddedByteArrayFieldStrategy
DBDictionary dict = field.getMappingRepository().getDBDictionary(); DBDictionary dict = field.getMappingRepository().getDBDictionary();
_maxSize = dict.maxEmbeddedBlobSize; _maxSize = dict.maxEmbeddedBlobSize;
} }
protected Object getValue(OpenJPAStateManager sm) {
byte[] b = PrimitiveWrapperArrays.toByteArray(sm.fetchObject
(field.getIndex()));
if (b == null || (b.length > _maxSize && !field.getColumns()[0].isNotNull()))
return null;
return b;
}
} }

View File

@ -111,4 +111,13 @@ public class MaxEmbeddedCharArrayFieldStrategy
DBDictionary dict = field.getMappingRepository().getDBDictionary(); DBDictionary dict = field.getMappingRepository().getDBDictionary();
_maxSize = dict.maxEmbeddedClobSize; _maxSize = dict.maxEmbeddedClobSize;
} }
protected Object getValue(OpenJPAStateManager sm) {
char[] c = PrimitiveWrapperArrays.
toCharArray(sm.fetchObject(field.getIndex()));
if (c == null || c.length > _maxSize)
return null;
else
return c;
}
} }

View File

@ -52,8 +52,8 @@ public class MaxEmbeddedClobFieldStrategy
protected void update(OpenJPAStateManager sm, Row row) protected void update(OpenJPAStateManager sm, Row row)
throws SQLException { throws SQLException {
String s = sm.fetchString(field.getIndex()); String s = (String) getValue(sm);
if (s == null || (s.length() > _maxSize && !field.getColumns()[0].isNotNull())) if (s == null)
row.setNull(field.getColumns()[0], true); row.setNull(field.getColumns()[0], true);
else else
row.setString(field.getColumns()[0], s); row.setString(field.getColumns()[0], s);
@ -81,4 +81,12 @@ public class MaxEmbeddedClobFieldStrategy
DBDictionary dict = field.getMappingRepository().getDBDictionary(); DBDictionary dict = field.getMappingRepository().getDBDictionary();
_maxSize = dict.maxEmbeddedClobSize; _maxSize = dict.maxEmbeddedClobSize;
} }
protected Object getValue(OpenJPAStateManager sm) {
String s = sm.fetchString(field.getIndex());
if (s == null || (s.length() > _maxSize && !field.getColumns()[0].isNotNull())) {
return null;
}
return s;
}
} }

View File

@ -46,7 +46,7 @@ import org.apache.openjpa.meta.JavaTypes;
* @since 0.4.0 * @since 0.4.0
*/ */
abstract class MaxEmbeddedLobFieldStrategy abstract class MaxEmbeddedLobFieldStrategy
extends AbstractFieldStrategy { extends HandlerFieldStrategy {
/** /**
* Return the expected type of the field from {@link JavaTypes} or * Return the expected type of the field from {@link JavaTypes} or
@ -95,11 +95,13 @@ abstract class MaxEmbeddedLobFieldStrategy
tmpCol.setIdentifier(fieldName); tmpCol.setIdentifier(fieldName);
tmpCol.setJavaType(getExpectedJavaType()); tmpCol.setJavaType(getExpectedJavaType());
tmpCol.setSize(-1); tmpCol.setSize(-1);
Column[] cols = vinfo.getColumns(field, fieldName, _cols = vinfo.getColumns(field, fieldName,
new Column[]{ tmpCol }, field.getTable(), adapt); new Column[]{ tmpCol }, field.getTable(), adapt);
_io = vinfo.getColumnIO();
field.setColumns(cols); if (_io == null)
field.setColumnIO(vinfo.getColumnIO()); _io = field.getColumnIO();
field.setColumns(_cols);
field.setColumnIO(_io);
field.mapConstraints(fieldName, adapt); field.mapConstraints(fieldName, adapt);
field.mapPrimaryKey(adapt); field.mapPrimaryKey(adapt);
} }
@ -266,4 +268,6 @@ abstract class MaxEmbeddedLobFieldStrategy
Object prevValue) Object prevValue)
throws SQLException { throws SQLException {
} }
protected abstract Object getValue(OpenJPAStateManager sm);
} }

View File

@ -86,4 +86,8 @@ public class EmbedOwner {
public Set<EmbedValue> getEmbedCollection() { public Set<EmbedValue> getEmbedCollection() {
return embedCollection; return embedCollection;
} }
public void setEmbedCollection(Set<EmbedValue> embedCollection) {
this.embedCollection = embedCollection;
}
} }

View File

@ -19,6 +19,7 @@
package org.apache.openjpa.persistence.jdbc.annotations; package org.apache.openjpa.persistence.jdbc.annotations;
import java.sql.Types; import java.sql.Types;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
@ -70,6 +71,9 @@ public class TestEJBEmbedded extends SingleEMFTestCase {
embed.setBlob("foobar".getBytes()); embed.setBlob("foobar".getBytes());
embed.setOwner(owner); embed.setOwner(owner);
owner.setEmbed(embed); owner.setEmbed(embed);
Set<EmbedValue> embedVals = new HashSet<EmbedValue>();
embedVals.add(embed);
owner.setEmbedCollection(embedVals);
em.persist(owner); em.persist(owner);
int pk = owner.getPk(); int pk = owner.getPk();
em.getTransaction().commit(); em.getTransaction().commit();