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
Related
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.
Say I have a person object with a field that is profession, which is simply a list of strings. What is the canonical way, using Spring and hibernate, to restrict this list to only a subset of professions that are defined by either user or the admin? Ie, the list of global, predefined professions at runtime is {Accountant, Developer}, and the user adds 'Plumber' to the list. Now if a new person is created, I'd like to restrict the possible professions that person can have to the 3 that are in the list.
Originally, I implemented an Enum, but this seems like a poor design, as it's generated at compile time, and can't be added to at run time (I think?). Would the proper way be to define a one column table with profession, and at each request to make a person, populate a singleton with one member, which is a list of the professions? Then the domain object person would only source the profession from the singleton (presumably in the service layer?).
You can create a new Entity Profession which is related to Person with a one-to-many relationship. This way a profession is always only what is persisted by Hibernate. The profession entity does not need much. Just an id and name for now. Later you might add more attributes as you need.
sample:
I have 2 tables, department and employees (these 2 table is link via the department_id foreign key in employees). and obviously, department has a one-to-many relationship with the employees table.
I need to create a form that has a subset of data from both tables.
These are fields in the web form.
family/surname (from employees table)
first name (from employees table)
employment start date (from employees table)
department (drop-down list of the various departments with department_id as the value return and obviously from the departments table).
I'm wondering if creating a new class for these 4 fields is "best practice"? Thanks! :)
If you are just returning one of each, you can simply add them to the Model. If you are returning a bunch of "rows" of these things, then I would say it makes more sense to wrap the values in a Bean and pass back a collection of instances of that bean. It makes it easier to look through them on the view-side.
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.
This is something I'd really like to be able to do - resolve names based on id values without fetching the whole child model.
Here is an example of what I have, a Table say Employee and a Name_Details table
The Employee may look like this
Create Table Employee {
emp_idinteger not null generated by default as identity; -- generated pk
Department varchar(44);
emp_name_id Integer; -- fk to Name_Details table
...other details such as hire_date etc..
}
now emp_id is a foreign key to the name_details table which may look like this:-
Create Table Name_Details {
id Integer;
Name varchar(32);
Address Varchar(127);
Postcode Varchar(10);
--other details..
}
My question is that I'd like to model the first table Employee with a Java class but I am not interested in setting up a one-to-one relationship between the Employee class and the Name_Details table to fetch that whole Name_details table (as its quite large) the only information I want from this second class is just the Name field (found by joining the emp_name_id column with the Name_Details.id column.
So is it possible in JPA to say declare something like a transient variable in my Employee class called say String employeeName and have this retrieved by JPA based on the above relationShip?
This is a simplified example of what I have wgere there are several tables with name-value pairs and the master table has the values. I need this for reading and not updating/deleting etc.
I am not using JPA v1.x with EJB3 (and not hibernate) on WPS 6.1
Thx G
There are a few options.
The first is to just create a Name class that maps to Name_Details but only maps the name and id fields. Employee would then have a OneToOne to Name, and only the name would be read.
A second option is define Name_Details as a #SecondaryTable in Employee and map only the name from it. The JPA spec restricts the secondary table join to have to share the same id, but depending on what JPA provider you are using, using the 1-1 foreign key may be possible (EclipseLink does support this). You could also define a view the does the join and map to the view.
A third solution is to still map all of the Name_Details fields but define them as LAZY. If your JPA provider supports LAZY basics (or fetch groups), then these will not be fetch unless accessed.