Example usage for org.hibernate Session persist

List of usage examples for org.hibernate Session persist

Introduction

In this page you can find the example usage for org.hibernate Session persist.

Prototype

void persist(Object object);

Source Link

Document

Make a transient instance persistent.

Usage

From source file:business.AccountDB.java

public static String addNewAccount(Account a) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    String msg = "";

    try {//w w w .  j  a  v  a 2 s .  c om
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.persist(a);
        session.getTransaction().commit();
        session.flush();
        session.refresh(a);
        msg = "Account Created!";
    } catch (HibernateException e) {
        msg = "Error Creating Account: " + e.getMessage();
        session.getTransaction().rollback();
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }

    return msg;
}

From source file:business.ChampionDB.java

public static String addNewChampion(Champion c) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    String msg = "";

    try {//from  w  ww . j a va 2 s.c  o  m
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.persist(c);
        session.getTransaction().commit();
        session.flush();
        session.refresh(c);
        msg = "Champion Created!";
    } catch (HibernateException e) {
        msg = "Error Creating Champion: " + e.getMessage();
        if (session != null && session.isOpen()) {
            session.getTransaction().rollback();
        }
    } finally {
        session.close();
    }

    return msg;
}

From source file:business.MoveDB.java

public static String addNewMove(Move m) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    String msg = "";

    try {//from  w  w w .  j  a  va2  s .c o m
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.persist(m);
        session.getTransaction().commit();
        session.flush();
        session.refresh(m);
        msg = "Move Added!";
    } catch (HibernateException e) {
        msg = "Error Creating Move: " + e.getMessage();
        if (session != null && session.isOpen()) {
            session.getTransaction().rollback();
        }
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }

    return msg;
}

From source file:business.PostDB.java

public static String addNewPost(Post p) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = null;
    String msg = "";

    try {/*from w ww  .j  a  v  a  2 s .  c o  m*/
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.persist(p);
        session.getTransaction().commit();
        session.flush();
        session.refresh(p);
        msg = "Post Added!";
    } catch (HibernateException e) {
        msg = "Error Creating Post: " + e.getMessage();
        session.getTransaction().rollback();
    } finally {
        session.close();
    }

    return msg;
}

From source file:by.telecom.subscriberapp.DAO.GenericDaoImpl.java

public Long create(T newInstance) {
    Session session = null;
    try {//from   ww w  .  j  av  a2  s  .c o m
        session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.persist(newInstance);
        session.getTransaction().commit();
        return 1L;
    } catch (HibernateException e) {
        session.getTransaction().rollback();
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }
    return 0L;
}

From source file:ca.usask.gmcte.currimap.action.OrganizationManager.java

License:Open Source License

public boolean addCharacteristicToOrganization(int charId, int deptId) {
    boolean createSuccessful = false;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/* ww  w . j  av  a2  s .co m*/
    try {
        Organization o = (Organization) session.get(Organization.class, deptId);
        CharacteristicType cType = (CharacteristicType) session.get(CharacteristicType.class, charId);
        LinkOrganizationCharacteristicType link = new LinkOrganizationCharacteristicType();
        int max = 0;
        try {
            max = (Integer) session.createQuery(
                    "select max(displayIndex) from LinkOrganizationCharacteristicType l where l.organization.id = :orgId")
                    .setParameter("orgId", o.getId()).uniqueResult();
        } catch (Exception e) {
            logger.error("unable to determine max!", e);
        }

        link.setDisplayIndex(max + 1);
        link.setCharacteristicType(cType);
        link.setOrganization(o);
        //p.getLinkProgramCharacteristicTypes().add(link);
        session.persist(link);
        session.getTransaction().commit();
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
    }
    createSuccessful = true;
    return createSuccessful;
}

From source file:ca.usask.gmcte.currimap.action.ProgramManager.java

License:Open Source License

public boolean addCharacteristicToOrganization(int charId, int orgId) {
    boolean createSuccessful = false;
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/* w  ww .j  av a2  s. c  o m*/
    try {
        Organization d = (Organization) session.get(Organization.class, orgId);
        CharacteristicType cType = (CharacteristicType) session.get(CharacteristicType.class, charId);
        LinkOrganizationCharacteristicType link = new LinkOrganizationCharacteristicType();
        int max = 0;
        try {
            max = (Integer) session.createQuery(
                    "select max(displayIndex) from LinkOrganizationCharacteristicType l where l.organization.id = :orgId")
                    .setParameter("orgId", d.getId()).uniqueResult();
        } catch (Exception e) {

            logger.error("unable to determine max!", e);
        }

        link.setDisplayIndex(max + 1);
        link.setCharacteristicType(cType);
        link.setOrganization(d);
        //p.getLinkProgramCharacteristicTypes().add(link);
        session.persist(link);
        session.getTransaction().commit();
    } catch (Exception e) {
        HibernateUtil.logException(logger, e);
    }
    createSuccessful = true;
    return createSuccessful;
}

From source file:ch.flashcard.service.CardDeckService.java

License:Open Source License

private void createCardDecks() {
    Session session = HibernateUtil.getSessionFactory().openSession();

    Transaction transaction = session.beginTransaction();

    List<CardEntity> cards = new ArrayList<CardEntity>();
    cards.add(new CardEntity("When was the Confederation gegrndet?", "In 1291"));
    cards.add(new CardEntity("What is the capital of Switzerland?", "Bern"));
    cards.add(new CardEntity("How and when the 30-year-old war is ended?", "Peace of Westphalia in 1648"));
    cards.add(new CardEntity("When the Switzerland solves the Roman Empire? How is it called?",
            "1648, Confoederatio Helvetica"));
    session.persist(createCardDeck(cards, "switzerland.png", "Swiss history"));

    cards = new ArrayList<CardEntity>();

    cards.add(new CardEntity("What man played the lead role in Notting Hill?", "Hugh Grant"));
    cards.add(new CardEntity("Who was the last James Bond movie leading man?", "Daniel Craig"));
    session.persist(createCardDeck(cards, "hollywood.png", "Hollywood stars"));

    cards = new ArrayList<CardEntity>();

    cards.add(new CardEntity("good night", "buona notte"));
    cards.add(new CardEntity("thank you", "grazie"));
    cards.add(new CardEntity("You speak good Italian", "Lei parla italiano bene"));
    cards.add(new CardEntity("Nice to meet you!", "Piacere!"));
    cards.add(new CardEntity("please", "prego"));
    cards.add(new CardEntity("From Rimini we drive to Turin.", "Da Rimini partiamo by Torino."));
    session.persist(createCardDeck(cards, "italy.png", "Italian"));

    transaction.commit();//from   w  ww. jav  a2  s .c o  m
    session.close();
}

From source file:ch.flashcard.service.CardDeckServiceImpl.java

License:Open Source License

private List<CardDeckEntity> createCardDecks() {
    List<CardDeckEntity> cardDecks = new ArrayList<CardDeckEntity>();

    Session session = HibernateUtil.getSessionFactory().openSession();

    Transaction transaction = session.beginTransaction();

    List<CardEntity> cards = new ArrayList<CardEntity>();
    cards.add(new CardEntity("When was the Confederation gegrndet?", "In 1291"));
    cards.add(new CardEntity("What is the capital of Switzerland?", "Bern"));
    cards.add(new CardEntity("How and when the 30-year-old war is ended?", "Peace of Westphalia in 1648"));
    cards.add(new CardEntity("When the Switzerland solves the Roman Empire? How is it called?",
            "1648, Confoederatio Helvetica"));
    CardDeckEntity cardDeck = createCardDeck(cards, "switzerland.png", "Swiss history");
    session.persist(cardDeck);
    cardDecks.add(cardDeck);//from   ww w. j av  a 2 s  .  c  o  m

    cards = new ArrayList<CardEntity>();

    cards.add(new CardEntity("What man played the lead role in Notting Hill?", "Hugh Grant"));
    cards.add(new CardEntity("Who was the last James Bond movie leading man?", "Daniel Craig"));
    cardDeck = createCardDeck(cards, "hollywood.png", "Hollywood stars");
    session.persist(cardDeck);
    cardDecks.add(cardDeck);

    cards = new ArrayList<CardEntity>();

    cards.add(new CardEntity("good night", "buona notte"));
    cards.add(new CardEntity("thank you", "grazie"));
    cards.add(new CardEntity("You speak good Italian", "Lei parla italiano bene"));
    cards.add(new CardEntity("Nice to meet you!", "Piacere!"));
    cards.add(new CardEntity("please", "prego"));
    cards.add(new CardEntity("From Rimini we drive to Turin.", "Da Rimini partiamo by Torino."));
    cardDeck = createCardDeck(cards, "italy.png", "Italian");
    session.persist(cardDeck);
    cardDecks.add(cardDeck);

    transaction.commit();
    session.close();

    return cardDecks;
}

From source file:City.City.java

/**
 * @param args the command line arguments
 *//*from  w  w w .  j a va2  s .  c o  m*/
public static void main(String[] args) {
    // TODO code application logic here

    //creating configuration object  
    //Configuration cfg=new Configuration();  
    //cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  
    //creating seession factory object  
    //ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()). build();
    //SessionFactory factory=cfg.buildSessionFactory(serviceRegistry);
    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    //creating session object  

    String csvFile = "/Users/test/desktop/SC labs/geo.csv";
    String line = "";
    String cvsSplitBy = ",";

    try {
        BufferedReader br = new BufferedReader(new FileReader(csvFile));
        br.readLine();
        br.readLine();
        //int count=1;
        while ((line = br.readLine()) != null) {
            Session session = factory.openSession();
            //creating transaction object  

            // use comma as separator
            String[] country = line.split(cvsSplitBy);

            //System.out.println(line);
            Transaction t = session.beginTransaction();
            perCity c1 = new perCity();

            c1.setId(Integer.valueOf(country[0])); // setting the value of id
            c1.setLat(Double.valueOf(country[5])); // setting the value of Latitude
            c1.setLon(Double.valueOf(country[6])); // setting the value of Longitude
            c1.setCountry(country[1]); // Srtting the value of Country code
            session.persist(c1);//persisting the object  
            t.commit();//transaction is commited  

            session.close(); // Closing the session after 
        }

    } catch (IOException e) {
        e.printStackTrace();

    }

}