diff --git a/src/test/java/hibernateTest/models/Groups.java b/src/test/java/hibernateTest/models/Groups.java index 7d08be1..1a69688 100644 --- a/src/test/java/hibernateTest/models/Groups.java +++ b/src/test/java/hibernateTest/models/Groups.java @@ -1,17 +1,51 @@ package hibernateTest.models; -import com.example.cosmos_serversb.entities.Group; -import com.example.cosmos_serversb.entities.Request; +import hibernateTest.entities.Group; +import hibernateTest.entities.Request; import com.example.cosmos_serversb.models.DeleteResult; +import hibernateTest.entities.Event; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import javax.inject.Singleton; import java.util.HashMap; +import java.util.List; @Singleton public class Groups { private static Groups theInstance = null; + private static SessionFactory sessionFactory; + + public static void setUp() { + // A SessionFactory is set up once for an application! + final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() + .configure() // configures settings from hibernate.cfg.xml + .build(); + try { + sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); + } + catch (Exception e) { + // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory + // so destroy it manually. + StandardServiceRegistryBuilder.destroy( registry ); + } + } + + public static void tearDown() { + try { + if ( sessionFactory != null ) { + sessionFactory.close(); + } + } catch (Exception e) { + System.out.println("Exception!"); + } + } + private Groups(){ } @@ -30,20 +64,34 @@ public static Group createGroup(String name, String uId){ //先にGidとuriの発行を行う Group testGroup = new Group("123456789", "http://test.com", name, uId); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.save(testGroup); + //hibernateテスト用出力 + List groupResult = session.createQuery( "from Group" ).list(); + for ( hibernateTest.entities.Group group : (List) groupResult ) { + System.out.println( "Group (" + group.getgId() + ") : " + group.getUri() + ": " + group.getName()); + } + + session.getTransaction().commit(); + session.close(); return testGroup; } public static Group getGroupByGid(String gId){ Group editGroup; - /* - for (int i = 0; i < groups.size(); i++) { - editGroup = groups.get(i); + Session session = sessionFactory.openSession(); + session.beginTransaction(); + List groupResult = session.createQuery( "from Group" ).list(); + for ( hibernateTest.entities.Group group : (List) groupResult ) { + editGroup = group; if (editGroup.getgId().equals(gId)) { return editGroup; } } - */ + session.getTransaction().commit(); + session.close(); return null; } @@ -111,4 +159,6 @@ return testMap; } + + }