Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="hibernate.dialect">
  8. org.hibernate.dialect.MySQLDialect
  9. </property>
  10.  
  11. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  12.  
  13. <!-- Connects to the localhost database. This is for development purposes only. !-->
  14. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/x_database</property>
  15. <!-- Should the system flush the database before running or only update the database? !-->
  16. <property name="hbm2ddl.auto">
  17. create
  18. </property>
  19.  
  20. <property name="hibernate.connection.username">
  21. root
  22. </property>
  23.  
  24. <property name="hibernate.connection.password">
  25. root
  26. </property>
  27.  
  28. <mapping class="org.hva.folivora.model.user.UserEntity"></mapping>
  29. <mapping class="org.hva.folivora.model.user.ParentEntity"></mapping>
  30. <mapping class="org.hva.folivora.model.user.StudentEntity"></mapping>
  31. <mapping class="org.hva.folivora.model.questionanswer.QuestionEntity"></mapping>
  32. <mapping class="org.hva.folivora.model.global.GroupEntity"></mapping>
  33. <mapping class="org.hva.folivora.model.global.ThemeEntity"></mapping>
  34.  
  35. </session-factory>
  36.  
  37. </hibernate-configuration>
  38.  
  39. public class Database {
  40. private static final SessionFactory sessionFactory = buildSessionFactory();
  41.  
  42. private static SessionFactory buildSessionFactory() {
  43. try {
  44. // Create the SessionFactory from hibernate.cfg.xml
  45. StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
  46. Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build();
  47. return metadata.getSessionFactoryBuilder().build();
  48. } catch (Throwable ex) {
  49. // Make sure you log the exception, as it might be swallowed
  50. System.err.println("Initial SessionFactory creation failed." + ex);
  51. throw new ExceptionInInitializerError(ex);
  52. }
  53. }
  54.  
  55. public static SessionFactory getSessionFactory() {
  56. return sessionFactory;
  57. }
  58.  
  59. public static Session getSession() {
  60.  
  61. return getSessionFactory().openSession();
  62. }
  63.  
  64. public static void main(final String[] args) throws Exception {
  65. final org.hibernate.Session session = getSession();
  66. try {
  67. System.out.println("querying all the managed entities...");
  68. final Map metadataMap = session.getSessionFactory().getAllClassMetadata();
  69. for (Object key : metadataMap.keySet()) {
  70. final ClassMetadata classMetadata = (ClassMetadata) metadataMap.get(key);
  71. final String entityName = classMetadata.getEntityName();
  72. final Query query = session.createQuery("from " + entityName);
  73. System.out.println("executing: " + query.getQueryString());
  74. for (Object o : query.list()) {
  75. System.out.println(" " + o);
  76. }
  77. }
  78. } finally {
  79. session.close();
  80. }
  81.  
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement