Yoshi
Yoshi

Reputation: 57

How to use Spring transactional management to update current user to database?

For Example: login user is [test2] and I want to update mAccount and mPassword.
How to get login session and click on the jsp form to update?

(mId is Primary Key)

What should I do?

When I click submit to update the SQL database, mAccount and mPassword are not updated.

I think my controller must have problem to get login session, so I can't update what I want. I click again and again but SQL database still not any change,controller not get test2 object to update.how to fix controller let me so confuse and anxious.this problem spent me 1week ...

SQL Member database:

enter image description here

Edit: Was same image as above, likely missing the intended one UpdateMemberController debug mode:

<code>UpdateMemberController</code> debug mode

Bean:

private Integer mId;
private String mAccount;
private String mPassword;
private String mName;
private Date mDate;
private String mPhone;
private String mAddress;
private String mGender;
private String mEmail;

MemberDao:

public void setConnection(Connection conn);
public boolean checkAccount(String mAccount);
public int registerMember(MemberBean mb);
public MemberBean queryMember(String mAccount);
public MemberBean checkPassword(String mAccount, String mPassword);
public void updateMember(MemberBean mb);

DaoImpl:

@Override
public void updateMember(MemberBean mb) {

    String hql = "UPDATE MemberBean mb SET mb.mAccount =:mAccount , mb.mPassword =:mPassword WHERE mId =:mId";
    Session session = factory.getCurrentSession();

    session.createQuery(hql).setParameter("mAccount", mb.getmAccount()).setParameter("mPassword", mb.getmPassword())
            .setParameter("mId", mb.getmId()).executeUpdate();

}

MemberService:

boolean accountCheck(String mAccount);
int registerMember(MemberBean mb);
MemberBean queryMember(String mAccount);
public MemberBean checkPassword(String mAccount, String mPassword);
void updateMember(MemberBean mb );

MemberSeriveImpl:

@Transactional
@Override
public void updateMember(MemberBean mb) {
    if (mb.getmAccount() != null && mb.getmPassword() != null) {
        dao.updateMember(mb);
    }
}

Upvotes: 4

Views: 116

Answers (1)

PraveenKumar Lalasangi
PraveenKumar Lalasangi

Reputation: 3543

There is nothing bigger challenge for a developer if he get hold on debugging skills. If anything causing problem to developer he should hold the neck of that and solve it.

  1. Check hibernate.properties for which database you have connected. If you connected to different database, update may be happening there. (If step1 has no problem)

  2. Directly jump to debug your dao implementation.(updateMember() method)

    • Confirm updateMember method is getting invoked or not.(Just put print statement)
    • First you print mAccount, mPassword and mId
    • If printed correctly, then put create query and execute update in try catch block and don't forget to print in catch block.
    • If exception not caught then print the executeUpdate return type.
    • If executeUpdate is zero then go to step 3.

Step 3: Why it is not updating to database. Am i committing transactions?
If i am relied on spring to manage transactions (begin, commit/rollback) then question yourself am i configured it correctly?
(Even i have not worked on Transaction in spring let me guess your problem)
Is spring container picking up service method with @Transactional?
What is your component scan base package? what is the package name of your service?
Is your service class scanned for components?

Note:

Add necessary information in your question
Add servlet-context.xml(in xml configuration) or equivalent java configuration file
Add root-context.xml (in xml configuration) or equivalent java configuration file

Filter question or remove unnecessary information
Bean(required)
MemberDao(required)
MemberService(required)
MemberSeriveImpl(required)
controller(Not required)
jsp(not required)

Upvotes: 2

Related Questions