Java tutorial
/** * Copyright (C) 2014 BigLoupe http://bigloupe.github.io/SoS-JobScheduler/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ /********************************************************* begin of preamble ** ** Copyright (C) 2003-2012 Software- und Organisations-Service GmbH. ** All rights reserved. ** ** This file may be used under the terms of either the ** ** GNU General Public License version 2.0 (GPL) ** ** as published by the Free Software Foundation ** http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file ** LICENSE.GPL included in the packaging of this file. ** ** or the ** ** Agreement for Purchase and Licensing ** ** as offered by Software- und Organisations-Service GmbH ** in the respective terms of supply that ship with this file. ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS ** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, ** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS ** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ** POSSIBILITY OF SUCH DAMAGE. ********************************************************** end of preamble*/ package com.sos.hibernate.classes; import java.io.File; import java.sql.Connection; import java.sql.SQLException; //import javax.print.attribute.standard.Finishings; import org.hibernate.FlushMode; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.jdbc.Work; import com.sos.hibernate.interfaces.IHibernateOptions; /** * \class SosHibernateSession * * \brief SosHibernateSession - * * \details * * \section SosHibernateSession.java_intro_sec Introduction * * \section SosHibernateSession.java_samples Some Samples * * \code .... code goes here ... \endcode * * <p style="text-align:center"> * <br /> * --------------------------------------------------------------------------- <br /> * APL/Software GmbH - Berlin <br /> * ##### generated by ClaviusXPress (http://www.sos-berlin.com) ######### <br /> * --------------------------------------------------------------------------- * </p> * \author Uwe Risse \version 02.02.2012 \see reference * * Created on 02.02.2012 16:08:04 */ public class SosHibernateSession { private static Session session = null; private static SessionFactory sessionFactory = null; public static File configurationFile = null; protected SosHibernateSession() { // no Instances } public static Session getInstance(final File configurationFile) { if (session == null) { ClassLoader classLoader = ClassLoader.getSystemClassLoader(); try { Configuration configuration = new Configuration(); try { Class dailyScheduleDBItem = classLoader .loadClass("com.sos.dailyschedule.db.DailyScheduleDBItem"); configuration.addAnnotatedClass(dailyScheduleDBItem); } catch (ClassNotFoundException e) { } try { Class schedulerTaskHistoryDBItem = classLoader .loadClass("com.sos.scheduler.history.db.SchedulerTaskHistoryDBItem"); configuration.addAnnotatedClass(schedulerTaskHistoryDBItem); } catch (ClassNotFoundException e) { } try { Class schedulerOrderStepHistoryDBItem = classLoader .loadClass("com.sos.scheduler.history.db.SchedulerOrderStepHistoryDBItem"); configuration.addAnnotatedClass(schedulerOrderStepHistoryDBItem); } catch (ClassNotFoundException e) { } try { Class schedulerOrderHistoryDBItem = classLoader .loadClass("com.sos.scheduler.history.db.SchedulerOrderHistoryDBItem"); configuration.addAnnotatedClass(schedulerOrderHistoryDBItem); } catch (ClassNotFoundException e) { } try { Class schedulerInstancesDBItem = classLoader .loadClass("com.sos.scheduler.db.SchedulerInstancesDBItem"); configuration.addAnnotatedClass(schedulerInstancesDBItem); } catch (ClassNotFoundException e) { } try { Class jadeFilesDBItem = classLoader.loadClass("sos.ftphistory.db.JadeFilesDBItem"); configuration.addAnnotatedClass(jadeFilesDBItem); } catch (ClassNotFoundException e) { } try { Class JadeFilesHistoryDBItem = classLoader .loadClass("sos.ftphistory.db.JadeFilesHistoryDBItem"); configuration.addAnnotatedClass(JadeFilesHistoryDBItem); } catch (ClassNotFoundException e) { } try { Class SchedulerEventDBItem = classLoader.loadClass("com.sos.eventing.db.SchedulerEventDBItem"); configuration.addAnnotatedClass(SchedulerEventDBItem); } catch (ClassNotFoundException e) { } try { Class jobNetPlanDBItem = classLoader.loadClass("com.sos.jobnet.db.JobNetPlanDBItem"); configuration.addAnnotatedClass(jobNetPlanDBItem); } catch (ClassNotFoundException e) { } try { Class jobNetNodeDBItem = classLoader.loadClass("com.sos.jobnet.db.JobNetNodeDBItem"); configuration.addAnnotatedClass(jobNetNodeDBItem); } catch (ClassNotFoundException e) { } try { Class jobNetEdgesDBItem = classLoader.loadClass("com.sos.jobnet.db.JobNetEdgesDBItem"); configuration.addAnnotatedClass(jobNetEdgesDBItem); } catch (ClassNotFoundException e) { } try { Class eventsDBItem = classLoader.loadClass("com.sos.jobnet.db.EventsDBItem"); configuration.addAnnotatedClass(eventsDBItem); } catch (ClassNotFoundException e) { } try { Class jobNetHistoryDBItem = classLoader.loadClass("com.sos.jobnet.db.JobNetHistoryDBItem"); configuration.addAnnotatedClass(jobNetHistoryDBItem); } catch (ClassNotFoundException e) { } try { Class jobNetCmdHistoryDBItem = classLoader .loadClass("com.sos.jobnet.db.JobNetCmdHistoryDBItem"); configuration.addAnnotatedClass(jobNetCmdHistoryDBItem); } catch (ClassNotFoundException e) { } try { Class jobNetDBItem = classLoader.loadClass("com.sos.jobnet.db.JobNetDBItem"); configuration.addAnnotatedClass(jobNetDBItem); } catch (ClassNotFoundException e) { } // Configuration configuration = new Configuration(); // Configuration configuration = new // Configuration().addAnnotatedClass(dailyScheduleDBItem).addAnnotatedClass(schedulerTaskHistoryDBItem).addAnnotatedClass(schedulerOrderStepHistoryDBItem) // .addAnnotatedClass(schedulerOrderHistoryDBItem).addAnnotatedClass(schedulerInstancesDBItem).addAnnotatedClass(jobNetPlanDBItem).addAnnotatedClass(jobNetNodeDBItem).addAnnotatedClass(jobNetEdgesDBItem); configuration.configure(configurationFile); /* * serviceRegistry = new * ServiceRegistryBuilder().applySettings(configuration * .getProperties()).buildServiceRegistry(); sessionFactory = * configuration.buildSessionFactory(serviceRegistry); * * configuration.setSessionFactoryObserver(new * SessionFactoryObserver() { * * @Override public void sessionFactoryCreated(SessionFactory * factory) { } * * @Override public void sessionFactoryClosed(SessionFactory * factory) { ServiceRegistryBuilder.destroy(serviceRegistry); } * }); */ sessionFactory = configuration.buildSessionFactory(); session = sessionFactory.openSession(); session.doWork(new Work() { public void execute(Connection connection) throws SQLException { connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); connection.setAutoCommit(false); } }); session.setFlushMode(FlushMode.ALWAYS); } catch (Exception e) { e.printStackTrace(); } } else { session.clear(); } return session; } public static Session getInstance(IHibernateOptions options) { if (session == null) { ClassLoader classLoader = ClassLoader.getSystemClassLoader(); try { Class dailyScheduleDBItem = classLoader.loadClass("com.sos.dailyschedule.db.DailyScheduleDBItem"); Class schedulerTaskHistoryDBItem = classLoader .loadClass("com.sos.scheduler.history.db.SchedulerTaskHistoryDBItem"); Class schedulerOrderStepHistoryDBItem = classLoader .loadClass("com.sos.scheduler.history.db.SchedulerOrderStepHistoryDBItem"); Class schedulerOrderHistoryDBItem = classLoader .loadClass("com.sos.scheduler.history.db.SchedulerOrderHistoryDBItem"); Class schedulerInstancesDBItem = classLoader .loadClass("com.sos.scheduler.db.SchedulerInstancesDBItem"); Class schedulerEventDBItem = classLoader.loadClass("com.sos.eventing.db.SchedulerEventDBItem"); Class jobNetPlanDBItem = classLoader.loadClass("com.sos.jobnet.db.JobNetPlanDBItem"); Class jobNetNodeDBItem = classLoader.loadClass("com.sos.jobnet.db.JobNetNodeDBItem"); Class jobNetEdgesDBItem = classLoader.loadClass("com.sos.jobnet.db.JobNetEdgesDBItem"); Class eventsDBItem = classLoader.loadClass("com.sos.Jobnet.db.EventsDBItem"); Class jobNetHistoryDBItem = classLoader.loadClass("com.sos.jobnet.db.JobNetHistoryDBItem"); Class jobNetCmdHistoryDBItem = classLoader.loadClass("com.sos.jobnet.db.JobNetCmdHistoryDBItem"); Class jobNetDBItem = classLoader.loadClass("com.sos.jobnet.db.JobNetDBItem"); // Configuration configuration = new Configuration(); Configuration configuration = new Configuration().addAnnotatedClass(dailyScheduleDBItem) .addAnnotatedClass(schedulerTaskHistoryDBItem) .addAnnotatedClass(schedulerOrderStepHistoryDBItem) .addAnnotatedClass(schedulerOrderHistoryDBItem).addAnnotatedClass(schedulerInstancesDBItem) .addAnnotatedClass(jobNetPlanDBItem).addAnnotatedClass(jobNetNodeDBItem) .addAnnotatedClass(jobNetEdgesDBItem).addAnnotatedClass(schedulerEventDBItem); configuration.setProperty("hibernate.connection.url", options.gethibernate_connection_url().Value()); configuration.setProperty("hibernate.connection.password", options.gethibernate_connection_password().Value()); configuration.setProperty("hibernate.connection.url", options.gethibernate_connection_url().Value()); configuration.setProperty("hibernate.connection.username", options.gethibernate_connection_username().Value()); configuration.setProperty("hibernate.dialect", options.gethibernate_dialect().Value()); configuration.setProperty("hibernate.show_sql", options.gethibernate_show_sql().Value()); configuration.setProperty("hibernate.connection.autocommit", options.gethibernate_connection_autocommit().Value()); configuration.setProperty("hibernate.format_sql", options.gethibernate_format_sql().Value()); sessionFactory = configuration.buildSessionFactory(); session = sessionFactory.openSession(); session.setFlushMode(FlushMode.ALWAYS); } catch (ClassNotFoundException e) { e.printStackTrace(); } } else { session.clear(); } return session; } public static void close() { sessionFactory.close(); session.close(); session = null; sessionFactory = null; } }