Example usage for org.hibernate HibernateException HibernateException

List of usage examples for org.hibernate HibernateException HibernateException

Introduction

In this page you can find the example usage for org.hibernate HibernateException HibernateException.

Prototype

public HibernateException(Throwable cause) 

Source Link

Document

Constructs a HibernateException using the given message and underlying cause.

Usage

From source file:edu.sjsu.cmpe275.lab2.service.ManagePersonController.java

License:Open Source License

/** Update a person object
 3 Path: person /{id} ?firstname=XX&lastname=YY& email=ZZ&description=UU&street=VV$...
 Method: POST//from   ww  w .  ja va 2 s.c  o m
 This API updates a person object.
 ? For simplicity, all the person fields (firstname, lastname, email, street, city,
 organization, etc), except friends, should be passed in as query parameters. Required
 fields like email must be present. The object constructed from the parameters will
 completely replace the existing object in the server, except that it does not change the
 persons list of friends.
 ? Similar to the get method, the request returns the updated person object, including all
 attributes (first name, last name, email, friends , organization, etc), in JSON. If the
 person ID does not exist, 404 should be returned. If required parameters are missing,
 return 400 instead. Otherwise, return 200.
        
 * @param a         Description of a
 * @param b         Description of b
 * @return         Description of c
 */

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public ResponseEntity<?> updatePerson(@PathVariable("id") long id,
        @RequestParam(value = "firstname", required = false) String firstname,
        @RequestParam(value = "lastname", required = false) String lastname,
        @RequestParam(value = "email", required = true) String email,
        @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,
        @RequestParam(value = "orgid", required = false) Long orgid) {
    Session session = null;
    Transaction transaction = null;
    Person person = null;

    try {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        person = (Person) session.get(Person.class, id);
        if (person == null) {
            throw new HibernateException("Can't find any record with id = " + id);
        }
        person.setDescription(description);
        person.setAddress(new Address(street, city, state, zip));
        person.setLastname(lastname);
        person.setFirstname(firstname);
        person.setEmail(email);
        session.update(person);
        transaction.commit();

    } catch (HibernateException e) {
        if (transaction != null) {
            transaction.rollback();
        }
        return new ResponseEntity<Object>("Can't find any record with id = " + id, HttpStatus.NOT_FOUND);
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return new ResponseEntity<Object>(person, HttpStatus.OK);
}

From source file:edu.sjsu.cmpe275.lab2.service.ManagePersonController.java

License:Open Source License

/** Delete a person object
 (4) Delete a person//from  ww w.jav  a 2 s  .  c o  m
 URL: h ttp://person/{ id}
 Method: DELETE
 This deletes the person object with the given ID.
 ? If the person with the given ID does not exist, return 404.
 ? Otherwise, delete the person and remove any reference of this person from your
 persistence of friendship relations, and return HTTP status code 200 and the deleted
 person in JSON.
        
 * @param a         Description of a
 * @param b         Description of b
 * @return         Description of c
 */

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public ResponseEntity deletePerson(@PathVariable("id") long id) {
    Session session = null;
    Transaction transaction = null;
    Person person = null;
    try {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        person = (Person) session.get(Person.class, id);
        if (person == null) {
            throw new HibernateException("Can't find the record with id = " + id);
        }

        //delete friendship
        for (Person p : person.getFriends()) {
            java.util.List<Person> f = p.getFriends();
            if (f.contains(person)) {
                f.remove(person);
                session.update(p);
            }
        }

        //delete this friendship
        person.setFriends(null);
        session.update(person);

        //delete this person
        session.delete(person);

        transaction.commit();

    } catch (HibernateException e) {
        if (transaction != null) {
            transaction.rollback();
        }
        return new ResponseEntity("Can't find the record with id = " + id, HttpStatus.NOT_FOUND);
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return new ResponseEntity(person, HttpStatus.OK);
}

From source file:edu.sjsu.cmpe275.project.dao.RoomDaoImpl.java

License:Open Source License

@Override
public Room updateRoom(Room room) {
    Session session = null;/*from ww  w  .j  a v  a 2  s . co  m*/
    Transaction transaction = null;
    Room roomOld = null;

    try {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        roomOld = (Room) session.get(Room.class, room.getRoomNo());
        if (roomOld == null) {
            throw new HibernateException("Can't find room with id = " + room.getRoomNo());
        }
        if (room.getRoomType() != null) {
            roomOld.setRoomType(room.getRoomType());
        }
        if (room.getBasePrice() != null) {
            roomOld.setBasePrice(room.getBasePrice());
        }
        if (room.getSmoking() != null) {
            roomOld.setSmoking(room.getSmoking());
        }
        if (room.getStatus() != null) {
            roomOld.setStatus(room.getStatus());
        }
        session.update(roomOld);
        transaction.commit();

    } catch (HibernateException e) {
        if (transaction != null) {
            transaction.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return roomOld;
}

From source file:edu.sjsu.cmpe275.project.dao.RoomDaoImpl.java

License:Open Source License

@Override
public Room removeRoom(String roomNo) {

    Session session = null;//from  w  w  w  . j a va2 s.  c o  m
    Transaction transaction = null;
    Room room = null;
    try {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        room = (Room) session.get(Room.class, roomNo);
        if (room == null) {
            throw new HibernateException("Can't find the record with id = " + roomNo);
        }

        /*
         * A room cannot be removed from the system if it is in use
         * or has a reservation.
         */

        for (Reservation reservation : room.getReservationList()) {

            //
        }

        //delete this friendship
        room.setReservationList(null);
        session.update(room);

        //delete this room
        session.delete(room);

        transaction.commit();

    } catch (HibernateException e) {
        if (transaction != null) {
            transaction.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }

    return room;
}

From source file:edu.uiowa.icts.criterion.CastAsVarcharLike.java

License:Apache License

/**
 * {@inheritDoc}/* w  w  w. j ava 2s . co  m*/
 *
 * (non-Javadoc)
 * @see org.hibernate.criterion.Criterion#toSqlString(org.hibernate.Criteria, org.hibernate.criterion.CriteriaQuery)
 */
@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
    if (columns.length != 1) {
        throw new HibernateException("like may only be used with single-column properties");
    }
    return " lower( cast( " + columns[0] + " as varchar ) ) like ? ";
}

From source file:edu.uiowa.icts.criterion.DateLike.java

License:Apache License

/**
 * {@inheritDoc}//from w  w w  . j a v  a2s  .  c  o m
 *
 * (non-Javadoc)
 * @see org.hibernate.criterion.Criterion#toSqlString(org.hibernate.Criteria, org.hibernate.criterion.CriteriaQuery)
 */
@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
    if (columns.length != 1) {
        throw new HibernateException("like may only be used with single-column properties");
    }
    if (SQL_SERVER.equalsIgnoreCase(dialect.toString())) {
        return " cast( convert( varchar( 10 ), " + columns[0] + ", 101 ) as varchar( 10 ) ) like lower( ? ) ";
    } else if (POSTGRES.equalsIgnoreCase(dialect.toString())) {
        return " cast( " + columns[0] + " as varchar ) like lower( ? ) ";
    } else {
        throw new HibernateException("dialect not supported [" + dialect.toString() + "}");
    }

}

From source file:entite.dao.DAOTypeT.java

@Override
public void update(T entity) {
    session = getSession();//from  w  ww  .ja  v  a 2s.  co m
    try {
        transaction = session.beginTransaction();
        session.update(entity);
        transaction.commit();
    } catch (HibernateException hex) {
        throw new HibernateException(hex.getCause());
    } finally {
        session.close();
    }
}

From source file:es.caib.seycon.ng.model.ServerEntityDaoImpl.java

/**
 * @see es.caib.seycon.ng.model.ServerEntityDao#serverToEntity(es.caib.seycon.ng.comu.Server, es.caib.seycon.ng.model.ServerEntity)
 *///w w  w. j av  a 2s .c  o  m
public void serverToEntity(es.caib.seycon.ng.comu.Server source, es.caib.seycon.ng.model.ServerEntity target,
        boolean copyIfNull) {
    // @todo verify behavior of serverToEntity
    super.serverToEntity(source, target, copyIfNull);
    if (source.getBackupDatabase() == null && copyIfNull)
        target.setBackupDatabase(null);
    else if (source.getBackupDatabase() != null) {
        ReplicaDatabaseEntity rde = getReplicaDatabaseEntityDao().load(source.getBackupDatabase());
        if (rde != null)
            target.setBackupDatabase(rde);
        else
            throw new HibernateException(String.format(Messages.getString("ServerEntityDaoImpl.DBnotFound"), //$NON-NLS-1$
                    source.getBackupDatabase()));
    }
}

From source file:eu.java.pg.jsonb.types.JSONBUserType.java

License:Apache License

@Override
public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner)
        throws HibernateException, SQLException {
    try {//w  w w .j a v a  2  s .  c  o m
        final String json = resultSet.getString(names[0]);
        return json == null ? null : objectMapper.readValue(json, jsonClassType);
    } catch (IOException e) {
        throw new HibernateException(e);
    }
}

From source file:eu.java.pg.jsonb.types.JSONBUserType.java

License:Apache License

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
        throws HibernateException, SQLException {
    try {//w  w  w  . jav a2 s  . c  o m
        final String json = value == null ? null : objectMapper.writeValueAsString(value);
        PGobject pgo = new PGobject();
        pgo.setType(JSONB_TYPE);
        pgo.setValue(json);
        st.setObject(index, pgo);
    } catch (JsonProcessingException e) {
        throw new HibernateException(e);
    }
}