Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.oracle.hibernate;
- /*//import org.hibernate.cfg.Configuration;
- import org.hibernate.Session;
- //import org.hibernate.Transaction;
- import org.hibernate.SessionFactory;
- import org.hibernate.boot.MetadataSources;
- import org.hibernate.boot.registry.StandardServiceRegistry;
- import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
- public class OCIAuthManager {
- //protected SessionFactory sessionFactory;
- protected SessionFactory sessionFactory1;
- protected SessionFactory sessionFactory2;
- protected void setup() {
- final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
- .configure() // configures settings from hibernate.cfg.xml
- .build();
- final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
- .configure("hibernate.cfg.xml") // configures settings from hibernate.cfg.xml
- .build();
- try {
- sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
- } catch (Exception ex) {
- StandardServiceRegistryBuilder.destroy(registry);
- throw new RuntimeException(ex);
- }
- }
- protected void setupPdb1() {
- final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
- .configure("hibernate-1.cfg.xml") // configures settings from hibernate.cfg.xml
- .build();
- try {
- sessionFactory1 = new MetadataSources(registry).buildMetadata().buildSessionFactory();
- } catch (Exception ex) {
- StandardServiceRegistryBuilder.destroy(registry);
- throw new RuntimeException(ex);
- }
- }
- protected void setupPdb2() {
- final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
- .configure("hibernate-2.cfg.xml") // configures settings from hibernate.cfg.xml
- .build();
- try {
- sessionFactory2 = new MetadataSources(registry).buildMetadata().buildSessionFactory();
- } catch (Exception ex) {
- StandardServiceRegistryBuilder.destroy(registry);
- throw new RuntimeException(ex);
- }
- }
- protected void exit() {
- sessionFactory1.close();
- sessionFactory2.close();
- }
- protected void create() {
- Applications app = new Applications();
- book.setTitle("Effective Java");
- book.setAuthor("Joshua Bloch");
- book.setPrice(32.59f);
- //app.setApplication_id(1);
- app.setApplication_name("helloworld");
- app.setAttr((short)1);
- //app.setApplication_id_providers();
- Session session = sessionFactory.openSession();
- session.beginTransaction();
- session.save(book);
- session.getTransaction().commit();
- session.close();
- Session session1 = sessionFactory1.openSession();
- session1.beginTransaction();
- session1.save(app);
- session1.getTransaction().commit();
- session1.close();
- Session session2 = sessionFactory2.openSession();
- session2.beginTransaction();
- session2.save(app);
- session2.getTransaction().commit();
- session2.close();
- }
- protected void read() {
- Session session = sessionFactory.openSession();
- long bookId = 20;
- Book book = session.get(Book.class, bookId);
- System.out.println("Title: " + book.getTitle());
- System.out.println("Author: " + book.getAuthor());
- System.out.println("Price: " + book.getPrice());
- session.close();
- }
- protected void update() {
- Book book = new Book();
- book.setId(20);
- book.setTitle("Ultimate Java Programming");
- book.setAuthor("Nam Ha Minh");
- book.setPrice(19.99f);
- storeIntoPdb1(book);
- storeIntoPdb2(book);
- Session session = sessionFactory.openSession();
- session.beginTransaction();
- session.update(book);
- session.getTransaction().commit();
- session.close();
- }
- protected void delete() {
- Book book = new Book();
- book.setId(20);
- Session session = sessionFactory.openSession();
- session.beginTransaction();
- session.delete(book);
- session.getTransaction().commit();
- session.close();
- }
- public static void storeIntoPdb1(Book b1){
- SessionFactory sessionFactory;
- try{
- final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
- .configure("hibernate.cfg.xml") // configures settings from hibernate.cfg.xml
- .build();
- try {
- sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
- } catch (Exception ex) {
- StandardServiceRegistryBuilder.destroy(registry);
- throw new RuntimeException(ex);
- }
- Session session = sessionFactory.openSession();
- session.beginTransaction();
- session.update(b1);
- session.getTransaction().commit();
- session.close();
- }catch(Exception ex){
- System.out.println("Error: "+ex.getMessage());
- }
- }
- public static void storeIntoPdb2(Book b2){
- SessionFactory sessionFactory;
- try{
- final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
- .configure("hibernate-1.cfg.xml") // configures settings from hibernate.cfg.xml
- .build();
- try {
- sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
- } catch (Exception ex) {
- StandardServiceRegistryBuilder.destroy(registry);
- throw new RuntimeException(ex);
- }
- Session session = sessionFactory.openSession();
- session.beginTransaction();
- session.update(b2);
- session.getTransaction().commit();
- session.close();
- }catch(Exception ex){
- System.out.println("Error: "+ex.getMessage());
- }
- }
- public static void main(String[] args) {
- OCIAuthManager manager = new OCIAuthManager();
- //manager.setup();
- manager.setupPdb1();
- manager.setupPdb2();
- manager.create();
- //manager.read();
- //manager.delete();
- manager.exit();
- }
- }
- */
- import java.util.*;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.hibernate.SessionFactory;
- //import org.hibernate.cfg.Configuration;
- import org.hibernate.boot.MetadataSources;
- import org.hibernate.boot.registry.StandardServiceRegistry;
- import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
- public class OCIAuthManager {
- private static SessionFactory factory1;
- //private static SessionFactory factory2;
- /*protected void setupTenant1Pdb1()
- {
- try
- {
- factory1 = new Configuration().configure("hibernate-1.cfg.xml").buildSessionFactory();
- }
- catch (Throwable ex)
- {
- System.err.println("Failed to create sessionFactory object." + ex);
- throw new ExceptionInInitializerError(ex);
- }
- }*/
- protected void setupTenant1Pdb1()
- {
- final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
- .configure("hibernate-1.cfg.xml") // configures settings from hibernate.cfg.xml
- .build();
- try
- {
- factory1 = new MetadataSources(registry).buildMetadata().buildSessionFactory();
- }
- catch (Exception ex)
- {
- StandardServiceRegistryBuilder.destroy(registry);
- throw new RuntimeException(ex);
- }
- }
- /*protected void setupTenant2Pdb1()
- {
- final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
- .configure("hibernate-2.cfg.xml") // configures settings from hibernate.cfg.xml
- .build();
- try
- {
- factory2 = new MetadataSources(registry).buildMetadata().buildSessionFactory();
- }
- catch (Exception ex)
- {
- StandardServiceRegistryBuilder.destroy(registry);
- throw new RuntimeException(ex);
- }
- }*/
- public static void main(String[] args) {
- /*try {
- factory = new Configuration().configure().buildSessionFactory();
- } catch (Throwable ex) {
- System.err.println("Failed to create sessionFactory object." + ex);
- throw new ExceptionInInitializerError(ex);
- }*/
- OCIAuthManager OAM = new OCIAuthManager();
- //HashSet application_id_providers = new HashSet();
- Set<IdentityProviders> application_id_providers = new HashSet<IdentityProviders>();
- OAM.setupTenant1Pdb1();
- //OAM.setupTenant2Pdb1();
- application_id_providers.add(new IdentityProviders("MCA"));
- application_id_providers.add(new IdentityProviders("MBA"));
- application_id_providers.add(new IdentityProviders("PMP"));
- /* Add application record in the database */
- Integer appID1 = OAM.addApplication("helloworld1", (short)1, application_id_providers);
- /* Add another application record in the database */
- Integer appID2 = OAM.addApplication("helloworld2", (short)2, application_id_providers);
- /* List down all the applications */
- //OAM.listApplications();
- /* Update application name */
- OAM.updateApplication(appID1, "newApplicationName");
- /* Delete an employee from the database */
- OAM.deleteApplication(appID2);
- /* List down all the employees */
- //OAM.listApplications();
- }
- /* Method to add an application record in the database */
- public Integer addApplication(String application_name, short attr, Set<IdentityProviders> application_id_providers){
- Session session = factory1.openSession();
- Transaction tx = null;
- Integer applicationID = null;
- try {
- tx = session.beginTransaction();
- Applications application = new Applications(application_name, attr);
- application.setApplication_id_providers(application_id_providers);
- applicationID = (Integer) session.save(application);
- tx.commit();
- } catch (HibernateException e) {
- if (tx!=null) tx.rollback();
- e.printStackTrace();
- } finally {
- session.close();
- }
- return applicationID;
- }
- /* Method to list all the application detail */
- /*public void listApplications( ){
- Session session = factory1.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- //List applications = session.createQuery("FROM Applications").list();
- List<Applications> applications = new ArrayList<Applications>();
- //for (Iterator iterator1 = applications.iterator(); iterator1.hasNext();){
- for (Iterator<Applications> iterator1 = applications.iterator(); iterator1.hasNext();){
- Applications application = (Applications) iterator1.next();
- System.out.print("Application Name: " + application.getApplication_name());
- System.out.print("Application Attribute: " + application.getAttr());
- //Set application_id_providers = application.getApplication_id_providers();
- Set<ApplicationIdentityProviders> application_id_providers = application.getApplication_id_providers();
- //for (Iterator iterator2 = application_id_providers.iterator(); iterator2.hasNext();){
- for (Iterator<ApplicationIdentityProviders> iterator2 = application_id_providers.iterator(); iterator2.hasNext();){
- IdentityProviders issuerName = (IdentityProviders) iterator2.next();
- System.out.println("Identity Provider Issuer Name: " + issuerName.getIssuer());
- }
- }
- tx.commit();
- } catch (HibernateException e) {
- if (tx!=null) tx.rollback();
- e.printStackTrace();
- } finally {
- session.close();
- }
- }*/
- /* Method to update application name */
- public void updateApplication(Integer ApplicationID, String application_name ){
- Session session = factory1.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- Applications application = (Applications)session.get(Applications.class, ApplicationID);
- application.setApplication_name(application_name);
- session.update(application);
- tx.commit();
- } catch (HibernateException e) {
- if (tx!=null) tx.rollback();
- e.printStackTrace();
- } finally {
- session.close();
- }
- }
- /* Method to delete an application from the records */
- public void deleteApplication(Integer ApplicationID){
- Session session = factory1.openSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- Applications application = (Applications)session.get(Applications.class, ApplicationID);
- session.delete(application);
- tx.commit();
- } catch (HibernateException e) {
- if (tx!=null) tx.rollback();
- e.printStackTrace();
- } finally {
- session.close();
- }
- }
- }
Add Comment
Please, Sign In to add comment