code: private void insertRecordInJoinTable(String schemaName, String joinTableName, String joinColumnName,
              String inverseJoinColumnName, Object parentId, Set<Object> childrenIds)
      {
          s = getStatelessSession();
          Transaction tx = onBegin();
          for (Object childId : childrenIds)
          {
              StringBuffer query = new StringBuffer();
  
              // write an update query
              Object[] existingRowIds = findIdsByColumn(schemaName, joinTableName, joinColumnName, inverseJoinColumnName,
                      (String) childId, null);
  
              boolean joinTableRecordsExists = false;
              if (existingRowIds != null && existingRowIds.length > 0)
              {
                  for (Object o : existingRowIds)
                  {
                      if (o.toString().equals(parentId.toString()))
                      {
                          joinTableRecordsExists = true;
                          break;
                      }
                  }
              }
  
              if (!joinTableRecordsExists)
              {
                  query.append("INSERT INTO ").append(getFromClause(schemaName, joinTableName)).append("(")
                          .append(joinColumnName).append(",").append(inverseJoinColumnName).append(")")
                          .append(" VALUES('").append(parentId).append("','").append(childId).append("')");
  
                  s.createSQLQuery(query.toString()).executeUpdate();
              }
          }
          onCommit(tx);
          // tx.commit();
      }
docs: Insert record in join table.
  
  @param schemaName
  the schema name
  @param joinTableName
  the join table name
  @param joinColumnName
  the join column name
  @param inverseJoinColumnName
  the inverse join column name
  @param parentId
  the parent id
  @param childrenIds
  the children ids