List of usage examples for org.hibernate HibernateException HibernateException
public HibernateException(Throwable cause)
From source file:edu.sjsu.cmpe275.lab2.dao.FriendshipDaoImpl.java
License:Open Source License
@Override public int create(long id1, long id2) { int result = 0; Session session = null;// ww w . j a va2 s. c om Transaction transaction = null; Person person1 = null, person2 = null; try { session = sessionFactory.openSession(); transaction = session.beginTransaction(); person1 = (Person) session.get(Person.class, id1); person2 = (Person) session.get(Person.class, id2); if (person1 == null || person2 == null) { throw new HibernateException("Can't find persons with id1 = " + id1 + " and id2 = " + id2); } List<Person> f = person1.getFriends(); if (!f.contains(person2)) { f.add(person2); } person1.setFriends(f); session.update(person1); f = person2.getFriends(); if (!f.contains(person1)) { f.add(person1); } person2.setFriends(f); session.update(person2); transaction.commit(); } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } result = 1; } finally { if (session != null) { session.close(); } } return result; }
From source file:edu.sjsu.cmpe275.lab2.dao.FriendshipDaoImpl.java
License:Open Source License
@Override public int delete(long id1, long id2) { int result = 1; Session session = null;/*from w w w. j a va 2s .co m*/ Transaction transaction = null; Person person1 = null, person2 = null; try { session = sessionFactory.openSession(); transaction = session.beginTransaction(); person1 = (Person) session.get(Person.class, id1); person2 = (Person) session.get(Person.class, id2); if (person1 == null || person2 == null) { result = 1; throw new HibernateException("can't find person records with id1 = " + id1 + " and id2 = " + id2); } List<Person> l = person1.getFriends(); if (l.contains(person2)) { l.remove(person2); } l = person2.getFriends(); if (l.contains(person1)) { l.remove(person1); } session.update(person1); session.update(person2); transaction.commit(); result = 0; } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } } finally { if (session != null) { session.close(); } } return result; }
From source file:edu.sjsu.cmpe275.lab2.dao.OrgDaoImpl.java
License:Open Source License
@Override public Organization update(Organization organization) { Session session = null;// ww w .j a v a 2 s . com Transaction transaction = null; Organization organizationOld = null; try { session = sessionFactory.openSession(); transaction = session.beginTransaction(); organizationOld = (Organization) session.get(Organization.class, organization.getId()); if (organizationOld == null) { throw new HibernateException("can't find organization with id = " + organization.getId()); } if (organization.getDescription() != null) { organizationOld.setDescription(organization.getDescription()); } if (organization.getName() != null) { organizationOld.setName(organization.getName()); } if (organization.getAddress() != null) { Address address = new Address(organization.getAddress().getStreet(), organization.getAddress().getCity(), organization.getAddress().getState(), organization.getAddress().getZip()); organizationOld.setAddress(address); } session.update(organizationOld); transaction.commit(); } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } } finally { if (session != null) { session.close(); } } return organizationOld; }
From source file:edu.sjsu.cmpe275.lab2.dao.OrgDaoImpl.java
License:Open Source License
@Override public Organization delete(long id) { Session session = null;/* w w w .j a v a2s . c o m*/ Transaction transaction = null; Organization organization = null; try { session = sessionFactory.openSession(); transaction = session.beginTransaction(); organization = (Organization) session.get(Organization.class, id); if (organization == null) { throw new HibernateException("Can't find organization record with id = " + id); } else if (organization.getPersons() != null && organization.getPersons().size() != 0) { organization = null; throw new HibernateException("Can't delete organization record because it contains members."); } session.delete(organization); transaction.commit(); } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } } finally { if (session != null) { session.close(); } } return organization; }
From source file:edu.sjsu.cmpe275.lab2.service.ManageFriendshipController.java
License:Open Source License
/** Add a friendship object (9) Add a friend//from ww w .ja va 2 s .co m Path:friends/{id1}/{id2} Method: PUT This makes the two persons with the given IDs friends with each other. If either person does not exist, return 404. If the two persons are already friends, do nothing, just return 200. Otherwise, Record this friendship relation. If all is successful, return HTTP code 200 and any informative text message in the HTTP payload. * @param a Description of a * @param b Description of b * @return Description of c */ @RequestMapping(value = "/{id1}/{id2}", method = RequestMethod.PUT) public ResponseEntity createFriendship(@PathVariable("id1") long id1, @PathVariable("id2") long id2) { Session session = null; Transaction transaction = null; Person person1 = null, person2 = null; try { session = sessionFactory.openSession(); transaction = session.beginTransaction(); person1 = (Person) session.get(Person.class, id1); person2 = (Person) session.get(Person.class, id2); if (person1 == null || person2 == null) { throw new HibernateException("Can't find persons with id1 = " + id1 + " and id2 = " + id2); } List<Person> f = person1.getFriends(); if (!f.contains(person2)) { f.add(person2); } person1.setFriends(f); session.update(person1); f = person2.getFriends(); if (!f.contains(person1)) { f.add(person1); } person2.setFriends(f); session.update(person2); transaction.commit(); } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } return new ResponseEntity("Can't find persons with id1 = " + id1 + " and id2 = " + id2, HttpStatus.NOT_FOUND); } finally { if (session != null) { session.close(); } } return new ResponseEntity("Created the friendship between id1 = " + id1 + " and id2 = " + id2, HttpStatus.OK); }
From source file:edu.sjsu.cmpe275.lab2.service.ManageFriendshipController.java
License:Open Source License
/** Remove a friendship object (10) Remove a friend//w w w . j a v a 2 s.c o m Path:friends/{id1}/{id2} Method: DELETE This request removes the friendship relation between the two persons. If either person does not exist, return 404. If the two persons are not friends, return 404. Otherwise, Remove this friendship relation. Return HTTP code 200 and a meaningful text message if all is successful. * @param a Description of a * @param b Description of b * @return Description of c */ @RequestMapping(value = "/{id1}/{id2}", method = RequestMethod.DELETE) public ResponseEntity deleteFriendship(@PathVariable("id1") long id1, @PathVariable("id2") long id2) { Session session = null; Transaction transaction = null; Person person1 = null, person2 = null; try { session = sessionFactory.openSession(); transaction = session.beginTransaction(); person1 = (Person) session.get(Person.class, id1); person2 = (Person) session.get(Person.class, id2); if (person1 == null || person2 == null) { throw new HibernateException("can't find person records with id1 = " + id1 + " and id2 = " + id2); } List<Person> l = person1.getFriends(); if (l.contains(person2)) { l.remove(person2); } l = person2.getFriends(); if (l.contains(person1)) { l.remove(person1); } session.update(person1); session.update(person2); transaction.commit(); } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } return new ResponseEntity("can't find person records with id1 = " + id1 + " and id2 = " + id2, HttpStatus.NOT_FOUND); } finally { if (session != null) { session.close(); } } return new ResponseEntity("you have deleted a friendship.", HttpStatus.OK); }
From source file:edu.sjsu.cmpe275.lab2.service.ManageOrgController.java
License:Open Source License
/** (6) Get a organization<br> Path:org/{id}?format={json | xml | html} <br> Method: GET <br>/*from w ww .j av a 2 s . c o m*/ This returns a full organization object with the given ID in the given format. All existing fields, including the optional organization and list of friends should be returned. If the organization of the given user ID does not exist, the HTTP return code should be 404; otherwise, 200. The format parameter is optional, and the value is case insensitive. If missing, JSON is assumed. * @param id Description of a * @param format Description of b * @return Description of c */ @RequestMapping(value = "/{id}", method = RequestMethod.GET) public ResponseEntity getOrganization(@PathVariable("id") long id, @RequestParam(value = "format", required = true) String format) { Session session = null; Transaction transaction = null; Organization organization = null; HttpHeaders httpHeaders = new HttpHeaders(); if ("json".equals(format)) { httpHeaders.setContentType(MediaType.APPLICATION_JSON); } else if ("xml".equals(format)) { httpHeaders.setContentType(MediaType.APPLICATION_XML); } else if ("html".equals(format)) { httpHeaders.setContentType(MediaType.TEXT_HTML); } else { httpHeaders.setContentType(MediaType.APPLICATION_JSON); } try { session = sessionFactory.openSession(); transaction = session.beginTransaction(); organization = (Organization) session.get(Organization.class, id); if (organization == null) { throw new HibernateException("can't find record with id = " + id); } transaction.commit(); } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } return new ResponseEntity("can't find record with id = " + id, httpHeaders, HttpStatus.NOT_FOUND); } finally { if (session != null) { session.close(); } } return new ResponseEntity(organization, httpHeaders, HttpStatus.OK); }
From source file:edu.sjsu.cmpe275.lab2.service.ManageOrgController.java
License:Open Source License
/** Function and Requirement: (7) Update an organization/*ww w .j a v a 2 s. c o m*/ Path: org/{id}?name=XX description=YY street=ZZ ... Method: POST This API updates an organization object. For simplicity, all the fields (name, description, street, city, etc), except ID, are passed in as query parameters. Only name is required. Similar to the get method, the request returns the updated organization object, including all attributes in JSON. If the organization ID does not exist, 404 should be returned. If required parameters are missing, return 400 instead. Otherwise, return 200. * @param id Description of a * @param name Description of b * @param description Description of a * @param street Address of Organization * @param city Address of Organization * @param state Address of Organization * @param zip Address of Organization * @return Description of c */ @RequestMapping(value = "/{id}", method = RequestMethod.POST) public ResponseEntity updateOrganization(@PathVariable("id") long id, @RequestParam(value = "name", required = true) String name, @RequestParam(value = "description", required = false) String description, @RequestParam(value = "street", required = false) String street, @RequestParam(value = "city", required = false) String city, @RequestParam(value = "state", required = false) String state, @RequestParam(value = "zip", required = false) String zip) { Session session = null; Transaction transaction = null; Organization organization = null; try { session = sessionFactory.openSession(); transaction = session.beginTransaction(); organization = (Organization) session.get(Organization.class, id); if (organization == null) { throw new HibernateException("can't find organization with id = " + id); } organization.setDescription(description); organization.setName(name); organization.setAddress(new Address(street, city, state, zip)); session.update(organization); transaction.commit(); } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } return new ResponseEntity("can't find organization with id = " + id, HttpStatus.NOT_FOUND); } finally { if (session != null) { session.close(); } } return new ResponseEntity(organization, HttpStatus.NOT_FOUND); }
From source file:edu.sjsu.cmpe275.lab2.service.ManageOrgController.java
License:Open Source License
/** Delete an organization object (8) Delete an organization//from w w w . j a va 2 s.co m URL: http://org/{id} Method: DELETE This method deletes the organization object with the given ID. If there is still any person belonging to this organization, return 400. If the organization with the given ID does not exist, return 404. Return HTTP code 200 and the deleted object in JSON if the object is deleted; * @param id Description of a * @return Description of c */ @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public ResponseEntity deleteOrganization(@PathVariable("id") long id) { Session session = null; Transaction transaction = null; Organization organization = null; try { session = sessionFactory.openSession(); transaction = session.beginTransaction(); organization = (Organization) session.get(Organization.class, id); if (organization == null) { throw new HibernateException("Can't find organization record with id = " + id); } session.delete(organization); transaction.commit(); } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } return new ResponseEntity("Can't find organization record with id = " + id, HttpStatus.NOT_FOUND); } finally { if (session != null) { session.close(); } } return new ResponseEntity(organization, HttpStatus.OK); }
From source file:edu.sjsu.cmpe275.lab2.service.ManagePersonController.java
License:Open Source License
/** Get a person object 2 Path: person/ {id}?format={json | xml | html} Method: GET/* ww w.jav a2 s.c om*/ This returns a full person object with the given ID in the given format in its HTTP payload. ? All existing fields, including the optional organization and list of friends should be returned. The payload should contain the full organization object, if present. The list of friends can be either (a) list of person IDs, or (b) list of shallow ? person objects that do not have their friends list populated. If you take option (b), you want to use techniques like lazy loading to avoid serializing the whole social network starting from the requested person in the returned payload. ? If the person of the given user ID does not exist, the HTTP return code should be 404; otherwise, 200. ? The format parameter is optional, and the value is case insensitive. If missing, JSON is assumed. * @param a Description of a * @param b Description of b * @return Description of c */ @RequestMapping(value = "/{id}", method = RequestMethod.GET) public ResponseEntity<?> getPerson(@PathVariable("id") long id, @RequestParam(value = "format", required = true) String format) { Transaction transaction = null; Session session = null; Person person = null; HttpHeaders httpHeaders = new HttpHeaders(); if ("json".equals(format)) { httpHeaders.setContentType(MediaType.APPLICATION_JSON); } else if ("xml".equals(format)) { httpHeaders.setContentType((MediaType.APPLICATION_XML)); } else if ("html".equals(format)) { httpHeaders.setContentType(MediaType.TEXT_HTML); } else { httpHeaders.setContentType(MediaType.APPLICATION_JSON); } try { session = sessionFactory.openSession(); transaction = session.beginTransaction(); person = (Person) session.get(Person.class, id); if (person == null) { throw new HibernateException("Can't find record with id = " + id); } transaction.commit(); } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } return new ResponseEntity<Object>("Can't find record with id = " + id, httpHeaders, HttpStatus.NOT_FOUND); } finally { if (session != null) { session.close(); } } return new ResponseEntity<>(person, httpHeaders, HttpStatus.OK); }