Potrzebuje podpowiedzi jak powinien wyglądać prawidłowo układ klas i ich odpowiedzialność.
Pierwsza klasa która chce stworzyć jest klasa MODEL. Umieszcze w niej pola oraz getery i setery.Bedzie to klasa na podstawie której stworze tabele do zapisu danych w mySQL.:
@Entity
public class Student
{
private int id;
private String firstName;
private String lastName;
private Date birthDate;
public Student()
{
}
public Student(int id, String firstName, String lastName, Date birthDate) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
Druga klasa to klasa StudentService. Posiada zmienna student oraz wstrzyknięty obiekt klasy RepositoryStudent. Wrzuce i rozwinę w niej jedynie metody które sa związane z modelem student, tzn dodawanie, usuwanie, odnajdywanie w bazie itp.Nie wiem tylko co powinny zwracać metody w klasie service. Po każdej metodzie powinien być return tak jak w przypadku addStudent czy wystarcza metody takie jak deleteStudent bez return ? Jaka jest roznica w tych metodach ?
@Service
public class StudentService
{
private Student student;
@Inject
private StudentRepository studentRepository
public StudentService{}
@Transactional
public Student addStudent()
{
student = new Student();
studentRepository.add(student);
return student;
}
@Transactional
public void deletStudent()
{
student = new Student();
studentRepository.delete(student);
}
}
Trzecia klasa to klasa StudentRepository gdzie będą wszystkie operacje związane z np. utworzeniem EntityFactory i EntityManagera. I nie rozwinięte metody typu:
@Repository
public class StudentRepository
private emf…
private em…
public void add (Object object)
{
em.getTransaction().begin();
em.persist(object);
em.getTransaction().commit();
}
}
Wiem, że brakuje bardzo duzo kodu do poprawnego dzialania ale nie o to chodzi chodzi o to czy takie rozwiązanie ma sens. Podpowiedzcie jak rozlozyc prawidłowo te klasy ?