Package org.jboss.as.cmp.jdbc2

Examples of org.jboss.as.cmp.jdbc2.PersistentContext


            throw new EJBException("Internal error setting instance field " + getFieldName(), e);
        }
    }

    public void setValueInternal(CmpEntityBeanContext ctx, Object value, boolean makeDirty) {
        PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();

        // todo this is weird
        if (cmpFieldIAmMappedTo != null && cmpFieldIAmMappedTo.isPrimaryKeyMember) {
            Object curValue = pctx.getFieldValue(rowIndex);
            if (value != null && !value.equals(curValue)) {
                throw new IllegalStateException(
                        "Attempt to modify a primary key field through a foreign key field mapped to it: "
                                +
                                entity.getEntityName()
                                + "."
                                + cmpFieldIAmMappedTo.getFieldName()
                                +
                                " -> "
                                + entity.getQualifiedTableName()
                                + "."
                                + cmpFieldIAmMappedTo.getColumnName() +
                                ", current value=" + curValue + ", new value=" + value
                );
            }

            makeDirty = false;
        } else {
            pctx.setFieldValue(rowIndex, value);
        }

        if (makeDirty) {
            pctx.setDirty();
        }
    }
View Full Code Here


    public String getFieldName() {
        return fieldName;
    }

    public Object getValue(CmpEntityBeanContext ctx) {
        PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
        return pctx.getFieldValue(rowIndex);
    }
View Full Code Here

        this.pkSql = "";
    }

    public Object execute(Method m, Object[] args, CmpEntityBeanContext ctx) throws CreateException {
        Object pk;
        PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
        if (ctx.getPrimaryKey() == null) {
            Connection con = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                if (log.isDebugEnabled()) {
                    log.debug("executing sql: " + pkSql);
                }

                con = entityBridge.getDataSource().getConnection();
                ps = con.prepareStatement(pkSql);
                rs = ps.executeQuery();

                if (!rs.next()) {
                    throw new CreateException("pk-sql " + pkSql + " returned no results!");
                }

                pk = pkField.loadArgumentResults(rs, 1);
                pctx.setFieldValue(pkField.getRowIndex(), pk);
                pk = entityBridge.extractPrimaryKeyFromInstance(ctx);
            } catch (SQLException e) {
                log.error("Failed to execute pk sql. error code: " + e.getErrorCode() + ", sql state: " + e.getSQLState(), e);
                throw new CreateException("Failed to execute pk sql: " + e.getMessage() +
                        ", error code: " + e.getErrorCode() + ", sql state: " + e.getSQLState());
            } finally {
                JDBCUtil.safeClose(rs);
                JDBCUtil.safeClose(ps);
                JDBCUtil.safeClose(con);
            }

            if (pk == null) {
                log.error("Primary key for created instance is null.");
                throw new CreateException("Primary key for created instance is null.");
            }

            pctx.setPk(pk);
        } else {
            // insert-after-ejb-post-create
            try {
                pctx.flush();
            } catch (SQLException e) {
                if ("23000".equals(e.getSQLState())) {
                    throw new DuplicateKeyException("Unique key violation or invalid foreign key value: pk=" + ctx.getPrimaryKey());
                } else {
                    throw new CreateException("Failed to create instance: pk=" +
View Full Code Here

            for (int i = 0; i < foreignKeyFields.length; ++i) {
                foreignKeyFields[i].setValueInternal(ctx, null, fkConstraint == null);
            }

            if (fkConstraint != null) {
                PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
                pctx.nullForeignKey(fkConstraint);
            }
        }
View Full Code Here

                Object fieldValue = relatedPKField.getPrimaryKeyValue(relatedId);
                foreignKeyFields[i].setValueInternal(ctx, fieldValue, markDirty);
            }

            if (fkConstraint != null) {
                PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
                if (relatedId == null) {
                    pctx.nullForeignKey(fkConstraint);
                } else {
                    pctx.nonNullForeignKey(fkConstraint);
                }
            }
        }
View Full Code Here

            relatedPKFields = null;
        }
    }

    private FieldState getFieldState(CmpEntityBeanContext ctx) {
        PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
        FieldState state = pctx.getCMRState(cmrIndex);
        if (state == null) {
            if (isSingleValued()) {
                state = new SingleValuedFieldState();
            } else {
                state = new CollectionValuedFieldState();
            }
            pctx.setCMRState(cmrIndex, state);
        }
        return state;
    }
View Full Code Here

        public Object getCachedValue() {
            return value == null ? NULL_VALUE : value;
        }

        public void cacheValue(CmpEntityBeanContext ctx) {
            PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
            pctx.cacheRelations(cmrIndex, this);
        }
View Full Code Here

            loaded = true;
        }

        private Object getLoadedValue(CmpEntityBeanContext ctx) {
            if (!loaded) {
                PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
                pctx.loadCachedRelations(cmrIndex, this);
                if (!loaded) {
                    loader.load(ctx, this);
                    loaded = true;
                    cacheValue(ctx);
                }
View Full Code Here

        public Object getCachedValue() {
            return value;
        }

        public void cacheValue(CmpEntityBeanContext ctx) {
            PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
            pctx.cacheRelations(cmrIndex, this);
        }
View Full Code Here

            }
            return value;
        }

        private void loadOnlyFromCache(CmpEntityBeanContext ctx) {
            PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
            if (pctx == null) {
                throw CmpMessages.MESSAGES.persistenceContextNotAvailable();
            }
            pctx.loadCachedRelations(cmrIndex, this);
        }
View Full Code Here

TOP

Related Classes of org.jboss.as.cmp.jdbc2.PersistentContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.