1 22 package org.jboss.ejb.plugins.cmp.jdbc2; 23 24 import org.jboss.ejb.EntityEnterpriseContext; 25 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2; 26 import org.jboss.deployment.DeploymentException; 27 28 import javax.ejb.CreateException ; 29 import javax.ejb.DuplicateKeyException ; 30 import java.lang.reflect.Method ; 31 import java.sql.SQLException ; 32 33 37 public class ApplicationPkCreateCommand 38 implements CreateCommand 39 { 40 private JDBCEntityBridge2 entityBridge; 41 42 public void init(JDBCStoreManager2 manager) throws DeploymentException 43 { 44 this.entityBridge = (JDBCEntityBridge2) manager.getEntityBridge(); 45 } 46 47 public Object execute(Method m, Object [] args, EntityEnterpriseContext ctx) throws CreateException 48 { 49 Object pk; 50 PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext(); 51 if(ctx.getId() == null) 52 { 53 pk = entityBridge.extractPrimaryKeyFromInstance(ctx); 54 55 if(pk == null) 56 { 57 throw new CreateException ("Primary key for created instance is null."); 58 } 59 60 pctx.setPk(pk); 61 } 62 else 63 { 64 try 66 { 67 pctx.flush(); 68 } 69 catch(SQLException e) 70 { 71 if("23000".equals(e.getSQLState())) 72 { 73 throw new DuplicateKeyException ("Unique key violation or invalid foreign key value: pk=" + ctx.getId()); 74 } 75 else 76 { 77 throw new CreateException ("Failed to create instance: pk=" + ctx.getId() + 78 ", state=" + e.getSQLState() + 79 ", msg=" + e.getMessage()); 80 } 81 } 82 pk = ctx.getId(); 83 } 84 return pk; 85 } 86 } 87 | Popular Tags |