It quickly became clear that the data was of significant value to the Chinese government: American officials with security clearances — including some of the most senior members of the government — had to reveal foreign contacts, relationships including extramarital affairs, health histories and information about their children and other family members.

The breach was so severe that the C.I.A. had to cancel assignments for undercover officers planning to go to China; though the agency did not submit its employees’ information to the personnel office, those individuals were often undercover as State Department or other government officials.

Then it got worse. Hacks into Anthem’s database and Starwood hotels — later taken over by Marriott — appeared to be orchestrated by the same or related Chinese groups. The United States assessed that China was building a vast database of who worked with whom in national security jobs, where they traveled and what their health histories were, according to American officials.

Over time, China can use the data sets to improve its artificial intelligence capabilities to the point where it can predict which Americans will be primed for future grooming and recruitment, John C. Demers, the assistant attorney general for national security at the Justice Department, said in an interview.

The charges were only the second time that the Justice Department has indicted Chinese military officers on suspicions of hacking. In 2014, five Chinese military officers were indicted in data thefts from a labor union, critical infrastructure and companies including U.S. Steel.

The Justice Department rarely secures indictments against members of foreign militaries or intelligence services, in part to avoid retaliation against American troops and spies, but Mr. Barr said it has made exceptions for state-sponsored actors who hacked into American networks to steal intellectual property or interfere in United States elections.

In 2015, President Barack Obama and President Xi Jinping of China agreed to rein in economically motivated cyberattacks in order to cooperate with requests to investigate cybercrimes and to avoid targeting critical infrastructure in each other’s countries.