A disgruntled former school caretaker is accused of injuring 20 children during a terrifying hammer rampage inside a primary school in China’s capital of Beijing.

Three of the children have serious but non-life-threatening injuries, the Xicheng district government said on its social media account, adding that the attack took place at 11.17am.

Earlier reports said the man was armed with a knife but the school and parents later said he was armed with a hammer, according to media reports.

A former worker told the South China Morning Post the attack was carried out by a former temporary caretaker who was angry that he had not been employed beyond his probation period.

Surnamed Jia, the 49-year-old assailant was a contractor hired by the school for daily maintenance work.

His contract was scheduled to terminate at the end of January and though the school was discussing another job posting for Jia, he used a hammer and attacked students to “vent his resentment”, Xicheng district mayor Wang Shaofeng said at a press conference late Tuesday.

Of the 20 children injured, four suffered skull fractures that required surgery, a hospital representative told reporters.

“We will do everything possible to treat the injured children, severely punish the criminal... and take more effective measures to ensure the safety of schools,” Wang said.

The suspect was apprehended at the scene and an investigation has been launched, the statement said. All of the injured children are receiving hospital treatment.

Police in Xicheng, a major district of Beijing, declined to comment when reached by phone.

The primary school, Beijing No. 1 Affiliated Elementary School of Xuanwu Normal School, was founded in 1908. According to the school’s website, it has 2537 students and 199 teaching personnel.

Many schools in China have beefed up security following violence against students and family members.

In 2010, nearly 20 children were killed in knife attacks outside school gates. Last April, nine people were killed outside a middle school in northwestern China, allegedly by a former student seeking revenge for having been bullied. In November, five people were killed when a car slammed into a crowd of children outside a northeastern Chinese kindergarten.