0

I need to build a little program that will be able to handle a data of students and courses they have enrolled. The functionality is not that complicated, I want to be able to get the course's average and median, student's average and stuff like that.

So I've read a lot about hibernate and databases, but I have no experience with that (For the record, I obviously don't mind open one of the billion toturials online and learn that by myself, I just need the project to get done by the end of the week).

So I wonder if there is any efficient way to build such program using java classes only? I mean, how can I handle many-to-many relationship without databases in smart way?

user2375340
  • 87
  • 1
  • 2
  • 9
  • Sure. Same way you would do it with Hibernate, minus the annotations and config to read/write to a DB. Basically, build your "domain" objects, maintain them in memory, and come up with some way to persist them (if this is part of the assignment). For the record, with Spring and annotations, JPA and the like are not that hard to get going. – CodeChimp Feb 17 '14 at 19:06
  • jdbc is the very basic way of doing this. i.e., Java classes only, if you like to automate a few jdbc tasks like mapping rows to Domain objects(class mapped to row) then use spring jdbctemplate. – Zeus Feb 17 '14 at 19:09
  • I wouldn't try to do many-to-many relations on your own. There are probably a thousand alternatives to Hibernate. Ranging from direct [JDBC](http://en.wikipedia.org/wiki/Java_Database_Connectivity) (i.e. writing raw SQL queries) to [other](http://www.jpab.org/Home.html) [JPA](http://en.wikipedia.org/wiki/Java_Persistence_API) providers (Hibernate is one) or several NoSQL approaches that tend to question the classic relational model itself e.g. [Neo4J](http://www.neo4j.org/learn/graphdatabase) – zapl Feb 17 '14 at 19:46

1 Answers1

0

The solution you'll need depends largely on whether your many-to-many mapping is one-directional or bi-directional.

If one-directional (only needing to lookup which courses a student is enrolled in, but not what students are enrolled in a course):

Map<Student, List<Course>> mapping = new HashMap<Student, Course>();

public void enrollStudent(Student student, Course course){
    if(mapping.containsKey(student)){
        mapping.put(student, mapping.get(student).add(course));
    } else {
        mapping.put(student, new ArrayList<Course>().add(course));
    }
}

public List<Course> getStudentsCourses(Student student){
    return mapping.get(student);
}

If, however, you require it to be bi-directional, you will need to include a bi-directional map in place of the hashmap above (and then create corresponding inverse-direction functions). Take a look at Google's BiMap or Apache's BidiMap

Jake Haller-Roby
  • 6,335
  • 1
  • 18
  • 31
  • First, your answer was very informative so thank you. So as much as I get it, Bidi-Map won't let me put 2 students that have enrolled to the same courses. Am I right? By the way, I do need it to be bi-directional. So I can't see how it helps me to put the Bidi-Map instead of the HashMap (if I want to look up for the students that enrolled to a specific course, for example). – user2375340 Feb 17 '14 at 20:55
  • Bidirectional will definitely be more difficult. I would honestly suggest using JDBC or Hibernate if you need functionality this complex. But if you're deadset on doing it in java, check out this question/answer: http://stackoverflow.com/questions/8066109/bidirectional-multi-valued-map-in-java?rq=1 – Jake Haller-Roby Feb 17 '14 at 21:15