Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

Locked History Actions

Butterfly_Persistence/PrimaryKey

Primary Keyについて

Butterfly Persistenceで扱うことのできるプライマリキーは一つのフィールドでなくてはならない。複数のフィールドを複合した場合には対応していない。

どのフィールドがプライマリキーであるかは自動的にデータベースメタデータから判定される。したがって、特にマッピング上でプライマリキーを指定する必要はない。

プライマリキーの判定ロジックを変更するには、IDbPrimaryKeyDeterminerインターフェースを実装したオブジェクトを次のようにPersistenceManagerに指定する。

persistenceManager
 .getConfiguration()
 .getObjectMapper()
 .setDbPrimaryKeyDeterminer(new MyKeyDeterminer());

次はHSQLDB用のプライマリキー判定クラスである。もともとのリリース(5.2.0)にも含まれているが、うまく動作していない。

  private static class MyKeyDeterminer implements IDbPrimaryKeyDeterminer{
    ObjectMappingFactory factory = new ObjectMappingFactory();
    public IKey getPrimaryKeyMapping(String table, String databaseName, Connection connection)
    throws PersistenceException{
      ResultSet result = null;
      IKey mapping = this.factory.createKey();
      mapping.setTable(table);
      try {
        result = connection.getMetaData().getPrimaryKeys(null, databaseName, table.toUpperCase());            
        while(result.next()){
          mapping.addColumn(result.getString(4));
        }
      } catch (SQLException e) {
        throw new PersistenceException("Error determining primary key for table " + table, e);
      } finally {
        JdbcUtil.close(result);
      }
      return mapping;
    }
  }