"McDaniel, Joe R." <mcdanij..igr.org> wrote:
> In looking through the responses I did not see (I think) a simple
> solution -- you can use the mapper tool to add a visible alias to a
> primary key. I have done this when using "meaningful" primary keys (like
> ssn, barcode for an item, etc.). When you regenerate the Java, the
> primary key will then be visible for searching.
For what it's worth, I use the following methods in GenericEntity.
GenericEntity is a superclass of all of my DataObjects. No guarantee that
this is the best way, but it doesn't require any special knowledge of your
database tables.
-Mike
public Number primaryKey()
{
String primaryKeysString = null;
Map pkAttributes = this.getObjectId().getIdSnapshot();
if (1 != pkAttributes.size()) throw new
CayenneRuntimeException("multi-field primary key found.");
Iterator pkIterator = pkAttributes.keySet().iterator();
String primaryKeyName = (String) pkIterator.next();
Number primaryKeyValue = (Number)pkAttributes.get(primaryKeyName);
return primaryKeyValue;
}
/**
*..aram aDataContext
*..aram class1
*..aram invoiceNumber
*..eturn
*/
public static GenericEntity objectMatchingPrimaryKey(DataContext
aDataContext, Class aClass, Object aPrimaryKeyValue)
{
if (log.isTraceEnabled()) log.trace("in
GenericRecord.objectMatchingPrimaryKey(DataContext, Class, Object)");
EntityResolver anEntityResolver = aDataContext.getEntityResolver();
DbEntity aDbEntity = anEntityResolver.lookupDbEntity(aClass);
List pkDbAttributes = aDbEntity.getPrimaryKey();
if (0 == pkDbAttributes.size()) throw new
CayenneRuntimeException("no primary key found for class=" + aClass + ".");
if (1 != pkDbAttributes.size()) throw new
CayenneRuntimeException("multi-field primary key found for class=" + aClass
+ ".");
DbAttribute primaryKeyDbAttribute =
(DbAttribute)pkDbAttributes.get(0);
String primaryKeyName = primaryKeyDbAttribute.getName();
Expression qualifier = ExpressionFactory.matchDbExp(primaryKeyName,
aPrimaryKeyValue);
SelectQuery query = new SelectQuery(aClass, qualifier);
List aList = aDataContext.performQuery(query);
if (1 == aList.size()) return (GenericEntity)aList.get(0);
if (0 == aList.size()) return null;
throw new MoreThanOneException("Found " +
String.valueOf(aList.size()) + " results for primary key=" +
aPrimaryKeyValue);
}
This archive was generated by hypermail 2.0.0 : Fri Jul 16 2004 - 14:17:21 EDT