If using dry beans, soak overnight in plenty of cold water. In the morning, drain and rinse the beans. Add them to a large saucepan with plenty of water, bring to a simmer and cook for 30 minutes, stirring occasionally, or until tender. Drain and use immediately, or set aside for later.
Preheat oven to 200C (390F).
Heat olive oil in a frypan (skillet) over medium heat. Add onion and garlic, and cook until well coloured. Add finely chopped rosemary and cook for another 1-2 minutes, until fragrant.
Add remaining sauce ingredients and a good grind of black pepper. Bring to a simmer and cook for a further five minutes.
Mix beans and sauce, and pour into an ovenproof baking dish or casserole with a cover (or cover with aluminium foil if needed).
Bake, covered, for 45 minutes, then remove the cover for a further 15 minutes. The baked beans should be bubbly and well coloured around the edges, without too much excess sauce.
Serve on toast, with jacket or mashed potatoes, or as a barbecue side dish.