private TopologyHostGroupEntity toEntity(HostGroupInfo groupInfo, TopologyRequestEntity topologyRequestEntity) {
    TopologyHostGroupEntity entity = new TopologyHostGroupEntity();
    entity.setGroupAttributes(attributesAsString(groupInfo.getConfiguration().getAttributes()));
    entity.setGroupProperties(propertiesAsString(groupInfo.getConfiguration().getProperties()));
    entity.setName(groupInfo.getHostGroupName());
    entity.setTopologyRequestEntity(topologyRequestEntity);

    // host info
    Collection<TopologyHostInfoEntity> hostInfoEntities = new ArrayList<>();
    entity.setTopologyHostInfoEntities(hostInfoEntities);

    Collection<String> hosts = groupInfo.getHostNames();
    if (hosts.isEmpty()) {
      TopologyHostInfoEntity hostInfoEntity = new TopologyHostInfoEntity();
      hostInfoEntity.setTopologyHostGroupEntity(entity);
      hostInfoEntity.setHostCount(groupInfo.getRequestedHostCount());
      if (groupInfo.getPredicate() != null) {
        hostInfoEntity.setPredicate(groupInfo.getPredicateString());
      }
      hostInfoEntities.add(hostInfoEntity);
    } else {
      for (String hostName : hosts) {
        TopologyHostInfoEntity hostInfoEntity = new TopologyHostInfoEntity();
        hostInfoEntity.setTopologyHostGroupEntity(entity);
        if (groupInfo.getPredicate() != null) {
          hostInfoEntity.setPredicate(groupInfo.getPredicateString());
        }
        hostInfoEntity.setFqdn(hostName);
        hostInfoEntity.setRackInfo(groupInfo.getHostRackInfo().get(hostName));
        hostInfoEntity.setHostCount(0);
        hostInfoEntities.add(hostInfoEntity);
      }
    }
    return entity;
  }
Implementation which uses Ambari Database DAO and Entity objects for persistence
of topology related information.
ambari-server/src/main/java/org/apache/ambari/server/topology/PersistedStateImpl.java