標籤:bsp refresh raw pes pre mysq ble 架構 getc
一、在查詢出來的對象上直接設定屬性(該屬性配置了可以持久化,如果不是可持久化的就沒有關係)。
出錯的代碼:(查詢用的不好也會導致資料更新哦)
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;
}
161121、hibernate導致資料出錯的兩個地方