package com.github.aureliano.verbum_domini.dao.helper;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.github.aureliano.verbum_domini.bean.BibleBean;
import com.github.aureliano.verbum_domini.bean.BookBean;
import com.github.aureliano.verbum_domini.orm.PersistenceManager;

public final class BookDataHelper {

	private BookDataHelper() {
		super();
	}
	
	public static void createBooks() {
		String[] names = new String[] {
			"Genesis", "Exodus", "Leviticus", "Numeri", "Deuteronomii"
		};
		
		Session session = PersistenceManager.instance().openSession();
		int[] ids = new int[] { 1, 2 };
		int bookId = 0;
		
		for (int id : ids) {
			BibleBean bible = (BibleBean) session.load(BibleBean.class, id);
			
			Transaction transaction = session.beginTransaction();
			for (String name : names) {
				BookBean book = prepareBook(++bookId, name, bible);
				session.saveOrUpdate(book);
			}
			
			transaction.commit();
		}
	}
	
	private static BookBean prepareBook(Integer id, String name, BibleBean bible) {
		BookBean book = new BookBean();
		
		book.setId(id);
		book.setName(name);
		book.setBible(bible);
		
		return book;
	}
}
@@ -634,19 +634,75 @@
 Id = 0;%0A
+%09%09Transaction transaction = session.beginTransaction();%0A
 %09%09%0A
-
 %09%09for (i
@@ -792,65 +792,8 @@
 %09%09%09%0A
-%09%09%09Transaction transaction = session.beginTransaction();%0A
 %09%09%09f
@@ -914,20 +914,22 @@
 %0A%09%09%09%7D%0A%09%09
-%09
+%7D
 %0A%09%09
+%0A%09
 %09transac
@@ -943,20 +943,16 @@
 mmit();%0A
-%09%09%7D%0A
 %09%7D%0A%09%0A%09pr

Improve transaction control.