Code example for SessionFactory

Methods: openSession

0
        loadTheSecurityContactAndAddAnAuditContact(sessionFactory, savedContactId);
        showAllSecurityContacts(sessionFactory);
    } 
 
    private static Serializable createASecurityContactWithAgentContact(SessionFactory sessionFactory) {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        AgentContact agentContact = new AgentContact();
        agentContact.setName("my agent contact");
        SecurityContact contactWithAgent = new SecurityContact();
        contactWithAgent.setName("security contact with only an agent contact set");
        contactWithAgent.setAgentContact(agentContact);
        agentContact.getSecurityContacts().add(contactWithAgent);
        session.save(agentContact);
        Serializable savedContactId = session.save(contactWithAgent);
        transaction.commit();
        session.close(); 
        System.out.println("Created: " + contactWithAgent);
        return savedContactId;
    }