I have three table's student , course , student_course
table student
{
student_id(PK)
}
table course
{
course_id(PK)
}
table student_course
{
student_id(PK+FK)
course_id(PK+FK)
}
I created model class's and configuration files using Hibernate Generation Tool.
It create following files-
1) student.java & student.hbm.xml
2) course.java & course.hbm.xml
And for student_course it creates set in each hbm file with Many-to-Many relationship.
So I want Course object's related to student, for this i want to access student_course table separately.
Right Now i access Course object related to student by accessing set of student_course through student object.I think it is not efficient one.
What is the efficient way to this?
Can i do this
by writing sql query or
by manually creating studentCourse.java & studentCourse.hbm.xml
please suggest me efficient way to access course object's related to student object.
please suggest me efficient way to access course object's related to
student object.
I think what you've got it the right approach. There is a link table but Hibernate has hidden it through the use of a ManyToMany - this is the correct modelling for this relationship. A student can take many courses and a course has many students.
Related
I am setting up a Many-to-Many relationship similar to the example here: https://dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html.
If I want to create another entity called Grade that contains a specific Student-Course pair, how can I do that? I know I can have Grade reference Student and Course individually, but that doesn't reference the STUDENT_COURSE table. Ideally I would be able to have a StudentCourse object in my Grade class, but I don't know how to do that without creating a StudentCourse entity myself.
my project is about to create my own persistence implementation. Maybe it's easy but I can't find the solution. I have Classes annotated with my own annotation #Entity and in Annotation Processor I want to create SQL create table statements and save them into file and then execute them. But how do I sort these statements? I have references in tables, I have to first create the non referencing table and then table referencing on this first table
Ok. I will be concrete. Imagine you have for example 3 Classes: Person Department and Company. They are annotated as Entities and I want to create the SQL statements for create tables. But Company references on Department and Person references on Department, so I have to first create the Department table and then Person or Company. I am creating the statements in Annotation Processor, so I have for disposal the annotated elements. Now I am just sorting String to be written in file. But it isn't very pretty.
ArrayList<String> sortedListOfCommands = new ArrayList<>();
for(String command: listOfCommands){
if(!command.contains("FOREIGN"))
sortedListOfCommands.add(command);
}
for(String command: listOfCommands){
if(!sortedListOfCommands.contains(command))
sortedListOfCommands.add(command);
}
I am working on a little website. The database was created and we need to create objects from sql now.
Usually, in "Many to many" relation, I use a list to represent this relation. (List of ingredient in recipe, and if I need, a list of recipe in ingredient).
But I don't know what is the best way when the junction table contain field.
For example with theses tables:
###### #################### ##########
RECIPE INGREDIENT_IN_RECIPE INGREDIENT
id id_ingredient id
name id_recipe name
quantity
other
Is there a best way to create object from this sql?
I don't know if:
I need to create an third object "IngredientInRecipe". And list it on recipe/ingredient?
Maybe create fields quantity/other in ingredient and use it only when I want to handle ingredient as "ingredientinrecipe"?
Or create a Subclass of Ingredient with quantity/other?
Maybe I'm totally wrong and I just have to create list in recipe and use sql query or array for other things but I'm little bit lost.
This is a simple association class and you would model it like this:
You concrete object model with single tables it pretty fine.
The answer here is based on the question is the INGREDIENT_IN_RECIPE an entity by itself, or is it just a relational table to create the many to many in the db.
Currently, INGREDIENT_IN_RECIPE contains additional information, that is really important and further specifies the relation between RECIPE and INGREDIENT, so this qualifies it is a proper entity.
IMHO, the best way here is to create a entity class for the INGREDIENT_IN_RECIPE table and list it on the RECIPE entity class at least. You need to check if the relation from the INGREDIENT entity is really needed and useful.
I don't really know how to describe this problem (as you tell by the dodgy title), so here's a full description of the problem.
I have two classes, Teacher and Class.
Each teacher can teach to several classes and each class has several teachers.
In Java, we've mapped it like this:
public class Class {
#ManyToMany(mappedBy = "classes")
private List<Teacher> teachers = new ArrayList<>();
}
We have a superclass, User, and two subclasses, Teacher and Student. These classes inherit from the User superclass. So, in our database we have a table called Users which stores all users as teachers or students.
Now, students can only be in one class, so they can only have one ClassID stored in the database.
But as I said before, teachers can have several classes. So the point is : How am I able to store all the classes that a teacher has in the database?
I see Java created an extra table, called User_Class, which is probably ment to store all the different classes a teacher has and vica versa. Problem is that I can't find any documentation about how to work with this.
Can someone please give me a hand here?
Thanks in advance.
Before trying to solve this by code, I think you need to understand it from the database point of view.
You are trying to build a many-to-many relationship. This sort of relationship is built by splitting it in two one-to-many relationships.
So, if you have two entities (namely Course and Teacher... I avoid using "classes" to prevent confusion), and one Course can have many Teachers and one Teacher can have many Courses, then a way to create the relation (in the database) would be like this:
Table Courses
courseId (PK)
courseName
Table Teachers
teacherId (PK)
teacherName
Table Courses_Teachers
courseId (FK, PK)
teacherId (FK, PK)
(PK stands for "primary key", and FK stands for "foreign key")
Hope this little introduction helps you visualize the way to solve your problem.
That approach is based on a composite primary key.
This link is about that:
how to make a composite primary key (java persistence annotation)
I have the DB structure as follows:
table STUDENT (say, id, surname, etc)
table STUDENT_PROPERTIES (say, name_of_the_property:char, value_of_the_property:char, student_id:FK)
table COURSE (id, name, statusofcourse_id)
table STATUSOFSOMETHING (id, name_of_status:char ('active','inactive','suspended' etc))
table STUDENT_COURSE (student_id,course_id,statusofsomething_id)
Let's try to pick up domain objects in my database:
Student and Course are main entities.
Student has a list of courses he attends, also he has a list of properties, that is all for this student.
Next, Course entitity. It may contain a list of students that attend it.
But in fact, the whole structure looks like this: the starting point is Student, with it's PK
we can look a list of his properties,
then we look into the STUDENT_COURSE and extract both FK of the Course entity and also the Status
of the combination, it would look like "Student named bla bla, with all his properties,
attends math and the status of it is "ACTIVE".
now, quotation
1) Each DAO instance is responsible for one primary domain object or entity. If a domain object has an independent lifecycle, it should have its own DAO.
2) The DAO is responsible for creations, reads (by primary key), updates, and deletions -- that is, CRUD -- on the domain object.
Now, first question is
What are entities in my case?
Student, Course, Student_Course, Status = all except for StudentProperties?
Do I have to create a separate DAO for every object?
The entities you will need to create are:
Student
StudentProperties
Course
CourseStatus (not really necessary as you could use an enumerated field in Course instead)
StudentCourse doesn't need to be created, as you can just use a Many-to-Many mapping in Hibernate and it will give you a nice set of courses in your Student object.
Here's a great tutorial on hibernate mapping that does pretty much everything you need:
http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-many-1.html