List of usage examples for org.hibernate Session persist
void persist(Object object);
From source file:cl.model.dao.Operaciones.java
public void hacerReclamo(Reclamo us) { Session sesion = HibernateUtil.getSessionFactory().openSession(); sesion.beginTransaction();/*from ww w . j a v a 2 s . co m*/ sesion.persist(us); sesion.persist(us); sesion.getTransaction().commit(); sesion.close(); }
From source file:com.addshare.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); int m = 0;/*from w ww . ja v a2s .co m*/ HttpSession session = request.getSession(true); try { int comp = Integer.parseInt(request.getParameter("form1")); Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file SessionFactory factory = cfg.buildSessionFactory(); Session session1 = factory.openSession(); Transaction t = session1.beginTransaction(); if (comp == 1) { String comp1 = request.getParameter("comp"); String rate = request.getParameter("rate"); String email = (String) session.getAttribute("email"); int rate1 = Integer.parseInt(rate); int opening = rate1; Date date = new Date(); ShareCom u = new ShareCom(rate1, opening, date, rate1, rate1, email, comp1); session1.persist(u); t.commit(); session1.close(); request.setAttribute("success", "suc"); RequestDispatcher rd = request.getRequestDispatcher("admindash.jsp"); rd.forward(request, response); } } catch (Exception e1) { request.setAttribute("success", "err"); RequestDispatcher rd = request.getRequestDispatcher("admindash.jsp"); rd.forward(request, response); } }
From source file:com.amalto.core.storage.hibernate.ScatteredTypeMapping.java
License:Open Source License
Object _setValues(Session session, DataRecord from, Wrapper to) { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); for (FieldMetadata field : from.getType().getFields()) { FieldMetadata mappedDatabaseField = getDatabase(field); if (mappedDatabaseField == null) { throw new IllegalStateException( "Field '" + field.getName() + "' was expected to have a database mapping"); }// ww w .j a va 2 s . c o m if (field instanceof ContainedTypeFieldMetadata) { if (!(mappedDatabaseField instanceof ReferenceFieldMetadata)) { throw new IllegalStateException("Contained elements are expected to be mapped to reference."); } ReferenceFieldMetadata referenceFieldMetadata = (ReferenceFieldMetadata) mappedDatabaseField; StorageClassLoader classLoader = (StorageClassLoader) contextClassLoader; if (!field.isMany()) { DataRecord referencedObject = (DataRecord) readValue(from, field, mappedDatabaseField, session); Wrapper existingValue = (Wrapper) to.get(referenceFieldMetadata.getName()); if (referencedObject != null) { // Update existing value or update existing if (existingValue != null) { // Check for type change ComplexTypeMetadata existingType = classLoader .getTypeFromClass(existingValue.getClass()); TypeMapping mapping = mappings.getMappingFromDatabase(existingType); if (mapping == null) { throw new IllegalStateException( "Type '" + existingType.getName() + "' has no mapping."); //$NON-NLS-1$ //$NON-NLS-2$ } boolean isSameType = mapping.getUser().equals(referencedObject.getType()); if (isSameType) { to.set(referenceFieldMetadata.getName(), _setValues(session, referencedObject, existingValue)); } else { session.delete(existingValue); Wrapper newValue = createObject(contextClassLoader, referencedObject); to.set(referenceFieldMetadata.getName(), _setValues(session, referencedObject, newValue)); } } else { Wrapper object = createObject(contextClassLoader, referencedObject); to.set(referenceFieldMetadata.getName(), _setValues(session, referencedObject, object)); session.persist(object); } } else { to.set(referenceFieldMetadata.getName(), null); if (existingValue != null) { session.delete(existingValue); } } } else { List<DataRecord> dataRecords = (List<DataRecord>) readValue(from, field, mappedDatabaseField, session); Object value = to.get(getDatabase(field).getName()); List<Wrapper> existingValue = (List<Wrapper>) value; if (dataRecords != null) { if (existingValue != null && existingValue instanceof PersistentList) { ((PersistentList) existingValue).forceInitialization(); } List<Wrapper> objects = existingValue == null ? new ArrayList<Wrapper>(dataRecords.size()) : existingValue; List<Wrapper> newValue = new ArrayList<Wrapper>(); int i = 0; for (DataRecord dataRecord : dataRecords) { if (i < objects.size() && objects.get(i) != null) { ComplexTypeMetadata existingType = classLoader .getTypeFromClass(objects.get(i).getClass()); TypeMapping mapping = mappings.getMappingFromDatabase(existingType); if (mapping == null) { throw new IllegalStateException( "Type '" + existingType.getName() + "' has no mapping."); //$NON-NLS-1$ //$NON-NLS-2$ } boolean isSameType = mapping.getUser().equals(dataRecord.getType()); if (mapping.getUser() instanceof ContainedComplexTypeMetadata && dataRecord.getType() instanceof ContainedComplexTypeMetadata) { isSameType = ((ContainedComplexTypeMetadata) mapping.getUser()) .getContainedType() .equals(((ContainedComplexTypeMetadata) dataRecord.getType()) .getContainedType()); } if (!isSameType) { Wrapper object = createObject(contextClassLoader, dataRecord); newValue.add((Wrapper) _setValues(session, dataRecord, object)); } else { newValue.add((Wrapper) _setValues(session, dataRecord, objects.get(i))); } } else { Wrapper object = createObject(contextClassLoader, dataRecord); newValue.add((Wrapper) _setValues(session, dataRecord, object)); session.persist(object); } i++; } // TMDM-7590: Remove the deleted items if (objects.size() > newValue.size()) { for (i = objects.size() - 1; i >= newValue.size(); i--) { session.delete(objects.get(i)); objects.remove(i); } } objects = newValue; to.set(referenceFieldMetadata.getName(), objects); } else { if (value != null) { List<Wrapper> objects = (List<Wrapper>) value; for (Wrapper object : objects) { session.delete(object); } ((List) value).clear(); } } } } else if (field instanceof ReferenceFieldMetadata) { if (!field.isMany()) { DataRecord referencedObject = (DataRecord) readValue(from, field, mappedDatabaseField, session); if (referencedObject != null) { Collection<FieldMetadata> keyFields = referencedObject.getType().getKeyFields(); Object referenceId; if (keyFields.size() > 1) { List<Object> referenceIdList = new LinkedList<Object>(); for (FieldMetadata keyField : keyFields) { referenceIdList .add(readValue(referencedObject, keyField, mappedDatabaseField, session)); } referenceId = referenceIdList; } else { referenceId = readValue(referencedObject, keyFields.iterator().next(), mappedDatabaseField, session); } to.set(mappedDatabaseField.getName(), getReferencedObject(contextClassLoader, session, mappings.getMappingFromUser(referencedObject.getType()).getDatabase(), referenceId)); } else { to.set(mappedDatabaseField.getName(), null); } } else { List<DataRecord> referencedObjectList = (List<DataRecord>) readValue(from, field, mappedDatabaseField, session); if (referencedObjectList != null) { List<Object> wrappers = new LinkedList<Object>(); for (DataRecord dataRecord : referencedObjectList) { Collection<FieldMetadata> keyFields = dataRecord.getType().getKeyFields(); Object referenceId; if (keyFields.size() > 1) { List<Object> referenceIdList = new LinkedList<Object>(); for (FieldMetadata keyField : keyFields) { referenceIdList .add(readValue(dataRecord, keyField, mappedDatabaseField, session)); } referenceId = referenceIdList; } else { referenceId = readValue(dataRecord, keyFields.iterator().next(), mappedDatabaseField, session); } wrappers.add(getReferencedObject(contextClassLoader, session, dataRecord.getType(), referenceId)); } to.set(mappedDatabaseField.getName(), wrappers); } else { Object value = to.get(mappedDatabaseField.getName()); if (value != null && value instanceof List) { ((List) value).clear(); } } } } else { if (mappedDatabaseField.isMany()) { List<Object> oldValues = (List<Object>) to.get(mappedDatabaseField.getName()); List<Object> newValues = (List<Object>) readValue(from, field, mappedDatabaseField, session); if (oldValues != null) { resetList(oldValues, newValues); } else { to.set(mappedDatabaseField.getName(), newValues); } } else { to.set(mappedDatabaseField.getName(), readValue(from, field, mappedDatabaseField, session)); } } } return to; }
From source file:com.amalto.core.storage.hibernate.SystemScatteredTypeMapping.java
License:Open Source License
Object _setValues(Session session, DataRecord from, Wrapper to) { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); TypeMapping mapping = mappings.getMappingFromUser(from.getType()); Collection<FieldMetadata> fields; if (mapping != null) { fields = mapping.getUser().getFields(); } else {/*from w ww . ja v a 2 s . c o m*/ fields = from.getType().getFields(); } for (FieldMetadata field : fields) { FieldMetadata mappedDatabaseField; if (mapping != null) { mappedDatabaseField = mapping.getDatabase(field); } else { mappedDatabaseField = getDatabase(field); } if (mappedDatabaseField == null) { continue; // throw new IllegalStateException("Field '" + field.getName() + "' was expected to have a database mapping"); } if (field instanceof ContainedTypeFieldMetadata) { if (!(mappedDatabaseField instanceof ReferenceFieldMetadata)) { throw new IllegalStateException("Contained elements are expected to be mapped to reference."); } ReferenceFieldMetadata referenceFieldMetadata = (ReferenceFieldMetadata) mappedDatabaseField; if (!field.isMany()) { DataRecord referencedObject = (DataRecord) readValue(from, field, mappedDatabaseField, session); if (referencedObject != null) { TypeMapping mappingFromUser = mappings.getMappingFromUser(referencedObject.getType()); ComplexTypeMetadata referencedType = mappingFromUser != null ? mappingFromUser.getDatabase() : referencedObject.getType(); Wrapper existingValue = (Wrapper) to.get(referenceFieldMetadata.getName()); boolean needCreate = existingValue == null; if (!needCreate) { ComplexTypeMetadata existingType = ((StorageClassLoader) contextClassLoader) .getTypeFromClass(existingValue.getClass()); needCreate = !existingType.equals(referencedType); } Wrapper object = needCreate ? createObject(contextClassLoader, referencedType) : existingValue; to.set(referenceFieldMetadata.getName(), _setValues(session, referencedObject, object)); if (needCreate) { session.persist(object); } } else { to.set(referenceFieldMetadata.getName(), null); } } else { List<DataRecord> dataRecords = (List<DataRecord>) readValue(from, field, mappedDatabaseField, session); Object value; if (mapping != null) { value = to.get(mapping.getDatabase(field).getName()); } else { value = to.get(getDatabase(field).getName()); } if (dataRecords != null) { List<Wrapper> existingValue = (List<Wrapper>) value; if (existingValue != null) { ((PersistentList) existingValue).forceInitialization(); } List<Wrapper> objects = existingValue == null ? new ArrayList<Wrapper>(dataRecords.size()) : existingValue; int i = 0; for (DataRecord dataRecord : dataRecords) { if (i < objects.size() && objects.get(i) != null) { objects.set(i, (Wrapper) _setValues(session, dataRecord, objects.get(i))); } else { Wrapper object = createObject(contextClassLoader, dataRecord.getType()); objects.add((Wrapper) _setValues(session, dataRecord, object)); session.persist(object); } i++; } // TMDM-5257: Remove the deleted items while (objects.size() > dataRecords.size()) { objects.remove(objects.size() - 1); } to.set(referenceFieldMetadata.getName(), objects); } else { if (value != null && value instanceof List) { ((List) value).clear(); } } } } else if (field instanceof ReferenceFieldMetadata) { if (!field.isMany()) { DataRecord referencedObject = (DataRecord) readValue(from, field, mappedDatabaseField, session); if (referencedObject != null) { Collection<FieldMetadata> keyFields = referencedObject.getType().getKeyFields(); Object referenceId; if (keyFields.size() > 1) { List<Object> referenceIdList = new LinkedList<Object>(); for (FieldMetadata keyField : keyFields) { referenceIdList .add(readValue(referencedObject, keyField, mappedDatabaseField, session)); } referenceId = referenceIdList; } else { referenceId = readValue(referencedObject, keyFields.iterator().next(), mappedDatabaseField, session); } to.set(mappedDatabaseField.getName(), getReferencedObject(contextClassLoader, session, mappings.getMappingFromUser(referencedObject.getType()).getDatabase(), referenceId)); } else { to.set(mappedDatabaseField.getName(), null); } } else { List<DataRecord> referencedObjectList = (List<DataRecord>) readValue(from, field, mappedDatabaseField, session); if (referencedObjectList != null) { List<Object> wrappers = new LinkedList<Object>(); for (DataRecord dataRecord : referencedObjectList) { Collection<FieldMetadata> keyFields = dataRecord.getType().getKeyFields(); Object referenceId; if (keyFields.size() > 1) { List<Object> referenceIdList = new LinkedList<Object>(); for (FieldMetadata keyField : keyFields) { referenceIdList .add(readValue(dataRecord, keyField, mappedDatabaseField, session)); } referenceId = referenceIdList; } else { referenceId = readValue(dataRecord, keyFields.iterator().next(), mappedDatabaseField, session); } wrappers.add(getReferencedObject(contextClassLoader, session, dataRecord.getType(), referenceId)); } to.set(mappedDatabaseField.getName(), wrappers); } else { Object value = to.get(mappedDatabaseField.getName()); if (value != null && value instanceof List) { ((List) value).clear(); } } } } else { if (mappedDatabaseField.isMany()) { List<Object> oldValues = (List<Object>) to.get(mappedDatabaseField.getName()); List<Object> newValues = (List<Object>) readValue(from, field, mappedDatabaseField, session); if (oldValues != null) { resetList(oldValues, newValues); } else { to.set(mappedDatabaseField.getName(), newValues); } } else { to.set(mappedDatabaseField.getName(), readValue(from, field, mappedDatabaseField, session)); } } } return to; }
From source file:com.archive.spring.dao.impl.ImageDAOImpl.java
@Override public void addImage(Image img) { Session session = this.sessionFactory.getCurrentSession(); Transaction t = session.beginTransaction(); session.persist(img); t.commit();/* w ww. j a v a 2s . c o m*/ logger.info("Image added successfully"); }
From source file:com.archive.spring.dao.impl.RoleDAOImpl.java
@Override public boolean addRole(Role role) { Session session = sessionFactory.getCurrentSession(); Transaction t = session.beginTransaction(); try {/*from www . j a va2 s .c om*/ session.persist(role); t.commit(); return true; } catch (Exception e) { logger.warn("Role not added successfully, Role Details=" + role); return false; } }
From source file:com.bolao.persistencia.dao.ApostadorDAO.java
public String persist(Apostador apostador) { try {//w w w . j a va2s.c om Session session = HibernateFactory.getSessionFactory(); session.beginTransaction(); session.persist(apostador); session.getTransaction().commit(); session.close(); } catch (Exception e) { e.printStackTrace(); return HibernateFactory.FALHA; } return HibernateFactory.SUCESSO; }
From source file:com.bolao.persistencia.dao.GrupoDAO.java
public String persist(Grupo grupo) { try {//from www . j a v a 2s. c o m Session session = HibernateFactory.getSessionFactory(); session.beginTransaction(); session.persist(grupo); session.getTransaction().commit(); session.close(); } catch (Exception e) { return HibernateFactory.FALHA; } return HibernateFactory.SUCESSO; }
From source file:com.bolao.persistencia.dao.TimeDAO.java
public String persist(Time time) { try {//ww w. j a v a 2 s .c o m Session session = HibernateFactory.getSessionFactory(); session.beginTransaction(); session.persist(time); session.getTransaction().commit(); session.close(); } catch (Exception ex) { ex.printStackTrace(); return HibernateFactory.FALHA; } return HibernateFactory.SUCESSO; }
From source file:com.buy.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); int cost;/*from w w w. j a va 2s . c o m*/ HttpSession session = request.getSession(true); try { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file SessionFactory factory = cfg.buildSessionFactory(); Session session1 = factory.openSession(); Transaction t = session1.beginTransaction(); String UEmail = (String) session.getAttribute("email"); int price = 0; price = (Integer) session.getAttribute("price"); int i = Integer.parseInt(request.getParameter("i")); cost = Integer.parseInt(request.getParameter("cost")); if (price < cost) { out.println("You Dont have Enough Balance to purchase"); } else { Date d = new Date(); if (i == 1) { String comp = request.getParameter("comp"); String email = request.getParameter("email"); ShareBuy u = new ShareBuy(); u.setDate(d); u.setRate(cost); u.setSellerEmail(email); u.setUseremail(UEmail); u.setStatus("BUY"); u.setCompany(comp); u.setEnd_rate(0); session1.persist(u); price = price - cost; session.removeAttribute("price"); session.setAttribute("price", price); t.commit(); Transaction t1 = session1.beginTransaction(); session1.createSQLQuery( "UPDATE STOCK.STOCKUSER set MONEY=" + price + " WHERE EMAIL='" + UEmail + "' ") .executeUpdate(); t1.commit(); int moneyStock = 0; Transaction t3 = session1.beginTransaction(); List list = session1.createQuery("from com.StockUser Where EMAIL='" + email + "'").list(); Iterator iterator = list.iterator(); for (int j = 0; j < list.size(); j++) { StockUser user = (StockUser) iterator.next(); moneyStock = user.getMoney(); } t3.commit(); moneyStock = moneyStock + cost; Transaction t2 = session1.beginTransaction(); session1.createSQLQuery( "UPDATE STOCK.STOCKUSER set MONEY=" + moneyStock + " WHERE EMAIL='" + email + "' ") .executeUpdate(); t2.commit(); out.print("Success"); Transaction t4 = session1.beginTransaction(); TransactionT tra = new TransactionT(); tra.setAmount(cost); tra.setSellermail(email); tra.setStatus("S-U"); tra.setD(d); tra.setUsermail(UEmail); session1.persist(tra); t4.commit(); } } session1.close(); } catch (Exception e1) { e1.printStackTrace(); } }