[ Pobierz całość w formacie PDF ]

}
public PersonDTO(Long id, String name, Date dateOfBirth,
List relativeList) {
this.id = id;
this.name = name;
this.dateOfBirth = dateOfBirth;
this.relativeList = relativeList;
}
// getters and setters
@Override
public String toString() {
return "PersonDTO{" + "id=" + id + ", name=" + name + ",
dateOfBirth=" + dateOfBirth + ", relativeList=" + relativeList + '}';
}
}
Metody konwertujące zawarte w klasie Person.java:
public static PersonDTO convertToDto(Person object) {
if (object == null) {
return null;
}
PersonDTO out = new PersonDTO();
out.setId(object.getId());
out.setName(object.getName());
out.setDateOfBirth(object.getDateOfBirth());
out.setId(object.getId());
List relativeListObject = object.getRelativeList();
if (relativeListObject == null) {
return out;
}
List relativeListDTO = new ArrayList();
for (Person person : relativeListObject) {
relativeListDTO.add(convertToDto(person));
}
out.setRelativeList(relativeListDTO);
return out;
}
}
6
www.devcastzone.com
public static Person convertFromDto(PersonDTO dto) {
if (dto == null) {
return null;
}
Person out = new Person();
out.setId(dto.getId());
out.setName(dto.getName());
out.setDateOfBirth(dto.getDateOfBirth());
out.setId(dto.getId());
List relativeListDto = dto.getRelativeList();
if (relativeListDto == null) {
return out;
}
List relativeList = new ArrayList();
for (PersonDTO personDTO : relativeListDto) {
relativeList.add(convertFromDto(personDTO));
}
out.setRelativeList(relativeList);
return out;
}
}
Przykładowy plik konfiguracyjny hibernate:
Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-
3.0.dtd">
name="hibernate.dialect">org.hibernate.dialect.DerbyDialect
erty>
name="hibernate.connection.driver_class">org.apache.derby.jdbc.Cl
ientDriver
name="hibernate.connection.url">jdbc:derby://localhost:1527/sampl
e
app
app
name="hibernate.current_session_context_class">thread
create-
drop
HibernateUtil:
7
www.devcastzone.com
package pl.bnsit.rpc.server;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
/**
* Hibernate Utility class with a convenient method to get
Session Factory
* object.
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard
(hibernate.cfg.xml)
// config file.
sessionFactory = new
AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation
failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Interfejs synchroniczny:
package pl.bnsit.rpc.client;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import java.util.List;
import pl.bnsit.rpc.shared.PersonDTO;
@RemoteServiceRelativePath("PersonService")
public interface PersonListService extends RemoteService {
public List persistAndGetAllPerson(PersonDTO
person);
}
8
www.devcastzone.com
Interfejs asynchroniczny:
package pl.bnsit.rpc.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.List;
import pl.bnsit.rpc.shared.PersonDTO;
public interface PersonListServiceAsync {
public void persistAndGetAllPerson(PersonDTO person,
AsyncCallback callback);
}
Mapowanie serwletu:
xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
PersonService
pl.bnsit.rssreader.server.FeedServiceImpl
class>
PersonService
/MainModule/ PersonService
PersonService
pl.bnsit.rpc.server.PersonListServiceImpl
class>
MainModule.html
Implementacja serwisu:
9
www.devcastzone.com
package pl.bnsit.rpc.server;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import pl.bnsit.rpc.client.PersonListService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import pl.bnsit.rpc.shared.PersonDTO;
public class PersonListServiceImpl extends RemoteServiceServlet
implements PersonListService {
static {
System.out.println("Initializing person data");
Session session =
HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
transaction.begin();
List personList = new ArrayList();
personList.add(new Person(null, "Sister", new Date(),
null));
personList.add(new Person(null, "Brother", new Date(),
null));
personList.add(new Person(null, "Me", new Date(), null));
session.save(new Person(null, "Father", new Date(),
personList));
transaction.commit();
}
@Override
public List persistAndGetAllPerson(PersonDTO
person) {
Session session =
HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
transaction.begin();
session.save(Person.convertFromDto(person));
transaction.commit();
return getPersonDto();
}
public static List getPersonList() {
Session session =
HibernateUtil.getSessionFactory().openSession();
return session.createCriteria(Person.class).list();
}
public static List getPersonDto() {
List out = new ArrayList();
for (Person person : getPersonList()) {
out.add(Person.convertToDto(person));
}
return out;
}
}
10
www.devcastzone.com
Częśd kliencka (wywołująca):
package pl.bnsit.rpc.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import java.util.Date;
import java.util.List;
import pl.bnsit.rpc.shared.PersonDTO;
public class MainModule implements EntryPoint{
@Override
public void onModuleLoad() {
PersonListServiceAsync service =
GWT.create(PersonListService.class);
PersonDTO person = new PersonDTO(null, "Me", new Date(),
null);
service.persistAndGetAllPerson(person, new
AsyncCallback() {
@Override
public void onFailure(Throwable caught) {
Window.alert("error occured: "+caught.getMessage());
}
@Override
public void onSuccess(List result) {
Window.alert("People in db: I"+result.size()+"");
}
});
}
}
11 [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • souvenir.htw.pl