Exemplo 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);
    }
  }
Exemplo 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;
  }
Exemplo n.º 3
0
  /** {@inheritDoc} */
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }

    if (obj == null) {
      return false;
    }

    final ManagedInstance<?> other = (ManagedInstance<?>) obj;

    return this.getId().equals(other.getId());
  }
Exemplo n.º 4
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);
  }
Exemplo n.º 5
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());
            }
          }
        }
      }
    }
  }
Exemplo n.º 6
0
  /**
   * Performs removes from the table for the managed instance or joins.
   *
   * @param connection the connection to use
   * @param managedInstance the managed instance to perform remove for
   * @throws SQLException thrown in case of underlying SQLException
   * @since $version
   * @author hceylan
   */
  public void performRemove(ConnectionImpl connection, final ManagedInstance<?> managedInstance)
      throws SQLException {
    final String removeSql = this.getRemoveSql();

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

    final QueryRunner runner = new QueryRunner(this.jdbcAdaptor.isPmdBroken());
    runner.update(connection, removeSql, params);
  }
Exemplo n.º 7
0
  /**
   * Selects the version from the table.
   *
   * @param connection the connection to use
   * @param managedInstance the managed instance to perform select for
   * @return the version
   * @throws SQLException thrown in case of underlying SQLException
   * @since $version
   * @author hceylan
   */
  public Object performSelectVersion(
      ConnectionImpl connection, final ManagedInstance<?> managedInstance) throws SQLException {
    final Object instance = managedInstance.getInstance();

    // Do not inline, generation of the update SQL will initialize the insertColumns!
    final String updateSql = this.getSelectVersionSql(this.pkColumns);
    final AbstractColumn[] selectVersionColumns = this.getSelectVersionColumns();

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

    // execute the insert
    final QueryRunner runner = new QueryRunner(this.jdbcAdaptor.isPmdBroken());

    return runner.query(connection, updateSql, new SingleValueHandler<Object>(), params);
  }