Coat the bottom of a large skillet with oil and place it over medium heat. When the oil is hot, add the onion and cook it, stirring occasionally, until it becomes soft and translucent, about 5 minutes.
Add the garlic and cook it with the onion for 1 minute, stirring constantly, until very fragrant.
Add the wine, thyme and rosemary. Raise the heat and bring the wine to a simmer. Lower the heat and allow it to simmer until the liquid is reduced by about half, about 5 minutes.
Stir in the beans, tomatoes, tomato paste, and red pepper flakes. Raise the heat and bring the mixture to a boil. Lower the heat and allow it to simmer for about 10 minutes, stirring occasionally, until it thickens slightly. You want the consistency to be about that of spaghetti sauce.
Remove the skillet from the heat. Remove the rosemary sprig, and season the mixture with salt and pepper to taste.
Preheat the oven to 400°.
Cut the polenta into 12 to 18 slices (Note 1).
Ladle about one third of the tomato-bean mixture into the bottom of a 1 ½ quart baking dish. Layer half of the polenta slices over the mixture. Ladle another third of the tomato bean mixture over the polenta. Arrange the remaining half of the polenta slices over the second layer of sauce, then top everything with the remaining third of the sauce.
Cover the dish and bake the casserole for about 20 minutes, until the sauce is bubbly.
Remove the casserole from the oven and allow it to sit for at least 5 minutes.
Slice and serve.