1 22 package org.jboss.ejb.plugins.cmp.jdbc2; 23 24 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2; 25 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCCMPFieldBridge2; 26 import org.jboss.ejb.plugins.cmp.jdbc2.schema.Schema; 27 import org.jboss.ejb.plugins.cmp.jdbc.QueryParameter; 28 import org.jboss.ejb.plugins.cmp.jdbc.JDBCEntityPersistenceStore; 29 import org.jboss.ejb.plugins.cmp.jdbc.JDBCTypeFactory; 30 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCTypeMappingMetaData; 31 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCFunctionMappingMetaData; 32 import org.jboss.ejb.GenericEntityObjectFactory; 33 import org.jboss.logging.Logger; 34 import org.jboss.deployment.DeploymentException; 35 36 import javax.ejb.FinderException ; 37 import javax.ejb.ObjectNotFoundException ; 38 39 40 44 public class FindByPrimaryKeyCommand 45 extends AbstractQueryCommand 46 { 47 public FindByPrimaryKeyCommand(JDBCEntityBridge2 entity) throws DeploymentException 48 { 49 this.entity = entity; 50 51 JDBCCMPFieldBridge2[] fields = (JDBCCMPFieldBridge2[]) entity.getTableFields(); 52 String selectColumns = fields[0].getColumnName(); 53 for(int i = 1; i < fields.length; ++i) 54 { 55 selectColumns += ", " + fields[i].getColumnName(); 56 } 57 58 JDBCCMPFieldBridge2[] pkFields = (JDBCCMPFieldBridge2[]) entity.getPrimaryKeyFields(); 59 String whereColumns = pkFields[0].getColumnName() + "=?"; 60 for(int i = 1; i < pkFields.length; ++i) 61 { 62 whereColumns += " and " + pkFields[i].getColumnName() + "=?"; 63 } 64 65 if(entity.getMetaData().hasRowLocking()) 66 { 67 JDBCEntityPersistenceStore manager = entity.getManager(); 68 JDBCTypeFactory typeFactory = manager.getJDBCTypeFactory(); 69 JDBCTypeMappingMetaData typeMapping = typeFactory.getTypeMapping(); 70 JDBCFunctionMappingMetaData rowLockingTemplate = typeMapping.getRowLockingTemplate(); 71 72 if(rowLockingTemplate == null) 73 { 74 throw new DeploymentException("Row locking template is not defined for mapping: " + typeMapping.getName()); 75 } 76 77 sql = rowLockingTemplate.getFunctionSql( 78 new Object []{selectColumns, entity.getQualifiedTableName(), whereColumns, null}, new StringBuffer () 79 ).toString(); 80 } 81 else 82 { 83 sql = "select "; 84 sql += selectColumns; 85 sql += " from " + entity.getQualifiedTableName() + " where "; 86 sql += whereColumns; 87 } 88 89 log = Logger.getLogger(getClass().getName() + "." + entity.getEntityName() + "#findByPrimaryKey"); 90 91 log.debug("sql: " + sql); 92 93 setParameters(QueryParameter.createPrimaryKeyParameters(0, entity)); 94 setEntityReader(entity, false); 95 } 96 97 public Object fetchOne(Schema schema, GenericEntityObjectFactory factory, Object [] args) throws FinderException 98 { 99 Object pk = args[0]; 100 if(pk == null) 101 { 102 throw new IllegalArgumentException ("Null argument for findByPrimaryKey"); 103 } 104 105 Object instance; 106 boolean cached = entity.getTable().hasRow(pk); 107 if(!cached) 108 { 109 instance = super.executeFetchOne(args, factory); 110 if(instance == null) 111 { 112 throw new ObjectNotFoundException ("Instance not find: entity=" + entity.getEntityName() + ", pk=" + pk); 113 } 114 } 115 else 116 { 117 instance = factory.getEntityEJBObject(pk); 118 } 119 120 return instance; 121 } 122 } 123 | Popular Tags |