java - hibernate collections with inheritance -


My case is a form with categories, questions, answers ... in a form there are different categories, of which Everyone has different questions and these questions give one or more possible answers.

In my implementation of Java, I give an object called tree object which implements all the relations between the elements (and other common properties as the date of creation ...). This object has a list of children and is a parent to follow the hierarchy of the form. Then, categories, forms and other elements increase this class and add some extra functionality.

Databases with the specific data for each common data (tree object) and child and parent relationship, and some other tables (forms, categories, ...) for each one, I InheritanceType JOINED

Use the Tree Object Class code (updated to include @ kostja comments):

  @Antity @ table (name = "TREE_OBJECTS") @ Inheritance (strategy = inheritance type.joint) Public abstract class Mr. object Otryobakt {@Id @GeneratedValue (strategy = GenerationType.TABLE) @ Column (name = "id", unique = true, tap qualified = false) private Long id; @OneToMany (cascade = {CascadeType.ALL}, fetch = FetchType.EAGER) @JoinTable (name = "CHILDRENS_RELATIONSHIP") Private list & lt; TreeObject & gt; children; @ManyToOne (fetch = FetchType.EAGER) Private Peru Object Parent; // more parameters, gates and setters}  

For example the form class is:

  @Entity @Table (name = "FORMS") public class Form TreeObject {private string name; // saturs, gesters and other stuff}  

and this method of DAO (I'm using generic to simplify the code, but the code can be read): < / P>

  public T makePersistent (T unit) {setCreationInfo (unit); SetUpdateInfo (unit); Session session = getSessionFactory (). GetCurrentSession (); Session.beginTransaction (); Try {Session.saveOrUpdate (Unit); Session.flush (); Committed to session.getTransaction (). Return unit; } Hold (Runtime Execution E) {session.getTransaction (). Rollback (); Throw e }}  

Category, question and other elements are similar to the form class. I leave them.

The problem is that the list of children does not persist correctly For example, the next exam fails because the matching (). Size () is 0 and not 1 (but other form values ​​are retrieved correctly, only child list is empty):

  @Test public zero StoreFormWithCategory () throws NotValidChildException {form Form = new form (); Form.setName ("Test Form"); Category category = new category (); Form.addChild (category); FormDao.makePersistent (form); Form ReformForm = FormDown Read (form.getId ()); Assert.assertEquals (Reform Form .getId (), form.getId ()); Assert.assertEquals (Reform Form .get Childland.) Size (), 1); }  

If I move the code of the child list in the form class, then it works correctly and passes the test. But the list of parent classes is not working. I do not understand that the only difference is the use of heritage.

The solution to the problem is to interface was fired. I have read that hibernation can not work with intefaces, and that is why the children's parameters have not been implemented with the interface. To use the TreeObject to remove the interface implementation and to solve the problem of some setters and gates methods (in the form of joining). I have not thought that using the interface with the DAO was done to solve this issue. But it seems that the set of objects and the setters also do not use Infifes.

Perhaps, when I have copied the form to the object, I have not used the interface as quick copy paste, and this is the reason why it worked correctly in this case.


Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -