java - When I select a row in MySQL using hibernate classes, it makes an update automatically -
i'm trying develop blacklist users including several variables. when user sign in application, check if parameters blacklisted or not.
the problem when perform select , database find fits search, automatically perform update clean row.
this mysql log:
86 query select * blacklist mobile_token = 'b' 86 query show warnings 86 query select @@session.tx_read_only 86 query update mydatabase.blacklist set email=null, iban=null, mobile_token=null, nif=null blacklist_id=1 86 query show warnings 86 query commit 86 query set autocommit=1 86 query set autocommit=1 86 query set session transaction read write
this table:
my model:
package models.classes_hibernate; import javax.persistence.*; import static javax.persistence.generationtype.identity; @entity @table(name="blacklist" ,catalog="mydatabase" ) public class blacklist implements java.io.serializable { private integer blacklistid; private string mobiletoken; private string iban; private string nif; private string email; public blacklist() { } @id @generatedvalue(strategy=identity) @column(name="blacklist_id", unique=true, nullable=false) public integer getblacklistid() { return this.blacklistid; } public void setblacklistid(integer blacklistid) { this.blacklistid = blacklistid; } @column(name="mobile_token", nullable = false) public string getmobiletoken() { return this.mobiletoken; } public void setmobiletoken(string name) { this.mobiletoken = mobiletoken; } @column(name="iban", nullable = false) public string getiban() { return this.iban; } public void setiban(string name) { this.iban = iban; } @column(name="nif", nullable = false) public string getnif() { return this.nif; } public void setnif(string name) { this.nif = nif; } @column(name="email", nullable = false) public string getemail() { return this.email; } public void setemail(string name) { this.email = email; } }
and dao:
package models.dao; import com.google.common.collect.lists; import models.classes_hibernate.blacklist; import models.pages.page; import org.hibernate.criteria; import org.hibernate.session; import org.hibernate.criterion.restrictions; import org.hibernate.type.stringtype; import play.logger; import play.db.jpa.jpa; import play.db.jpa.transactional; import javax.persistence.noresultexception; import javax.persistence.query; import java.util.list; public class blacklistdaoimpl implements myappcruddaointerface<blacklist> { @override public void create(blacklist entity) { jpa.em().persist(entity); } @override public blacklist read(integer id) { return jpa.em().find(blacklist.class, id); } public page<blacklist> readall(string orientation,int pagesize, int beginelementid) { query query = null; list<blacklist> blacklists = null; boolean aretheremore = false; page<blacklist> allblacklists = null; int size = 0; if(orientation.equals("all")) { query = jpa.em().createnativequery("select * blacklist",blacklist.class); } if(orientation.equals("lt")) { query = jpa.em().createnativequery("select * blacklist blacklist_id < ? order blacklist_id desc",blacklist.class); query.setparameter(1, beginelementid); size =query.getresultlist().size(); query.setmaxresults(pagesize); } if(orientation.equals("gt")) { query = jpa.em().createnativequery("select * blacklist blacklist_id > ? order blacklist_id asc",blacklist.class); query.setparameter(1, beginelementid); size =query.getresultlist().size(); query.setmaxresults(pagesize); } if (size>pagesize) aretheremore = true; try { blacklists = query.getresultlist(); if (orientation.equals("gt")) { list<blacklist> reverselist = lists.reverse(blacklists); blacklists = reverselist; } allblacklists = new page<blacklist>(blacklists, aretheremore, "blacklist"); return allblacklists; } catch(noresultexception nre){ allblacklists=null; return allblacklists; } } @override public void update(blacklist entity) { jpa.em().merge(entity); } @override public void delete(blacklist entity) { jpa.em().remove(entity); } @override public boolean ismanaged(blacklist entity) { return jpa.em().contains(entity); } @override public void close() { jpa.em().close(); } public boolean ismobiletokenblacklisted(string mobiletoken) { query query = jpa.em().createnativequery("select * blacklist mobile_token = ?",blacklist.class); query.setparameter(1, mobiletoken); blacklist blacklist; try { logger.debug("voy comprobar"); blacklist = (blacklist)query.getsingleresult(); } catch (noresultexception nre){ blacklist=null; } return blacklist != null; }
ismobiletokenblacklisted call:
@post @path("/api/user") @apioperation(position = 3, nickname ="user", value = "sign new user",notes = "minimum json required: ", response = appuserjson.class, httpmethod = "post") @bodyparser.of(bodyparser.json.class) @transactional public static result signup() { appuserdaoimpl appuserdao = new appuserdaoimpl(); appuserjson user = null; appuser appuser = null; blacklistdaoimpl blacklistdao = new blacklistdaoimpl(); try { user = parse(); string encrypt_nif = user.nif; string encrypt_authorization = user.parental_authorization; string encrypt_password = user.password; try { encrypt_password= encryptutils.encrypt(config1.getstring("key"),user.password); if(user.nif!= null) encrypt_nif = encryptutils.encrypt(config1.getstring("key"),user.nif); if(user.parental_authorization!= null) encrypt_authorization = encryptutils.encrypt(config1.getstring("key"),user.parental_authorization); } catch (exception e) { e.printstacktrace(); } appuser = new appuser(new date(), new date(),user.email.tolowercase(), encrypt_password, user.mobile_token, user.mobile_device, 0, 0, 0, 0, encrypt_nif, false,"not_locked", encrypt_authorization, 0, false); if (user.email == null) { return status (200, "email missing"); } else if (blacklistdao.isemailblacklisted(user.email)){ return status(401, "email blacklisted"); } if (user.password == null) return status(201, "password missing"); if (user.mobile_token == null) { return status (206, "mobiletoken missing"); } else if (blacklistdao.ismobiletokenblacklisted(user.mobile_token)){ logger.debug("mobiletoken blacklisted"); return status(401, "mobile token blacklisted"); } if (user.mobile_device== null) return status(207, "mobiledevice missing"); else{ appuserdao.create(appuser); user.app_user_id= appuser.getappuserid(); return ok(json.tojson(user)); } } catch (incompletejsonexception e) { return badrequest("incompletejsonexception"); } catch (duplicatejsonexception e) { return badrequest("duplicatejsonexception"); } }
thanks!
i don't know comes can find way correct thing improve code , exclude queries.
- be sure use bracket around if. it's not compulsory way make code clearer
- in signup method, else not logical. depends on last if (mobiledevice test). want create user if test wrong.
- here want test if have blacklisted element corresponding research. can use count function or exists can more efficient maybe.
you can use debug mode see update done too.
Comments
Post a Comment