Hello together!
Ive got some questions to the structure of my hibernate-code and I would be glad if you could help me.
To show what I mean, I wrote some classes which clarify my problems in general. The questions are written down below.
Code:
public class Checks {
private HibernateDao projectDao;
@Transactional(readOnly = true)
public Map startChecks(String a, String b) {
// calls three checking methods, for example check1(), check2(), check3()
// two of them are requesting data from the database and so are marked as @Transactional
// depending on the result, a Model is composed and then returned
return myModel;
}
@Transactional(readOnly = true)
public String check1(String a) {
// checks something by requesting data from the database
// that means: calls method frm the class: HibernateDao
// returns something, for example a String
}
public String check2(String a) {
// checks something without requesting data from the database
// returns something, for example a String
}
@Transactional(readOnly = true)
public String check3(String a) {
// checks something by requesting data from the database
// that means: calls method frm the class: HibernateDao
// returns something, for example a String
}
}
Code:
public class CheckManager {
private Checks checksClass;
public void callChecks(){
checksClass.startChecks();
}
}
My first question is: Why do I have to mark startChecks() as Transactional? If I dont, it is not working. I guess I must mark it as Transactional, because it calls Transactional-Methods.
What irritates me: If I would put startChecks() in another class, I would not have to mark it as Transactional - why??? (Like: callChecks() must not be marked as @Transactional)
My second question is: The methods requesting the data in my HibernateDao-Class are not marked as Transactional, but the methods calling those HibernateDao-Methods are. Is that correct?
My third question is: As Im new to Hibernate, Im not sure how to handle Exceptions. I created my HibernateDao that way, that - if there is no result - null is returned. So I can always use (if result==null ... else... etc)
Now I did not know exactly if I have to surround something by try-catch in case the database-query does not work (for example no connection or something like this - I dont know)
As my project is growing now I have various attempts for using try-catch:
1. I surround the calls of the HibernateDao-methods in check1() and check3().
2. I surround the calls of check1() and check3() in the method startChecks()
3. I surround the call of startChecks() in callChecks() from CheckManager
- I even combine those methods, for example 2. + 3. and I even go further and surround the call of callChecks in another method :-)
It seems pretty cumbersome to me. Do I have to use try-catch at all? And if yes, where do I have to put it?
It would be great if someone could help me, because Im new to it and Im a little bit confused. :-)