Announcement Announcement Module
Collapse
No announcement yet.
Design patterns on Spring Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Design patterns on Spring Hibernate

    hellow guys, currently I'm creating web application based on spring + hibernate. I just have some question regarding the design patterns of using DAO layer.

    So lets say I have two tables, Student table and Subject table. Then I have 2 DAO to perform crud operations with the table.

    The DAO layer :

    for student;
    Code:
    public class StudentDaoImpl implements StudentDao {
            
             @Autowired
             private SessionFactory sessionFactory;
             public Student getStudeByPk(string studentid){
             // load instance of student
             return sessionFactory.getCurrentSession().load(Student.class, studentid);
             }          
             
             public void saveStudent(Student student){
             sessionFactor.getCurrentSession.save(student);
             }
    }
    for subject:
    Code:
    public class SubjectDaoImpl implements SubjectDao {
            
             @Autowired
             private SessionFactory sessionFactory;
    
             public Subject getSubjectByPk(string subjectId){
             // load instance of subject
             return sessionFactory.getCurrentSession().load(Subject.class, subjectId);
             }          
             
             public void saveSubject(Subject subject){
             sessionFactor.getCurrentSession.save(subject);
             }
    }

    So as you can see, both dao performs direct access to single table.

    My question is what if I want to join two tables? what approach do I need to do in order to do that?

    Any help would be appreciated!

  • #2
    This is actually a hibernate issue for you. You need to link the two objects together(1toM or MtoM) and then create a query that manipulates that relationship.

    All student that have taken a certain subject:
    "from student where student.subject.id = 5"

    or if you have a subject object:
    Query q = session.createQuery("from student where student.subject = :subject");
    q.setEntity("subject",subject);
    return q.list();

    Comment

    Working...
    X