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:net.purnama.pureff.dao.SellPriceDao.java

public SellPriceEntity addSellPrice(SellPriceEntity sellprice) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(sellprice);
    return sellprice;
}

From source file:net.purnama.pureff.dao.UomDao.java

public UomEntity addUom(UomEntity uom) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(uom);
    return uom;
}

From source file:net.purnama.pureff.dao.UserDao.java

public UserEntity addUser(UserEntity user) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(user);
    return user;/*w w w . java  2s .  c  o  m*/
}

From source file:net.purnama.pureff.dao.WarehouseDao.java

public WarehouseEntity addWarehouse(WarehouseEntity warehouse) {
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(warehouse);
    return warehouse;
}

From source file:nl.b3p.gis.viewer.services.MaatregelService.java

License:Open Source License

/**
 * Save the maatregel/*from w w w. ja  v a  2  s  .c  o  m*/
 */
public String save(String geplandeMaatregel) throws JSONException {
    JSONObject json = new JSONObject();
    json.put("success", false);
    Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tx = null;
    String error = "";
    try {
        tx = sess.beginTransaction();
        JSONObject plan = new JSONObject(geplandeMaatregel);
        //maatregel needed.
        if (!plan.has("maatregel")) {
            throw new Exception("No maatregel found!");
        }
        MaatregelGepland mg = null;
        if (plan.has("id")) {
            mg = (MaatregelGepland) sess.get(MaatregelGepland.class, plan.getLong("id"));
        }
        if (mg == null) {
            mg = new MaatregelGepland();
        }
        Maatregel m = (Maatregel) sess.get(Maatregel.class, plan.getString("maatregel"));
        mg.setMaatregel(m);
        if (plan.has("hoeveelheid")) {
            mg.setHoeveelheid(plan.getInt("hoeveelheid"));
        } else {
            mg.setHoeveelheid(null);
        }
        if (plan.has("objectType") && plan.get("objectType") != null) {
            mg.setObjectType(plan.getString("objectType"));
        } else {
            mg.setObjectType(null);
        }
        if (plan.has("featureId")) {
            mg.setFeatureId(plan.getString("featureId"));
        }
        if (plan.has("bronId")) {
            mg.setBronId(plan.getInt("bronId"));
        }
        if (mg.getCustomInputs() != null) {
            for (MaatregelCustomInput mci : mg.getCustomInputs()) {
                sess.delete(mci);
            }
            mg.setCustomInputs(null);
            sess.flush();
        }
        if (plan.has("customInputs")) {
            JSONArray ciArray = plan.getJSONArray("customInputs");
            for (int i = 0; i < ciArray.length(); i++) {
                MaatregelCustomInput mci = mapper.readValue(ciArray.get(i).toString(),
                        MaatregelCustomInput.class);
                mg.addCustomInput(mci);
            }
        }
        if (mg.getEigenschappen() != null) {
            for (MaatregelEigenschap eigenschap : mg.getEigenschappen()) {
                sess.delete(eigenschap);
            }
            mg.setEigenschappen(null);
            sess.flush();
        }
        if (plan.has("eigenschappen")) {
            JSONArray eigenschappen = plan.getJSONArray("eigenschappen");
            for (int i = 0; i < eigenschappen.length(); i++) {
                JSONObject eigenschap = eigenschappen.getJSONObject(i);
                MaatregelEigenschap me = mapper.readValue(eigenschap.toString(), MaatregelEigenschap.class);
                mg.addEigenschap(me);
            }
        }
        if (mg.getId() != null) {
            sess.save(mg);
        } else {
            sess.persist(mg);
        }
        sess.flush();
        tx.commit();
        json.put("success", true);
        json.put("error", error);
    } catch (Exception e) {
        log.error("Error while saving", e);
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
        json.put("error", "error while saving: " + e.getMessage());
    }
    return json.toString();
}

From source file:omero.cmd.graphs.DuplicateI.java

License:Open Source License

/**
 * Duplicate model object properties, linking them as appropriate with each other and with other model objects.
 * @throws GraphException if duplication failed
 *///from  w  w w  .j a v a  2s. c o m
private void persistDuplicatesWithNewPropertyValues() throws GraphException {
    /* copy property values into duplicates and link with other model objects */
    final Session session = helper.getSession();
    final ListMultimap<IObject, IObject> propertyUpdateTriggers = ArrayListMultimap.create();
    final Set<IObject> persisted = new HashSet<IObject>();
    final Set<IObject> remainingTransient = new HashSet<IObject>(originalsToDuplicates.values());
    while (!remainingTransient.isEmpty()) {
        boolean isProgress = false;
        final Iterator<IObject> remainingTransientIterator = remainingTransient.iterator();
        while (remainingTransientIterator.hasNext()) {
            final IObject duplicate = remainingTransientIterator.next();
            final Set<IObject> blockers = blockedBy.get(duplicate);
            blockers.retainAll(remainingTransient); /* changes to the view affect the underlying map */
            if (blockers.isEmpty()) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("duplicating an instance of " + duplicate.getClass().getName());
                }
                /* before persisting an object, fill in its links to other objects */
                for (final PropertyUpdate update : propertiesToUpdate.get(duplicate)) {
                    final Function<Object, IObject> duplicateProxyLookup = new Function<Object, IObject>() {
                        @Override
                        public IObject apply(Object original) {
                            if (original instanceof IObject) {
                                final String originalClass;
                                if (original instanceof HibernateProxy) {
                                    originalClass = Hibernate.getClass(original).getName();
                                } else {
                                    originalClass = original.getClass().getName();
                                }
                                final Long originalId = ((IObject) original).getId();
                                final IObject duplicate = originalClassIdToDuplicates
                                        .get(Maps.immutableEntry(originalClass, originalId));
                                if (duplicate == null) {
                                    return null;
                                }
                                if (persisted.contains(duplicate)) {
                                    return duplicate;
                                } else {
                                    /* this value is omitted from the object's property value when it is persisted so we note
                                     * to update this property when the value is persisted */
                                    propertyUpdateTriggers.put(duplicate, update.duplicate);
                                }
                            }
                            return null;
                        }
                    };
                    update.execute(duplicateProxyLookup);
                }
                /* fill in other objects' links to the object to be persisted, such as back-references */
                persisted.add(duplicate);
                final Function<Object, IObject> duplicateProxyLookup = new Function<Object, IObject>() {
                    @Override
                    public IObject apply(Object original) {
                        if (original instanceof IObject) {
                            final String originalClass;
                            if (original instanceof HibernateProxy) {
                                originalClass = Hibernate.getClass(original).getName();
                            } else {
                                originalClass = original.getClass().getName();
                            }
                            final Long originalId = ((IObject) original).getId();
                            final IObject duplicate = originalClassIdToDuplicates
                                    .get(Maps.immutableEntry(originalClass, originalId));
                            if (duplicate == null) {
                                return null;
                            }
                            if (persisted.contains(duplicate)) {
                                return duplicate;
                            }
                        }
                        return null;
                    }
                };
                for (final IObject objectToUpdate : propertyUpdateTriggers.get(duplicate)) {
                    for (final PropertyUpdate update : propertiesToUpdate.get(objectToUpdate)) {
                        update.execute(duplicateProxyLookup);
                    }
                }
                propertyUpdateTriggers.removeAll(duplicate);
                final Collection<PropertyUpdate> propertiesToUpdateForDuplicate = new ArrayList<PropertyUpdate>(
                        propertiesToUpdate.get(duplicate));
                /* when an object is persisted its hash changes, so move key in persisted and in propertiesToUpdate */
                propertiesToUpdate.removeAll(duplicate);
                persisted.remove(duplicate);
                session.persist(duplicate);
                propertiesToUpdate.putAll(duplicate, propertiesToUpdateForDuplicate);
                persisted.add(duplicate);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("persisted " + duplicate.getClass().getName() + ":" + duplicate.getId());
                }
                remainingTransientIterator.remove();
                isProgress = true;
            }
        }
        if (!isProgress) {
            throw new GraphException("internal duplication error: cyclic model graph");
        }
    }
}

From source file:OpenRate.customerinterface.webservices.AccountFacade.java

License:Open Source License

public long saveAccount(Session session, Account account) {
    long tmpAccountID;

    account.setModT(Calendar.getInstance().getTimeInMillis());
    session.save(account);/* w  ww . jav a  2  s  . c o  m*/
    session.persist(account);
    tmpAccountID = account.getAccountID();

    return tmpAccountID;
}

From source file:OpenRate.customerinterface.webservices.AccountVersionFacade.java

License:Open Source License

public Long saveAccountVersion(Session session, AccountVersion accountVersion) {
    accountVersion.setModT(Calendar.getInstance().getTimeInMillis());
    session.save(accountVersion);/*w ww  .  ja v a2 s  .  c om*/
    session.persist(accountVersion);

    return accountVersion.getAccountVerID();
}

From source file:OpenRate.customerinterface.webservices.AccountVersionFacade.java

License:Open Source License

public void updateAccountVersion(Session session, AccountVersion accountVersion, int EndDate) {
    accountVersion.setModT(Calendar.getInstance().getTimeInMillis());
    accountVersion.setEndDate(EndDate);/*from w ww.java  2 s .  c o m*/
    session.saveOrUpdate(accountVersion);
    session.persist(accountVersion);
}

From source file:OpenRate.customerinterface.webservices.AliasFacade.java

License:Open Source License

public void saveAlias(Session session, Alias alias) {
    alias.setModT(Calendar.getInstance().getTimeInMillis());
    session.save(alias);//from   w w w .j  a  v a 2 s  . c  om
    session.persist(alias);
}