Hibernate Many-to-Many Mapping with seperate dto class - hibernate-mapping

can any one tell how to get hibernate Many-To-Many mapping with different dto files means suppose there are student and teacher relationship in that i want -
1) Student Dto class
2) Teacher Dto class
3) Student_Teacher Dto class and in this class all mapping are there
is this possible and how to do it ??

You need to annotate collection attribute with #ManyToMany annotation on each of the entities. In your case you will want to annotate Student and Teacher entity. Student_Teacher table will be created automatically if you are using schema export or you can use #JoinTable to specify join table details. This join table will have all the mappings.
For more details I have tried to explain #ManyToMany mapping here in simple way.

Related

Mapping UML Association class to Java Model code

I would like to know the proper way to implement UML association class in Java programming code. I have Student and Course classes. Student can attend one or more Courses, Courses can be attended by one or more Student (many-to-many relationship). If I don't have any attribute in association class (despite course and student ids), would it be okey to implement it this way: public class Course{ private List<Student> students ...} public class Student{ private List<Course> course}. Please explain me both situations, because I don't get it if I should have model public CourseEnrollment { private Student; private Course; private LocalDate enrollementDate} and lists of CourseEnrollments in Student and Course classes.
What I tried is explained above with CourseEnrollment class and my doubts.
One easy way to implement an association class is to use a Collection<CourseEnrollment> e.g. an ArrayList<CourseEnrollment>, in both Course and Student, and make sure that CourseEnrollment has a property back to the respective Course and Student.
The navigation would then be indirect: Student -> List of enrolments -> corresponding courses, or Course -> List of enrolments -> corresponding students. The challenge in your code will be to maintain consistency, e.g. if your remove a student from a course, you'll need not only to delete the enrollment from the course's collection, but also from the student's collection.
Another alternative is to use an independent repository of CourseEnrollments, that keeps a list of all the enrolments and provides efficient access for a student and a course, through two HashMap. The advantage is that there's only one place to manage the enrollment, and students or courses can get the relevant and up-to-date links from there. Again, navigation is indirect, this time by querying the repository. The challenge here is to manage decoupling, since every Student or Course would need to know about the repository.
You may have noticed, that in both cases, the trick is to implement the association class as a class, and to decompose the direct association between Student and Course into an indirect association via the association class.
P.S: In the UML semantics, the association class is at the same time a class and an association. But it can have only one name. Chose either chooses or CourseEnrollment, but you should not use different names.

Why does GORM use a join table for one-to-many?

From the Grails documentation, by default one-to-many relationships are represented using a join table.
I don't understand why this is desirable. I had little SQL experience before starting to use Hibernate and/or Grails' GORM. It seems like using a foreign key in the 'many'-side table pointing at a row on the 'one'-side table is the way to implement a one-to-many relationship...
Can anyone explain this sort of design decision?
The reason for using an join table for a unidirectional one-to-many relationship is because the many side of the relationship may have many relationships and does not know of those relationships. Perhaps an example is best here:
class Book {
String title
}
class BookStore {
String name
static hasMany = [books: Book]
}
class Library {
String name
static hasMany = [books: Book]
}
In the above domain, a Book has no need to have both the BookStore and Library IDs on it's table. A Book is perfectly valid without either. By using join tables this keeps from polluting the book table with foreign keys.
Keep in mind because this is modeling uni-directional and not bi-directional relationships.

Skip properties when calling session.Merge in NHibernate

I want to support cascade update of "truncated graphs" in NHibernate.
Say I have a Student entity and a Class entity which has a Students collection. The Students collection is mapped with "cascade all". Now, suppose that in the client only the a class entity was changed, so I want the client to be able to send only the class entity without the contained students. My approach is to let the client send the class entity with the Students property nullified and let the DAL understand that the Students collection should be ignored.
Unfortunately, when NHibernate gets null property class.Students when given to session.Merge, it disconnects the child students from the parent class by setting their FKs to null and/or deleting them (depending on the specific cascade option).
I would expect NHibernate to behave like that only when getting an empty collection and not when the collection is nullified.
Is there a way to workaround this? E.g. by telling NHibernate somehow to skip the nullified properties during merge?
You can set the inverse = "true" for the Student collection in the Class entity, so that it will not clear the FK.

Specifying the Table on a HasMany() relationship mapping in FluentNHibernate

I have a mapping in FluentNHibernate for a HasMany relationship and I'd like to specify a Table on it to override the default Table that nHibernate will look in to find those objects that I have many of. Does that make sense?
So lets say I have a table for Invoices and a table for InvoiceItems and lets say I have table called InvoiceItemsTwo.
I have a class for Invoice and a Class for InvoiceItems as well, and their mappings are pretty straight forward. I'd like to specify in my mapping for Invoice, that it should look for it's items in InvoiceItemsTwo instead of the default InvoiceItems.
So my mapping of that relationship looks like this
HasMany(c => c.InvoiceItems).Cascade.SaveUpdate().Table("InvoiceItemsTwo");
But this doesn't work. I keep getting an error from my website at runtime that says Invalid object name 'InvoiceItems'.
Why is it ignoring the fact that I am explicitly specifying the Table in my mapping on the relationship?
I tried dumping the mapping at run time and it's being setup something like this
<bag cascade="save-update" table="InvoiceItemsTwo">
Any ideas?
The table attribute applies only to many-to-many relationships, not one-to-many.
you can't specify a different table in your mapping class. Fluent NHibernate uses the class mapped on the property list (InvoiceItems).
If yoy want to use another class to map your details table you must create a InvoceItemsTwo class and map it in your master table class.
You could map the list as composite-element instead of a one-to-many relation and then map it to another table. But it is not a good idea. Consider that NH needs to know where to store an object which is in memory. So it may happen that the object is stored in the wrong table.
Either store all the InvoiceItems in separate tables using composite-element instead of one-to-many and components instead of many-to-one (however this is called in Fluent).
Or store all the InvoiceItems in the same table and use regular references.

Relational Data: entity inheritance approaches. Best practice

There are several approaches how to store entities hierarchy in relation database
For example there is person entity (20 basic attributes), student entity (the same as person but several new specific fields are present), employee (the same as person but some new fields are present) e.t.c.
When you advice to use (and not to use) the following data modeling approaches:
One big table with all possible fields + personType marker field (student or employee)
Table inheritance
One Table with XML field (or maybe another data type) to store all the custom fields
Something else but also relational...
Thank you in advance!
A database models facts, not objects, and each table should model a relatively self-contained set of facts. The consequence of this is that your tables should look something like this:
person { person_id PK, name, dob, ... }
student { person_id PK FK(person.person_id), admission_id, year_started, ... }
employee { person_id PK FK(person.person_id), salary_bracket, ... }
An additional consequence is that a student can also be an employee, which probably models real life closer than an inheritance graph would.
Have a look at the hibernate inheritance mapping docs. There you find three common approaches and a list of pros and cons of each.
If you are using an ORM to implement your classes, the ORM tools you are using will provide you options, generally two options, one class one table or one parent class one table and each table for each children class. I am using XPO from Devexpress.com, one ORM framework. It offers these two options.
If you use ORM, I am afraid there are no other generic options.
Ying