@Override public void saveContentRelationShip(SSP ssp,Set<Long> relatedContentIdSet){
  List<Long> relatedContentIds=findRelatedContentFromSsp(ssp);
  Set<Long> newRelatedContentIdSet=new HashSet<>();
  for (  Long relatedContentId : relatedContentIdSet) {
    if (!relatedContentIds.contains(relatedContentId)) {
      newRelatedContentIdSet.add(relatedContentId);
    }
  }
  if (!newRelatedContentIdSet.isEmpty()) {
    StringBuilder queryValuesBuilder=new StringBuilder();
    for (    Long relatedContentId : newRelatedContentIdSet) {
      queryValuesBuilder.append("(");
      queryValuesBuilder.append(ssp.getId());
      queryValuesBuilder.append(",");
      queryValuesBuilder.append(relatedContentId);
      queryValuesBuilder.append(")");
      queryValuesBuilder.append(",");
    }
    queryValuesBuilder.setCharAt(queryValuesBuilder.length() - 1,';');
    Query query=entityManager.createNativeQuery(INSERT_QUERY + queryValuesBuilder.toString());
    try {
      query.executeUpdate();
      flushAndCloseEntityManager();
    }
 catch (    ConstraintViolationException micve) {
      LOGGER.warn(micve.getMessage());
    }
 finally {
      flushAndCloseEntityManager();
    }
  }
}

@ Override public void saveContentRelationShip ( SSP ssp , Set < Long > relatedContentIdSet ) { List < Long > relatedContentIds = findRelatedContentFromSsp ( ssp ) ; Set < Long > newRelatedContentIdSet = new HashSet < > ( ) ; for ( Long relatedContentId : relatedContentIdSet ) { if ( ! relatedContentIds . contains ( relatedContentId ) ) { newRelatedContentIdSet . add ( relatedContentId ) ; } } if ( ! newRelatedContentIdSet . isEmpty ( ) ) { StringBuilder queryValuesBuilder = new StringBuilder ( ) ; for ( Long relatedContentId : newRelatedContentIdSet ) { queryValuesBuilder . append ( "(" ) ; queryValuesBuilder . append ( ssp . getId ( ) ) ; queryValuesBuilder . append ( "," ) ; queryValuesBuilder . append ( relatedContentId ) ; queryValuesBuilder . append ( ")" ) ; queryValuesBuilder . append ( "," ) ; } queryValuesBuilder . setCharAt ( queryValuesBuilder . length ( ) - 1 , ';' ) ; Query query = entityManager . createNativeQuery ( INSERT_QUERY + queryValuesBuilder . toString ( ) ) ; try { query . executeUpdate ( ) ; flushAndCloseEntityManager ( ) ; } catch ( ConstraintViolationException micve ) { LOGGER . warn ( micve . getMessage ( ) ) ; } finally { flushAndCloseEntityManager ( ) ; } } }
this native query is used to avoid multiple select before insert realized by hibernate while persisting the content relationship relation .
