1

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

例如,假设您有 Movies 和 Genres 表,我们希望将流派分配给电影。

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;

}

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

电影_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
}

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

此外,它允许重复分配(例如两行 ["Star Wars | Sci-fi", "Star Wars | Sci-fi"])

我正在使用 S/4 1809 系统,componentsSAP_ABA 75D,SAP_BASIS 753。

有没有其他方法可以做到这一点?

4

0 回答 0