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:javaapplication6.DaO.java

public void addtoDB(Verify round) {
    // Get session factory and open a new session
    SessionFactory factory = Hibernate.getSessionFactory();
    // Begin transaction
    try {//from   w  w  w .j  a  v  a 2  s . co  m
        Session session = factory.openSession();
        // Begin transaction
        Transaction t = session.beginTransaction();
        // Persist city and commit changes
        session.persist(round);
        t.commit();
        // Close the session
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:jpa_bayan.PersonDAO.java

@Transactional
public void addPerson(Person p) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(p);
    // logger.info("Person saved successfully, Person Details="+p);
}

From source file:jpa_bayan.TestDAO.java

@Transactional
public void addTest(Test t) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(t);
    // logger.info("Test saved successfully, Test Details="+p);
}

From source file:kr.debop4j.data.mongodb.test.loading.LoadSelectedColumnsCollectionTest.java

License:Apache License

@Test
public void testLoadSelectedAssociationColumns() {
    Session session = openSession();
    final Transaction transaction = session.getTransaction();
    transaction.begin();//w w w  . ja v a2s.  c  om

    Module mongodb = new Module();
    mongodb.setName("MongoDB");
    session.persist(mongodb);

    Module infinispan = new Module();
    infinispan.setName("Infinispan");
    session.persist(infinispan);

    List<Module> modules = new ArrayList<Module>();
    modules.add(mongodb);
    modules.add(infinispan);

    Project hibernateOGM = new Project();
    hibernateOGM.setId("projectID");
    hibernateOGM.setName("HibernateOGM");
    hibernateOGM.setModules(modules);

    session.persist(hibernateOGM);
    transaction.commit();

    //        this.addExtraColumn();
    //        GridDialect gridDialect = this.getGridDialect();
    //        AssociationKeyMetadata metadata = new AssociationKeyMetadata("Project_Module", new String[] { "Project_id" });
    //        metadata.setRowKeyColumnNames(new String[] { "Project_id", "module_id" });
    //        AssociationKey associationKey = new AssociationKey(
    //                metadata,
    //                new Object[] { "projectID" }
    //        );
    //        associationKey.setAssociationKind(AssociationKind.ASSOCIATION);
    //        associationKey.setCollectionRole("modules");
    //        associationKey.setOwnerEntityKey(new EntityKey(new EntityKeyMetadata("Project", new String[] { "id" }), new String[] { "projectID" }));
    //
    //        AssociationContext associationContext = new AssociationContext(Arrays.asList(associationKey.getRowKeyColumnNames()));
    //        final Association association = gridDialect.getAssociation(associationKey, associationContext);
    //        final MongoDBAssociationSnapshot associationSnapshot = (MongoDBAssociationSnapshot) association.getSnapshot();
    //        final DBObject assocObject = associationSnapshot.getDBObject();
    //        this.checkLoading(assocObject);

    session.delete(mongodb);
    session.delete(infinispan);
    session.delete(hibernateOGM);
    session.close();
}

From source file:kr.debop4j.data.ogm.test.type.BuiltInTypeTest.java

License:Apache License

@Test
public void testTypesSupport() throws Exception {
    final Session session = openSession();

    Transaction transaction = session.beginTransaction();
    Bookmark b = new Bookmark();
    b.setId("42");
    b.setDescription("Hibernate Site");
    b.setUrl(new URL("http://www.hibernate.org/"));
    BigDecimal weight = new BigDecimal("21.77");
    b.setSiteWeight(weight);// w ww .j  a v a  2 s .  c  om
    BigInteger visitCount = new BigInteger("444");
    b.setVisitCount(visitCount);
    b.setIsFavorite(Boolean.TRUE);
    Byte displayMask = Byte.valueOf((byte) '8');
    b.setDisplayMask(displayMask);
    Date now = new Date(System.currentTimeMillis());
    b.setCreationDate(now);
    b.setDestructionDate(now);
    b.setUpdateDate(now);
    final Calendar iCal = Calendar.getInstance();
    iCal.setTimeInMillis(now.getTime());
    b.setCreationCalendar(iCal);
    b.setDestructionCalendar(iCal);
    byte[] blob = new byte[5];
    blob[0] = '1';
    blob[1] = '2';
    blob[2] = '3';
    blob[3] = '4';
    blob[4] = '5';
    b.setBlob(blob);
    UUID serialNumber = UUID.randomUUID();
    b.setSerialNumber(serialNumber);
    final Long userId = RANDOM.nextLong();
    log.info("User ID created: " + userId);
    b.setUserId(userId);
    final Integer stockCount = Integer.valueOf(RANDOM.nextInt());
    b.setStockCount(stockCount);

    session.persist(b);
    transaction.commit();

    session.clear();

    transaction = session.beginTransaction();
    b = (Bookmark) session.get(Bookmark.class, b.getId());
    assertEquals("http://www.hibernate.org/", b.getUrl().toString());
    assertEquals(weight, b.getSiteWeight());
    assertEquals(visitCount, b.getVisitCount());
    assertEquals(Boolean.TRUE, b.getIsFavorite());
    assertEquals(displayMask, b.getDisplayMask());
    assertEquals("serial number incorrect", serialNumber, b.getSerialNumber());
    assertEquals("user id incorrect", userId, b.getUserId());
    assertEquals("stock count incorrect", stockCount, b.getStockCount());

    assertEquals("Creation Date Incorrect", now, b.getCreationDate());

    assertEquals("Timezone Info Not Correct", iCal.getTimeZone(), b.getDestructionCalendar().getTimeZone());

    assertEquals("Date info String Not Correct iCal", iCal.getTime().toGMTString(),
            b.getDestructionCalendar().getTime().toGMTString());

    // This test can break in ehcache dialect.
    assertEquals("Timezone Info Not Correct", iCal.getTimeZone(), b.getDestructionCalendar().getTimeZone());

    assertEquals(
            "Date info Not Correct iCal: " + DATE_FORMAT.format(iCal.getTime()) + " dest millis: "
                    + b.getDestructionCalendar().getTimeInMillis() + " iCal millis: " + iCal.getTimeInMillis(),
            iCal.getTime(), b.getDestructionCalendar().getTime());

    assertEquals("Byte array incorrect length", blob.length, b.getBlob().length);
    assertEquals(blob[0], b.getBlob()[0]);
    assertEquals('1', b.getBlob()[0]);
    assertEquals('2', b.getBlob()[1]);
    assertEquals('3', b.getBlob()[2]);
    assertEquals('4', b.getBlob()[3]);
    assertEquals('5', b.getBlob()[4]);

    session.delete(b);
    transaction.commit();

    session.close();
}

From source file:kz.dao.ThemesDAOImpl.java

@Override
public Themes create(Themes theme) throws SQLException {
    Session session = sessionFactory.openSession();
    session.persist(theme);
    session.close();/* w  ww . ja v a2 s.com*/
    return theme;
}

From source file:lxk.hibernate.hibernateDao.HibernateBasicDao.java

License:Open Source License

public long persistData() {

    final HibernateUser user = new HibernateUser();
    user.setUserName("Brett Meyer");
    final Session s = openSession();
    s.getTransaction().begin();//from  w ww  .  j a v a2 s.co m
    s.persist(user);
    s.getTransaction().commit();
    s.close();

    return user.getId();
}

From source file:main.java.Contexto.ContextoPartido.java

@Override
public void insertar(Partido o) {
    Session ses = this.getFactory().openSession();
    Transaction tx = ses.beginTransaction();
    ses.persist(o);
    tx.commit();// w  ww  .  j  a  v  a2  s .  c o  m
    ses.close();
}

From source file:main.java.Contexto.ContextoUsuario.java

@Override
public void insertar(Empleado o) {
    Session ses = this.getFactory().openSession();
    Transaction tx = ses.beginTransaction();
    ses.persist(o);
    tx.commit();/*www . j  ava 2 s.  c  o m*/
    ses.close();
}

From source file:maintester.Tester.java

public static void main(String[] args) {
    Session session = new AnnotationConfiguration().configure().buildSessionFactory().openSession();

    Transaction t = session.beginTransaction();
    Employee e1 = new Employee();
    e1.setSerialNo("s101");
    e1.setEmployeeNme("karthik");
    Employee e2 = new Employee();
    e2.setSerialNo("s102");
    e2.setEmployeeNme("srikanth");

    session.persist(e1);
    session.persist(e2);//www  . java 2s . c  o  m

    t.commit();
}