在Fiori的ABAP编程模型中如何建模多对多关系?

时间:2018-12-20 13:02:05

标签: abap cds

如何使用CDS / BOPF和关联来实现多对多关系?

例如,假设您有“电影和流派”表,我们要在其中为电影分配流派。

define table zmv_movies {
  key client   : abap.clnt not null;
  key id       : snwd_node_key not null;
  name         : abap.string(0);
  release_date : abap.dats;
  plot         : abap.string(0);

}

define table zmv_genres {
  key client : abap.clnt not null;
  key id     : snwd_node_key not null;
  name       : abap.string(0);

}

联结表是映射多对多(n:m)关系的一种好方法,所以我创建了:

define table zmv_moviesgenres {
  @AbapCatalog.foreignKey.screenCheck : false
  key movie_id           : snwd_node_key not null
    with foreign key [0..*,1] zmv_movies
      where client = syst.mandt
        and id = zmv_moviesgenres.movie_id;
  @AbapCatalog.foreignKey.screenCheck : false
  key genre_id           : snwd_node_key not null
    with foreign key [0..*,1] zmv_genres
      where client = syst.mandt
        and id = zmv_moviesgenres.genre_id;
}

如何创建可正确处理此事务的事务视图?

到目前为止我的尝试

我创建了一个有效的示例,但由于数据一致性而失败。

我将zmv_moviesgenres更改为具有自己的主键:

define table zmv_moviesgenres {
  key movie_genre_id : snwd_node_key not null;
  @AbapCatalog.foreignKey.screenCheck : false
  movie_id           : snwd_node_key not null
    with foreign key [0..*,1] zmv_movies
      where client = syst.mandt
        and id = zmv_moviesgenres.movie_id;
  @AbapCatalog.foreignKey.screenCheck : false
  genre_id           : snwd_node_key not null
    with foreign key [0..*,1] zmv_genres
      where client = syst.mandt
        and id = zmv_moviesgenres.genre_id;

}

然后我创建了一些基本视图,然后创建了事务视图:

MOVIES_TP:

@AbapCatalog.sqlViewName: 'ZMVIMOVIESTP'
//@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Movies transactional view'

@VDM.viewType: #TRANSACTIONAL
@ObjectModel: {
  modelCategory: #BUSINESS_OBJECT,
  createEnabled: true,
  updateEnabled: true,
  deleteEnabled: true,

  compositionRoot: true,
  transactionalProcessingEnabled: true,
  writeActivePersistence: 'ZMV_MOVIES',

  draftEnabled: true,
  writeDraftPersistence: 'ZMVIDMOVIESTP',

  semanticKey: 'id'
}

define view ZMVI_Movies_TP
  as select from ZMVI_Movies
  association [0..*] to ZMVI_Movies_Genres_TP as _movies_genres on $projection.id = _movies_genres.movie_id
{
      @ObjectModel.readOnly: true
      key id,

      @ObjectModel.association.type: [ #TO_COMPOSITION_CHILD ]
      _movies_genres,
      name,
      release_date,
      plot
}

MOVIES_GENRES_TP:

@AbapCatalog.sqlViewName: 'ZMVIMOVGENTP'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Movies Genres Transactional View'

@VDM.viewType: #TRANSACTIONAL

@ObjectModel: {
  createEnabled: true,
  updateEnabled: true,
  deleteEnabled: true,


  writeDraftPersistence: 'ZMVDMOVIESGENRES',
  writeActivePersistence: 'ZMV_MOVIESGENRES', 

  semanticKey: 'movie_genre_id',
  alternativeKey: [{id: 'movie_id'}]
} 

define view ZMVI_Movies_Genres_TP as select from ZMVI_Movies_Genres
    association [1..1] to ZMVI_Movies_TP as _movies on $projection.movie_id = _movies.id
    association [1..1] to ZMVI_GENRES_TP as _genres on $projection.genre_id = _genres.id
{

    key movie_genre_id,
    genre_id,
    movie_id,

    @ObjectModel.association.type: [ #TO_COMPOSITION_PARENT, #TO_COMPOSITION_ROOT ]
    _movies,
    _genres
}

GENRES_TP:

@AbapCatalog.sqlViewName: 'ZMVIGENRESTP'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Genres transactional view'
@VDM.viewType: #TRANSACTIONAL
@ObjectModel: {
  modelCategory: #BUSINESS_OBJECT, 
  createEnabled: true,
  updateEnabled: true,
  deleteEnabled: true,

  compositionRoot: true,
  transactionalProcessingEnabled: true,
  writeActivePersistence: 'ZMV_GENRES',

  semanticKey: 'id',

  alternativeKey: [{element: ['_movies_genres']}]

}

define view ZMVI_GENRES_TP as select from ZMVI_Genres
  association [0..*] to ZMVI_Movies_Genres_TP as _movies_genres on $projection.id = _movies_genres.genre_id {
  @ObjectModel.readOnly: true
  key id,

  _movies_genres,
  name
}

这有效。它将电影与流派联系起来。但是,如果我删除一个流派,它将无法验证电影是否正在使用该流派。我必须为此创建一个确定/验证来手动检查。

此外,它还允许重复分配(例如[“星球大战|科幻”,“星球大战|科幻”]的两行)

我正在使用S / 4 1809系统,组件为SAP_ABA 75D,SAP_BASIS 753。

还有其他方法吗?

0 个答案:

没有答案