0

休止状態の助けを借りてデータベースを作成して入力しようとしています。

私の休止状態の構成:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="connection.url">
        jdbc:h2:C:\Users\data\datastore
    </property>
    <property name="connection.username">admin</property>
    <property name="connection.password">admin</property>
    <property name="connection.driver_class">org.h2.Driver</property>
    <property name="dialect">org.hibernate.dialect.H2Dialect</property>
    <property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
    <property name="current_session_context_class">thread</property>
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping class="de.model.Player" />
    <mapping class="de.model.Team" />
    <mapping class="de.model.Goal" />
    <mapping class="de.model.Match" />
</session-factory>

私のデータアクセスオブジェクト:

public class DAO {

/**
 * 
 */
public void clean() {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.createQuery("Delete from Player").executeUpdate();
    session.createQuery("Delete from Team").executeUpdate();
    session.createQuery("Delete from Goal").executeUpdate();
    session.createQuery("Delete from Match").executeUpdate();
    session.flush();
    session.clear();
    transaction.commit();
}

/**
 * 
 * @param player
 */
public void insertPlayer(Player player) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.save(player);
    transaction.commit();
}

/**
 * 
 * @param team
 */
public void insertTeam(Team team) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.save(team);
    transaction.commit();
}

/**
 * 
 * @param goal
 */
public void insertGoal(Goal goal) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.save(goal);
    transaction.commit();
}

/**
 * 
 * @param match
 */
public void insertMatch(Match match) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.save(match);
    transaction.commit();
}

/**
 * 
 * @param player
 */
public void updatePlayer(Player player) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.update(player);
    transaction.commit();
}

/**
 * 
 * @param team
 */
public void updateTeam(Team team) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.update(team);
    transaction.commit();
}

/**
 * 
 * @param goal
 */
public void updateGoal(Goal goal) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.update(goal);
    transaction.commit();
}

/**
 * 
 * @param match
 */
public void updateMatch(Match match) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.update(match);
    transaction.commit();
}

/**
 * 
 * @param player
 */
public void deletePlayer(Player player) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.delete(player);
    transaction.commit();
}

/**
 * 
 * @param team
 */
public void deleteTeam(Team team) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.delete(team);
    transaction.commit();
}

/**
 * 
 * @param goal
 */
public void deleteGoal(Goal goal) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.delete(goal);
    transaction.commit();
}

/**
 * 
 * @param match
 */
public void deleteMatch(Match match) {
    Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();

    session.delete(match);
    transaction.commit();
}
}

私のメイン:

public class Main {
/**
 * 
 */
private static Logger log = Logger.getLogger(Main.class);
/**
 * 
 */
private static DAO dao = new DAO();
/**
 * 
 */
private static XMLReader reader = new XMLReader();

/**
 * 
 * @param args
 */
public static void main(String[] args) {
    try {
        Document allTeams = reader.parseFile("src/main/resources/teams.xml");
        Document allMatches = reader.parseFile("src/main/resources/matches.xml");

        List<Element> teams = allTeams.getRootElement().getChildren("Team");

        for(Element team: teams) {
            Team newTeam = new Team();
            newTeam.setId(Integer.parseInt(team.getChild("teamID").getValue()));
            newTeam.setName(team.getChild("teamName").getValue());
            newTeam.setIconUrl(team.getChild("teamIconURL").getValue());
            newTeam.setStadion(team.getChild("stadion").getValue());
            newTeam.setPlayer(new HashSet<Player>());

            List<Element> players = team.getChildren("player");

            for(Element player: players) {
                Player newPlayer = new Player();
                newPlayer.setName(player.getValue());
                newPlayer.setTeam(newTeam);

                newTeam.getPlayer().add(newPlayer);
            }

            dao.insertTeam(newTeam);

            for(Player player: newTeam.getPlayer()) {
                dao.insertPlayer(player);
            }
        }

        // dao.clean();
    } catch(RuntimeException e) {
        try {
            Session session = DBSessionHandler.getSessionFactoryInstance().getCurrentSession();

            if(session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        } catch(HibernateException e1) {
            log.error("Error rolling back transaction");
        }

        throw e;
    } catch (JDOMException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Main を実行すると、次のエラーが発生します。

Exception in thread "main" org.hibernate.cache.CacheException:     net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM.  Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.

この問題を解決するにはどうすればよいですか? チュートリアルの助けを借りて、この最初の休止状態の試行を行いました。そのチュートリアルではうまくいきました^^

4

1 に答える 1