SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
All things are lawful, but not all things are profitable.
Knute Snortum wrote:Please give us a little more context. Are you issuing this right after an INSERT? Is the INSERT failing? Do you have a persistent connection?
Also, I don't think you need a FROM clause.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:My understanding is that you should retrieve the last ID immediately after the INSERT. Closing the connection or not having a persistent connection invalidate the call. Opening another connection may do this too -- don't know. Is there a reason you don't want to get the last ID immediately?
tangara goh wrote:The reason is that I have use generatedKey to get the last_insert_id() but when I set this key to my class - Tutor field Id,and when I called the getMethod to retrieve the Id, it just give me 0.
Why is it so?
This is the same problem that I faced for my List<String>subjects and when I set the class to the subjects, when I use Tutor.getSubjects(), it will show me the name.
However, when I created a method with insertSubjectsTutor(int tutor_id, List<String>subjects>, it won't show up.
Thus, I am also trying Weld which uses dependency injection to see if the m.getTutorId() and m.getSubjects() will show returns the values in another method.
Am I in the right track or there is something that I have missed out such that the values are not returned?
All things are lawful, but not all things are profitable.
tangara goh wrote:I could print out the generatedKey in the insert(Tutor tutor) method but my problem is that when I tried to call the tutor.getId() in another method, it just gives me 0, null pointer error.
I do not know the reason and I googled and checked that I have initialized the Tutor class.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:
tangara goh wrote:I could print out the generatedKey in the insert(Tutor tutor) method but my problem is that when I tried to call the tutor.getId() in another method, it just gives me 0, null pointer error.
I do not know the reason and I googled and checked that I have initialized the Tutor class.
But did you also set the id of the Tutor instance? If you use a framework like JPA that will be set for you, but usually only after the transaction ends. If you don't use a framework (and I'm assuming here that you don't), it's up to you to set the id.
All things are lawful, but not all things are profitable.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions