JPA query statements automatically become update

Source: Internet
Author: User

It was written before.


Today, I met a strange problem, clearly DAO in a query function, has been reported connection Read-only error, breakpoint set in the past, found clearly a SELECT query statement, all the time to generate query to see the variables inside are the code of Inquiry, Can be a implementation of the update, almost see a ghost.
My architecture is SSHJ, the hierarchy is entity,dao,service,action, the code is as follows, very simple code:


DAO Layer:

public boolean Findcanxiaohu (string code) {
		try {
			string querystr = '  select COUNT (*) from t_tablename ';
			Query query = em.createnativequery (QUERYSTR);
			Long res = (long) query.getsingleresult ();
			if (Res > 0) {return
				false;
			} else {return
				true;
			}
		\ catch (Exception e) {return
			TRUE;
  }
	}

Serivce Layer:

public boolean Getcanxiaohu (String code) {return
  Dao.findcanxiaohu (code);
 }

Action Layer:

public string edit () {
  string res =  Super.edit ();
  if ("1". Equals (Entity.getyhzhanghuxingzhi ()) {
   String code = Entity.getyhcunkuanrenbianhao ();
   Canxiaohu = Service.getcanxiaohu (code);
  } else {
   Canxiaohu = true;
  }
  return "input";
 

At first I thought it was because I wrote the Preparemodel method in the action layer, and the method didn't write to the database operation, but just in case, I changed it to the edit method, which is to open an editing page, The Preparemodel is executed in its prepareedit function, but the entity is found from the database based on the ID passed.
Strange to be strange in one execution to Getcanxiaohu, see Breakpoint also executed to Dao.findcanxiaohu (), to tell the truth, I found the problem in front of the process, but also the function name changed to this, there is no prefix find, get, as said above, I breakpoint set to query statements, step-by-step execution, generate query when you look inside the statement or select, can be executed to Query.getsingleresult (), it is directly abnormal, and then look at the output statement, became an update t_table statement, Where the SET statement set the values of the fields, because this function is a query function, no transaction, so cannot execute update, so there will be an error. But it is a query, I do not intend to make changes, do not know why this problem, how to change is not, also do not understand what is going on.
I'll try it all, and finally, I changed the statement to the JPQL standard statement, and then, actually can ...

public boolean Findcanxiaohu (string code) {
  try {
   string querystr = '  select COUNT (o) from ClassName o ';
   Query query = em.createquery (QUERYSTR);
   Long res = (long) query.getsingleresult ();
   if (Res > 0) {return
    false;
   } else {return
    true;
   }
  \ catch (Exception e) {return
   TRUE;
  }
 }



No error, but also the implementation of the query, although successful, but I still do not know what is going on.
Record here, expect the hero to guide.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.