You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2004/11/26 20:32:31 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker/util/sequence SequenceManagerNativeImpl.java
arminw 2004/11/26 11:32:31
Modified: src/java/org/apache/ojb/broker/util/sequence Tag:
OJB_1_0_RELEASE SequenceManagerNativeImpl.java
Log:
move PK assignment when using database identity columns from PBImpl to JdbcAccessImpl,
remove 1:1 FK assignment in SequenceManagerNativeImpl and method call in PBImpl
Revision Changes Path
No revision
No revision
1.18.2.3 +33 -102 db-ojb/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerNativeImpl.java
Index: SequenceManagerNativeImpl.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/sequence/SequenceManagerNativeImpl.java,v
retrieving revision 1.18.2.2
retrieving revision 1.18.2.3
diff -u -r1.18.2.2 -r1.18.2.3
--- SequenceManagerNativeImpl.java 16 Sep 2004 11:10:52 -0000 1.18.2.2
+++ SequenceManagerNativeImpl.java 26 Nov 2004 19:32:31 -0000 1.18.2.3
@@ -18,19 +18,13 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.Iterator;
-import java.util.Vector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ojb.broker.PersistenceBroker;
-import org.apache.ojb.broker.PersistenceBrokerException;
import org.apache.ojb.broker.accesslayer.JdbcAccess;
-import org.apache.ojb.broker.core.ValueContainer;
-import org.apache.ojb.broker.core.proxy.ProxyHelper;
import org.apache.ojb.broker.metadata.ClassDescriptor;
import org.apache.ojb.broker.metadata.FieldDescriptor;
-import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
@@ -44,28 +38,28 @@
* <li>field representing the identity column need attribute <code>primarykey</code> set 'true'</li>
* <li>only possible to declare one identity field per class</li>
* </ul>
- * <p>
+ * <p/>
* <b>Note:</b>
* Make sure that the DB generated identity columns represent values > 0, because negative values
* intern used by this implementation and 0 could cause problems with primitive FK fields.
* </p>
- * <p>
+ * <p/>
* Implementation configuration properties:
* <table cellspacing="2" cellpadding="2" border="3" frame="box">
* <tr>
- * <td><strong>Property Key</strong></td>
- * <td><strong>Property Values</strong></td>
+ * <td><strong>Property Key</strong></td>
+ * <td><strong>Property Values</strong></td>
* </tr>
* <tr>
- * <td>no properties to set</td>
- * <td>
- *
- * </td>
+ * <td>no properties to set</td>
+ * <td>
+ * <p/>
+ * </td>
* </tr>
* </table>
* </p>
- *
- * <p>
+ * <p/>
+ * <p/>
* <b>Limitations:</b>
* <ul>
* <li>Native key generation is not 'extent aware'
@@ -112,7 +106,7 @@
public void afterStore(JdbcAccess dbAccess, ClassDescriptor cld, Object obj) throws SequenceManagerException
{
FieldDescriptor identityField = extractIdentityColumnField(cld);
- if (identityField != null)
+ if(identityField != null)
{
ifNotReadOnlyFail(identityField);
long newId = getLastInsert(cld, identityField);
@@ -127,112 +121,50 @@
* @param cld The class descriptor
* @return The class's identity column or <code>null</code> if it does not have one
*/
- private FieldDescriptor extractIdentityColumnField(ClassDescriptor cld)
- {
- FieldDescriptor[] pkFields = cld.getPkFields();
- for (int i = 0; i < pkFields.length; i++)
- {
- // to find the identity column we search for a autoincrement
- // read-only field
- if (pkFields[i].isAutoIncrement() && pkFields[i].isAccessReadOnly())
- {
- return pkFields[i];
- }
- }
- return null;
- }
-
- private void ifNotReadOnlyFail(FieldDescriptor field) throws SequenceManagerException
+ private FieldDescriptor extractIdentityColumnField(ClassDescriptor cld)
{
- // is field declared as read-only?
- if (!field.isAccessReadOnly())
+ FieldDescriptor[] pkFields = cld.getPkFields();
+ for(int i = 0; i < pkFields.length; i++)
{
- throw new SequenceManagerException(
- "Can't find Identity column: Identity columns/fields need to be declared as" +
- " 'autoincrement' with 'readonly' access in field-descriptor");
- }
- }
-
- public void setReferenceFKs(Object obj, ClassDescriptor cld) throws SequenceManagerException
- {
- FieldDescriptor field = extractIdentityColumnField(cld);
- if (field != null)
- {
- Vector objectReferenceDescriptors = cld.getObjectReferenceDescriptors();
- // get all members of obj that are references and assign FKs
- Iterator i = objectReferenceDescriptors.iterator();
- while (i.hasNext())
+ // to find the identity column we search for a autoincrement
+ // read-only field
+ if(pkFields[i].isAutoIncrement() && pkFields[i].isAccessReadOnly())
{
- ObjectReferenceDescriptor rds = (ObjectReferenceDescriptor) i.next();
- Object ref = rds.getPersistentField().get(obj);
- if (ref != null)
- {
- // make sure the ref object actually exists
- // assertFkAssignment(obj, cld, ref, rds);
- assertFkAssignment(obj, ref, rds);
- }
+ return pkFields[i];
}
}
+ return null;
}
- /**
- * same method as in PBImpl
- */
- private void assertFkAssignment(Object obj, Object ref, ObjectReferenceDescriptor rds)
+ private void ifNotReadOnlyFail(FieldDescriptor field) throws SequenceManagerException
{
- // BRJ: ensure real object
- Object realObj = ProxyHelper.getRealObject(obj);
- Class objClass = realObj.getClass();
- ClassDescriptor objCld = getBrokerForClass().getDescriptorRepository().getDescriptorFor(objClass);
- FieldDescriptor[] objFkFields = rds.getForeignKeyFieldDescriptors(objCld);
- ValueContainer[] refPkValues;
-
- if (objFkFields == null)
- {
- throw new PersistenceBrokerException("No ForeignKeyFields !");
- }
-
- // MBAIRD: we have 'disassociated' this object from the referenced object,
- // the object representing the ord is now null, so set the fk to null.
- if (ref == null)
- {
- refPkValues = new ValueContainer[objFkFields.length];
- }
- else
- {
- // BRJ: ref may be Proxy
- Class refClass = ProxyHelper.getRealClass(ref);
- ClassDescriptor refCld = getBrokerForClass().getDescriptorRepository().getDescriptorFor(refClass);
-
- refPkValues = getBrokerForClass().serviceBrokerHelper().getKeyValues(refCld, ref, false);
- }
-
- for (int i = 0; i < objFkFields.length; i++)
+ // is field declared as read-only?
+ if(!field.isAccessReadOnly())
{
- objFkFields[i].getPersistentField().set(realObj, refPkValues[i].getValue());
+ throw new SequenceManagerException("Can't find Identity column: Identity columns/fields need to be declared as" +
+ " 'autoincrement' with 'readonly' access in field-descriptor");
}
}
private long getLastInsert(ClassDescriptor cld, FieldDescriptor field) throws SequenceManagerException
{
- FieldDescriptor fd = field;
long newId = 0;
Statement stmt = null;
- if (fd != null)
+ if(field != null)
{ // an autoinc column exists
try
{
stmt = getBrokerForClass().serviceConnectionManager().getConnection().createStatement();
ResultSet rs = stmt.executeQuery(lastInsertSelect(cld.getFullTableName()));
- if (!rs.next())
+ if(!rs.next())
{
throw new SequenceManagerException("Could not find native identifier");
}
newId = rs.getLong(1);
rs.close();
- if (log.isDebugEnabled()) log.debug("After store - newid=" + newId);
+ if(log.isDebugEnabled()) log.debug("After store - newid=" + newId);
}
- catch (Exception e)
+ catch(Exception e)
{
throw new SequenceManagerException(e);
}
@@ -240,20 +172,19 @@
{
try
{
- if (stmt != null) stmt.close();
+ if(stmt != null) stmt.close();
}
- catch (SQLException e)
+ catch(SQLException e)
{
if(log.isDebugEnabled())
- log.debug("Threw SQLException while in getLastInsert and closing stmt" , e);
+ log.debug("Threw SQLException while in getLastInsert and closing stmt", e);
// ignore it
}
}
}
else
{
- throw new SequenceManagerException(
- "No autoincrement field declared, please check repository for " + cld);
+ throw new SequenceManagerException("No autoincrement field declared, please check repository for " + cld);
}
return newId;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org