161121. Hibernate causes data error in two places

Source: Internet
Author: User
Tags openid

First, set the property directly on the queried object (this property is configured to persist, if it is not persisted, there is no relationship).

Error code: (Bad query will also lead to data updates OH)

pagination pagination = Groupjoinservice.findbyeg (GroupJoin, True, cond, PAGINATION.CPN (PageNo), Frontutils.pagesize ( pageSize));//This is a paged query method using hibernate criteria, the bottom of the code

list<groupjoin> list = (list<groupjoin>) pagination.getlist ();

if (null! = List && list.size () >0) {

for (GroupJoin bean:list) {

if (null = = Bean.getpersons ()) {

Integer curpersons = groupstartservice.getcurpersons (Bean.getstartid ());

Bean.setpersons (curpersons);//This object data in the database is likely to change

if (curpersons >= minpersonone) {

Bean.setstatus (Groupjoin.status_can);

}else{

Bean.setstatus (GROUPJOIN.STATUS_NO);

}

}else{

Bean.setstatus (Groupjoin.status_yes);

}

}

}

Pagination.setlist (list);

The correct code:

pagination pagination = Groupjoinservice.findbyeg (GroupJoin, True, cond, PAGINATION.CPN (PageNo), Frontutils.pagesize ( PageSize));

list<groupjoin> gjs = new arraylist<groupjoin> ();//re-new an object

list<groupjoin> list = (list<groupjoin>) pagination.getlist ();

if (null! = List && list.size () >0) {

for (GroupJoin bean:list) {

if (null = = Bean.getpersons ()) {

Integer curpersons = groupstartservice.getcurpersons (Bean.getstartid ());

Bean.setpersons (curpersons);

if (curpersons >= minpersonone) {

Bean.setstatus (Groupjoin.status_can);

}else{

Bean.setstatus (GROUPJOIN.STATUS_NO);

}

}else{

Bean.setstatus (Groupjoin.status_yes);

}

Gjs.add (bean);//Then add it in

}

}

Pagination.setlist (GJS);

Second, set the primary key generation mode when you set the ID will be ignored (database MySQL, persistent layer framework MySQL)

The generation of the primary key is set to identity, which causes the ID to be ignored and eventually the self-growing primary key of the database is used.

Here is the code and the Xxx.hbm.xml configuration of the error

Java code: (in service layer)

@Override

Public User inituser (String openid,string nickname,string headimgurl,string address,string sex,string Access_token, String Refresh_token

, String expires_in,string state) throws Exception {

User user = Findbyopenid (openId);

Boolean issellerurl = Frontutils.issellerurl (state);

if (null = = user) {

user = new User ();

User.setopenid (OPENID);

User.setaddtime (New Date ());

User.setstatus (integer.valueof (1));

User.setaccesstoken (Access_token);

User.setrefreshtoken (Refresh_token);

user = save (user);

Inituserdetail (User.getid (), Headimgurl, nickname, address, openId, sex);//If the primary key generation method is set to identity, even if you set the ID will be ignored, Direct use of self-growing IDs in the database

if (Issellerurl) {//If it is a merchant link to initialize the merchant

Initseller (User.getid ());

}

}else{

if (Issellerurl) {

Seller Seller = Sellerservice.findbyid (User.getid ());

if (null = = Seller) {

Initseller (User.getid ());

}

}

}

return user;

}

Seller.hbm.xml (the problem appears here)

Omitted

<class name= "Seller" table= "Sl_seller" dynamic-update= "true" >

<meta attribute= "Sync-dao" >false</meta>

<cache usage= "Read-write"/>

<id name= "id" type= "integer" column= "id" ><generator class= "Identity"/></id>

Omitted

After repair: <id name= "id" type= "integer" column= "id" ><generator class= "Assigned"/></id>

The code for the above instance query:

Public pagination Findbyeg (T eg, boolean anyWhere, condition[] conds,

int page, int pageSize, String ... exclude) {

order[] Orderarr = null;

condition[] Condarr = null;

if (conds! = null && conds.length > 0) {

list<order> orderlist = new arraylist<order> ();

list<condition> condlist = new arraylist<condition> ();

for (Condition c:conds) {

if (c instanceof) {

Orderlist.add (() c). GetOrder ());

} else {

Condlist.add (c);

}

}

Orderarr = new Order[orderlist.size ()];

Condarr = new Condition[condlist.size ()];

Orderarr = Orderlist.toarray (Orderarr);

Condarr = Condlist.toarray (Condarr);

}

Criteria crit = Getcritbyeg (eg, anyWhere, Condarr, exclude);

Return Findbycriteria (Crit, page, pageSize, NULL, Orderarr);

}

@SuppressWarnings ("Rawtypes")

protected pagination Findbycriteria (Criteria crit, int pageno,

int pageSize, Projection Projection, Order ... orders) {

int totalcount = ((number) crit.setprojection (Projections.rowcount ()). Uniqueresult ()). Intvalue ();

Pagination p = new pagination (PageNo, pageSize, TotalCount);

if (TotalCount < 1) {

P.setlist (New ArrayList ());

return p;

}

Crit.setprojection (projection);

if (projection = = null) {

Crit.setresulttransformer (criteria.root_entity);

}

if (orders! = null) {

for (Order order:orders) {

Crit.addorder (order);

}

}

Crit.setfirstresult (P.getfirstresult ());

Crit.setmaxresults (P.getpagesize ());

P.setlist (Crit.list ());

return p;

}

161121. Hibernate causes data error in two places

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.