КАТЕГОРИИ:
АстрономияБиологияГеографияДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРиторикаСоциологияСпортСтроительствоТехнологияФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника
|
PersonService
?
| package org.krams.tutorial.service;
import java.util.List;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.krams.tutorial.domain.Person;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* Service for processing Persons
*
*/
@Service("personService")
@Transactional
public class PersonService {
protected static Logger logger = Logger.getLogger("service");
@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
/**
* Retrieves all persons
*
* @return a list of persons
*/
public List<person> getAll() {
logger.debug("Retrieving all persons");
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// Create a Hibernate query (HQL)
Query query = session.createQuery("FROM Person");
// Retrieve all
return query.list();
}
/**
* Retrieves a single person
*/
public Person get( Integer id ) {
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// Retrieve existing person first
Person person = (Person) session.get(Person.class, id);
return person;
}
/**
* Adds a new person
*/
public void add(Person person) {
logger.debug("Adding new person");
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// Save
session.save(person);
}
/**
* Deletes an existing person
* @param id the id of the existing person
*/
public void delete(Integer id) {
logger.debug("Deleting existing person");
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// Retrieve existing person first
Person person = (Person) session.get(Person.class, id);
// Delete
session.delete(person);
}
/**
* Edits an existing person
*/
public void edit(Person person) {
logger.debug("Editing existing person");
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// Retrieve existing person via id
Person existingPerson = (Person) session.get(Person.class, person.getId());
// Assign updated values to this person
existingPerson.setFirstName(person.getFirstName());
existingPerson.setLastName(existingPerson.getLastName());
existingPerson.setMoney(existingPerson.getMoney());
// Save updates
session.save(existingPerson);
}
}</person>
| We've declared a simple CRUD system with the following methods:
getAll()
add()
delete()
edit()
In each method we retrieve the session:
?
This is similar to retrieving a connection from the database so that we can do our work. The Sessionobject provides numerous methods for persisting objects. For this tutorial, we use the following Session methods:
session.createQuery()
session.save()
session.delete()
We're done with the domain and the service layer. Let's move to the Spring controller.
|