The story of Joachim, his wife Anne (or Anna), and the miraculous birth of their child Mary, the mother of Jesus, is told for the first time in the 2nd century apocryphal infancy-gospel the Gospel of James (also called Protoevangelium of James). Joachim is a rich and pious man, who regularly gave to the poor. However, at the temple, Joachim's sacrifice was rejected, as the couple's childlessness was interpreted as a sign of divine displeasure. Joachim consequently withdrew to the desert, where he fasted and did penance for 40 days. Angels then appeared to both Joachim and Anne to promise them a child.
Joachim later returned to Jerusalem and embraced Anne at the city gate, located in the Walls of Jerusalem. An ancient belief held that a child born of an elderly mother who had given up hope of having offspring was destined for great things. Parallels occur in the Old Testament in the case of Hannah, mother of Samuel, and in the New Testament in the case of the parents of St John the Baptist.