Esempio n. 1
0
  /**
   * Performs inserts to the table for the managed instance or joins.
   *
   * @param connection the connection to use
   * @param managedInstance the managed instance to perform insert for
   * @throws SQLException thrown in case of underlying SQLException
   * @since $version
   * @author hceylan
   */
  public void performInsert(ConnectionImpl connection, final ManagedInstance<?> managedInstance)
      throws SQLException {
    final EntityTypeImpl<?> entityType = managedInstance.getType();
    final Object instance = managedInstance.getInstance();

    // Do not inline, generation of the insert SQL will initialize the insertColumns!
    final String insertSql = this.getInsertSql(entityType);
    final AbstractColumn[] insertColumns = this.getInsertColumns(entityType);

    // prepare the parameters
    final Object[] params = new Object[insertColumns.length];
    for (int i = 0; i < insertColumns.length; i++) {
      final AbstractColumn column = insertColumns[i];
      if (column instanceof DiscriminatorColumn) {
        params[i] = managedInstance.getType().getDiscriminatorValue();
      } else {
        params[i] = column.getValue(instance);
      }
    }

    // execute the insert
    final QueryRunner runner = new QueryRunner(this.jdbcAdaptor.isPmdBroken());
    runner.update(connection, insertSql, params);

    // if there is an identity column, extract the identity and set it back to the instance
    if (this.identityColumn != null) {
      final String selectLastIdSql = this.jdbcAdaptor.getSelectLastIdentitySql(this.identityColumn);
      final Number id = runner.query(connection, selectLastIdSql, new SingleValueHandler<Number>());

      this.identityColumn.setValue(managedInstance.getInstance(), id);
    }
  }
Esempio n. 2
0
  /**
   * Performs update to the table for the managed instance or joins. In addition checks if the table
   * participate in update.
   *
   * @param connection the connection to use
   * @param managedInstance the managed instance to perform update for
   * @return returns true if the table is updatable
   * @throws SQLException thrown in case of underlying SQLException
   * @since $version
   * @author hceylan
   */
  public boolean performUpdateWithUpdatability(
      ConnectionImpl connection, final ManagedInstance<?> managedInstance) throws SQLException {
    final EntityTypeImpl<?> entityType = managedInstance.getType();
    final Object instance = managedInstance.getInstance();

    // Do not inline, generation of the update SQL will initialize the insertColumns!
    final String updateSql = this.getUpdateSql(entityType, this.pkColumns);
    final AbstractColumn[] updateColumns = this.getUpdateColumns(entityType);

    // prepare the parameters
    final Object[] params = new Object[updateColumns.length];
    for (int i = 0; i < updateColumns.length; i++) {
      final AbstractColumn column = updateColumns[i];

      // if the first column is primary key then the table is not updatable
      if ((i == 0) && column.isPrimaryKey()) {
        return false;
      }

      params[i] = column.getValue(instance);
    }

    // execute the insert
    final QueryRunner runner = new QueryRunner(this.jdbcAdaptor.isPmdBroken());
    runner.update(connection, updateSql, params);

    return true;
  }
Esempio n. 3
0
  /**
   * Performs update to the table for the managed instance or joins.
   *
   * @param connection the connection to use
   * @param managedInstance the managed instance to perform update for
   * @throws SQLException thrown in case of underlying SQLException
   * @since $version
   * @author hceylan
   */
  public void performUpdate(ConnectionImpl connection, final ManagedInstance<?> managedInstance)
      throws SQLException {
    final EntityTypeImpl<?> entityType = managedInstance.getType();
    final Object instance = managedInstance.getInstance();

    // Do not inline, generation of the update SQL will initialize the insertColumns!
    final String updateSql = this.getUpdateSql(entityType, this.pkColumns);
    final AbstractColumn[] updateColumns = this.getUpdateColumns(entityType);

    // prepare the parameters
    final Object[] params = new Object[updateColumns.length];
    for (int i = 0; i < updateColumns.length; i++) {
      final AbstractColumn column = updateColumns[i];
      params[i] = column.getValue(instance);
    }

    // execute the insert
    final QueryRunner runner = new QueryRunner(this.jdbcAdaptor.isPmdBroken());
    runner.update(connection, updateSql, params);
  }
Esempio n. 4
0
  /**
   * Processes the associations.
   *
   * @since 2.0.0
   */
  public void processJoinedMappings() {
    ManagedInstance.LOG.debug("Post processing associations for instance {0}", this);

    final HashSet<String> _joinsLoaded = this.joinsLoaded;

    for (final PluralMappingEx<?, ?, ?> mapping : this.type.getMappingsPlural()) {
      final HashSet<String> joinsLoaded2 = _joinsLoaded;
      if (!joinsLoaded2.contains(mapping.getPath())) {
        if (mapping.isEager()) {
          mapping.load(this);
        } else {
          mapping.setLazy(this);
        }
      }
    }

    final X _instance = this.instance;
    final EntityManagerImpl entityManager = this.session.getEntityManager();

    for (final SingularAssociationMappingImpl<?, ?> mapping : this.type.getAssociationsSingular()) {
      if (mapping.isEager()) {
        if (!_joinsLoaded.contains(mapping.getPath())) {
          mapping.initialize(this);
        } else {
          final Object associate = mapping.get(_instance);
          if (associate instanceof EnhancedInstance) {
            final EnhancedInstance enhancedInstance = (EnhancedInstance) associate;
            if (!enhancedInstance.__enhanced__$$__isInitialized()) {
              final ManagedInstance<?> associateManagedInstance =
                  enhancedInstance.__enhanced__$$__getManagedInstance();
              entityManager.find(
                  associateManagedInstance.getType().getJavaType(),
                  associateManagedInstance.getId().getId());
            }
          }
        }
      }
    }
  }