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:hu.ptemik.gallery.control.Controller.java

/**
 * Registers a new Picture record.// w  ww  . j  av  a 2  s . c  om
 *
 * @param pic The url of the picture.
 * @param user The uploader.
 * @return True if succesful, false if not.
 */
public static boolean newPicture(Picture pic, User user) {
    Session session = HibernateUtil.getSessionFactory().openSession();

    try {
        if (user != null && pic != null) {
            user.addPicture(pic);
            pic.setUser(user);
            session.beginTransaction();
            session.persist(pic);
            session.getTransaction().commit();
            session.close();
            return true;
        } else {
            return false;
        }
    } catch (Exception ex) {
        if (session.isOpen())
            session.close();
        System.out.println(ex.getMessage());
        return false;
    }
}

From source file:ibacz.dao.StudentDaoImpl.java

@Override
public void editStudent(Student student) {
    Session session = sessionFactory.getCurrentSession();
    session.persist(student);
}

From source file:info.rmapproject.auth.dao.ApiKeyDaoImpl.java

License:Apache License

@Override
public int addApiKey(ApiKey apiKey) throws RMapAuthException {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(apiKey);
    LOG.info("API key saved successfully, API key={}", apiKey);
    return apiKey.getApiKeyId();
}

From source file:info.rmapproject.auth.dao.UserDaoImpl.java

License:Apache License

@Override
public int addUser(User user) throws RMapAuthException {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(user);
    LOG.info("User record saved successfully, User Details={}", user);
    return user.getUserId();
}

From source file:info.rmapproject.auth.dao.UserIdProviderDaoImpl.java

License:Apache License

@Override
public int addUserIdProvider(UserIdentityProvider userIdProvider) throws RMapAuthException {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(userIdProvider);
    LOG.info("User ID Provider record saved successfully. Details={}", userIdProvider);
    return userIdProvider.getUserIdentityProviderId();
}

From source file:io.jeandavid.projects.vod.entities.DvdOrder.java

License:Open Source License

public void addDvd(Dvd dvd, int quantity, Session session) {
    DvdOrderDvd temp = new DvdOrderDvd();
    temp.setQuantity(quantity);/*from w w  w. ja v  a  2s . c  o m*/
    session.persist(temp);
    dvd.addDvdOrderDvd(temp);
    this.addDvdOrderDvd(temp);
    session.saveOrUpdate(session.merge(temp));
    session.saveOrUpdate(session.merge(dvd));
    float price = temp.computePrice();
    temp.setPrice(price);
    session.saveOrUpdate(session.merge(temp));
    this.price = new Float(this.price + price);
    session.saveOrUpdate(session.merge(this));
}

From source file:io.jeandavid.projects.vod.service.DvdOrderFacadeREST.java

License:Open Source License

@POST
@Path("{id}/dvd")
@Consumes(MediaType.APPLICATION_JSON)//www.j av a 2s .  c  om
public void addDvd(@PathParam("id") Long id, Dvd dvd) {
    Session session = this.getSessionFactory().openSession();
    DvdOrder order = (DvdOrder) session.load(DvdOrder.class, id, LockMode.PESSIMISTIC_WRITE);
    if (order.getInternalState() != DvdOrder.CREATED) {
        session.close();
        return;
    }
    int quantity = dvd.getQuantity();
    session.refresh(dvd);
    Transaction tr = session.beginTransaction();

    session.refresh(order);
    DvdOrderDvd temp = new DvdOrderDvd();
    temp.setQuantity(quantity);
    session.persist(temp);
    dvd.addDvdOrderDvd(temp);
    order.addDvdOrderDvd(temp);
    session.saveOrUpdate(temp);
    session.saveOrUpdate(dvd);
    float price = temp.computePrice();
    temp.setPrice(price);
    session.saveOrUpdate(temp);
    order.updatePrice(order.getPrice() + price);
    session.save(session.merge(order));
    session.flush();
    tr.commit();
    session.close();
}

From source file:io.jeandavid.projects.vod.service.DvdOrderFacadeREST.java

License:Open Source License

@Asynchronous
public void transformIntoSubOrders(DvdOrder dvdOrder) {
    Session rootSession = em.unwrap(Session.class);
    rootSession.refresh(dvdOrder);/*from  w ww.  j  a va 2s. co  m*/
    HashMap<DvdProvider, List<Dvd>> split = dvdOrder.sortByDvdProvider();
    for (Entry<DvdProvider, List<Dvd>> entry : split.entrySet()) {
        Session session = this.getSessionFactory().openSession();
        Transaction tr = session.beginTransaction();
        DvdOrder subOrder = new DvdOrder();
        subOrder.switchInternalState(DvdOrder.PENDING);
        session.persist(subOrder);
        subOrder.setDvdProvider(entry.getKey());
        dvdOrder.addSubOrder(subOrder);
        for (Dvd dvd : entry.getValue()) {
            for (DvdOrderDvd dvdOrderDvd : new HashSet<>(dvdOrder.getDvdOrderDvds())) {
                if (dvdOrderDvd.getDvd().equals(dvd)) {
                    subOrder.addDvdOrderDvd(dvdOrderDvd);
                    session.saveOrUpdate(session.merge(dvdOrderDvd));
                    dvdOrder.getDvdOrderDvds().remove(dvdOrderDvd);
                    rootSession.save(dvdOrder);
                }
            }
        }
        subOrder.computePrice();
        session.persist(subOrder);
        session.flush();
        tr.commit();
        session.close();
        doThePackaging(subOrder);
    }
    this.refreshParentDvdOrderStatus(dvdOrder);
}

From source file:itensil.repository.hibernate.EntityManager.java

License:Open Source License

public Repository createRepository(String mount, User owner, Group editors, Group creators, Group readers,
        Group guests) throws AccessDeniedException, DuplicateException {

    User caller = SecurityAssociation.getUser();
    if (SysAdmin.isSysAdmin(caller)) {
        Session session = HibernateUtil.getSession();

        // 1. Check for duplicate
        Query qry = session.getNamedQuery("RepoMan.getRepoByMount");
        qry.setString("mount", mount);
        if (qry.iterate().hasNext()) {
            throw new DuplicateException(mount);
        }//from www  .j  a  v a  2s.  c o m

        // 2. Create Repository
        RepositoryEntity repository = new RepositoryEntity();
        repository.setMount(mount);
        repository.setManager(this);
        session.persist(repository);

        // 3. Create root node
        NodeEntity root = new NodeEntity();
        root.initNew();
        root.setRepoEntity(repository);
        root.setLocalUri("");
        root.setCollection(true);
        root.setOwner(owner);
        root.setCreateTime(new Date());
        session.persist(root);

        // 4. Assign creator permissions
        if (editors != null) {
            PermissionEntity perm = new PermissionEntity(editors, DefaultNodePermission.WRITE, true);
            perm.setNodeEntity(root);
            session.persist(perm);
        }

        // 5. Assign creator permissions
        if (creators != null) {
            PermissionEntity perm = new PermissionEntity(creators, DefaultNodePermission.CREATE, true);
            perm.setNodeEntity(root);
            session.persist(perm);
        }

        // 6. Assign reader permissions
        if (readers != null) {
            PermissionEntity perm = new PermissionEntity(readers, DefaultNodePermission.READ, true);
            perm.setNodeEntity(root);
            session.persist(perm);
        }

        // 7. Assign reader permissions
        if (guests != null) {
            PermissionEntity perm = new PermissionEntity(guests, DefaultNodePermission.READ, true);
            perm.setNodeEntity(root);
            session.persist(perm);
        }

        return repository;
    }
    throw new AccessDeniedException("createRepository", "sysadmin");
}

From source file:itensil.repository.hibernate.NodeEntity.java

License:Open Source License

/**
 * //from  w ww . j av  a  2s.  com
 * @param version
 * @return ver object
 */
public VersionEntity createVersion(NodeVersion version) {
    VersionEntity ver = new VersionEntity(version);
    ver.initNew();
    ver.setNodeEntity(this);
    if (Check.isEmpty(ver.getNumber())) {
        ver.setNumber("1.0");
    }
    VersionEntity def = getDefaultVersionEnt();
    Session session = HibernateUtil.getSession();
    if (def == null) {
        setDefaultVersionEnt(ver);
    } else if (ver.isDefault()) {
        def.setDefault(false);
        setDefaultVersionEnt(ver);
    }
    session.persist(ver);
    session.update(this);
    Set<VersionEntity> vers = getVersionEntities();
    if (Hibernate.isInitialized(vers)) {
        vers.add(ver);
    }
    return ver;
}