161121、hibernate导致数据出错的两个地方

一、在查询出来的对象上直接设置属性(该属性配置了可以持久化,如果不是可持久化的就没有关系)。

出错的代码:(查询用的不好也会导致数据更新哦)

Pagination pagination = groupJoinService.findByEg(groupJoin, true, cond, Pagination.cpn(pageNo),FrontUtils.pageSize(pageSize));//这是一个分页查询的方法使用hibernate的Criteria,最底下有代码

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);

}

}

}

pagination.setList(list);

正确的代码:

Pagination pagination = groupJoinService.findByEg(groupJoin, true, cond, Pagination.cpn(pageNo),FrontUtils.pageSize(pageSize));

List<GroupJoin> gjs = new ArrayList<GroupJoin>();//重新new一个对象

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);//然后添加进去

}

}

pagination.setList(gjs);

二、设置主键生成方式时自己设置的id会被忽略掉(数据库mysql,持久层框架mysql)

将主键的生成方式设置成了identity,导致了自己设置了id会被忽略,最终使用数据库的自增长主键。

下面是代码及错误的xxx.hbm.xml配置

java代码:(service层中)  

@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);//如果主键生成方式设置成了identity,就算你设置了id也会被忽略掉,直接使用数据库中自增长的id

if(isSellerUrl){//如果是商家链接初始化商家

initSeller(user.getId());

}

}else{

if(isSellerUrl){

Seller seller = sellerService.findById(user.getId());

if(null == seller){

initSeller(user.getId());

}

}

}

return user;

}

Seller.hbm.xml(问题出现在这里)

//省略

<hibernate-mapping package="com.sellers.entity">

<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> 

//省略

修复后:<id name="id" type="integer" column="id" ><generator class="assigned"/></id> 

上面实例查询的代码:

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 OrderBy) {

orderList.add(((OrderBy) 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;

}

上一篇:#Linux学习笔记# Linux在线帮助文档man page


下一篇:五一劳动节,讲讲NEO智能合约的调试