Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-configuration SYSTEM
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <!-- Connects to the localhost database. This is for development purposes only. !-->
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/x_database</property>
- <!-- Should the system flush the database before running or only update the database? !-->
- <property name="hbm2ddl.auto">
- create
- </property>
- <property name="hibernate.connection.username">
- root
- </property>
- <property name="hibernate.connection.password">
- root
- </property>
- <mapping class="org.hva.folivora.model.user.UserEntity"></mapping>
- <mapping class="org.hva.folivora.model.user.ParentEntity"></mapping>
- <mapping class="org.hva.folivora.model.user.StudentEntity"></mapping>
- <mapping class="org.hva.folivora.model.questionanswer.QuestionEntity"></mapping>
- <mapping class="org.hva.folivora.model.global.GroupEntity"></mapping>
- <mapping class="org.hva.folivora.model.global.ThemeEntity"></mapping>
- </session-factory>
- </hibernate-configuration>
- public class Database {
- private static final SessionFactory sessionFactory = buildSessionFactory();
- private static SessionFactory buildSessionFactory() {
- try {
- // Create the SessionFactory from hibernate.cfg.xml
- StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
- Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build();
- return metadata.getSessionFactoryBuilder().build();
- } catch (Throwable ex) {
- // Make sure you log the exception, as it might be swallowed
- System.err.println("Initial SessionFactory creation failed." + ex);
- throw new ExceptionInInitializerError(ex);
- }
- }
- public static SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- public static Session getSession() {
- return getSessionFactory().openSession();
- }
- public static void main(final String[] args) throws Exception {
- final org.hibernate.Session session = getSession();
- try {
- System.out.println("querying all the managed entities...");
- final Map metadataMap = session.getSessionFactory().getAllClassMetadata();
- for (Object key : metadataMap.keySet()) {
- final ClassMetadata classMetadata = (ClassMetadata) metadataMap.get(key);
- final String entityName = classMetadata.getEntityName();
- final Query query = session.createQuery("from " + entityName);
- System.out.println("executing: " + query.getQueryString());
- for (Object o : query.list()) {
- System.out.println(" " + o);
- }
- }
- } finally {
- session.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement