使用JPA自定义id策略避免主键自增
作者:勉之~
这篇文章主要介绍了使用JPA自定义id策略避免主键自增问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
JPA自定义id策略避免主键自增
1.实体Entity处理
/** * 主键id */ @Id @GeneratedValue(strategy = GenerationType.AUTO, generator = "myid") @GenericGenerator(name = "myid", strategy = "com.strategy.ManulInsertGenerator") @Column(name = "id" ) private Long id;
2.策略的设置
package com.strategy; import org.hibernate.HibernateException; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.id.IdentityGenerator; import java.io.Serializable; /** * 自定义的主键生成策略,如果填写了主键id,如果数据库中没有这条记录,则新增指定id的记录;否则更新记录 * * 如果不填写主键id,则利用数据库本身的自增策略指定id * */ public class ManulInsertGenerator extends IdentityGenerator { @Override public Serializable generate(SharedSessionContractImplementor s, Object obj) throws HibernateException { Serializable id = s.getEntityPersister(null, obj).getClassMetadata().getIdentifier(obj, s); if (id != null && Integer.valueOf(id.toString()) > 0) { return id; } else { return super.generate(s, obj); } } }
3.jpa多表分页处理
@Override public List<RetUser> userPageList(ReqQueryUser input, String appKey) { QRoleEntity qRoleEntity = QRoleEntity.roleEntity; QHrGroupEntity qHrGroupEntity = QHrGroupEntity.hrGroupEntity; QUserRoleEntity qUserRoleEntity = QUserRoleEntity.userRoleEntity; QUserDeptEntity qUserDeptEntity = QUserDeptEntity.userDeptEntity; JPAQuery<UserEntity> query = jpaQueryFactory.selectFrom(qUserEntity) .leftJoin(qUserRoleEntity).on(qUserEntity.id.eq(qUserRoleEntity.userId) .and(qUserEntity.deleted.isFalse()).and(qUserRoleEntity.deleted.isFalse()).and(qUserRoleEntity.appKey.eq(appKey))) .leftJoin(qRoleEntity).on(qRoleEntity.id.eq(qUserRoleEntity.roleId) .and(qRoleEntity.deleted.isFalse()).and(qUserRoleEntity.deleted.isFalse()).and(qUserRoleEntity.appKey.eq(appKey))) .leftJoin(qUserDeptEntity).on(qUserEntity.id.eq(qUserDeptEntity.userId) .and(qUserEntity.deleted.isFalse()).and(qUserDeptEntity.deleted.isFalse()).and(qUserDeptEntity.appKey.eq(appKey))) .leftJoin(qHrGroupEntity).on(qHrGroupEntity.id.eq(qUserDeptEntity.deptId) .and(qHrGroupEntity.deleted.isFalse()).and(qUserDeptEntity.deleted.isFalse()).and(qUserDeptEntity.appKey.eq(appKey))) .orderBy(qUserEntity.id.asc()); if (!CollectionUtils.isEmpty(input.getUserIds())) { query.where(qUserEntity.id.in(input.getUserIds())); } if (!StringUtils.isEmpty(input.getAdAccount())) { query.where(qUserEntity.adAccount.like("%" + input.getAdAccount() + "%")); } if (!StringUtils.isEmpty(input.getUserName())) { query.where(qUserEntity.userName.like("%" + input.getUserName() + "%")); } if (!StringUtils.isEmpty(input.getEmail())) { query.where(qUserEntity.email.eq(input.getEmail())); } if (!StringUtils.isEmpty(input.getMobile())) { query.where(qUserEntity.mobile.eq(input.getMobile())); } if (!StringUtils.isEmpty(input.getDeptName())) { query.where(qHrGroupEntity.groupName.like("%" + input.getDeptName() + "%")); } if (!StringUtils.isEmpty(input.getRoleName())) { query.where(qRoleEntity.roleName.like("%" + input.getRoleName() + "%")); } if (Objects.nonNull(input.getGender()) && input.getGender() == 0) { query.where(qUserEntity.gender.isFalse()); } if (Objects.nonNull(input.getGender()) && input.getGender() == 1) { query.where(qUserEntity.gender.isTrue()); } List<UserEntity> userEntities = query.select(Projections.bean(UserEntity.class, qUserEntity.id, qUserEntity.userName, qUserEntity.email, qUserEntity.adAccount, qUserEntity.mobile, qUserEntity.birthday, qUserEntity.createdAt, qUserEntity.createdBy, qUserEntity.currAppKey, qUserEntity.gender, qUserEntity.externalId, qUserEntity.disabled, qUserEntity.remark,qUserEntity.address,qUserEntity.headImage,qUserEntity.userNameEn, qRoleEntity.id.as("roleId"), qRoleEntity.roleName, qRoleEntity.roleCode, qHrGroupEntity.id.as("deptId"), qHrGroupEntity.groupName.as("deptName"), qHrGroupEntity.id.as("departmentId"), qHrGroupEntity.groupName.as("departmentName"))).fetch(); //分页 PageUtil.queryWithPaging(input.getPage(), query, qUserEntity); List<RetUser> retUserList = new ArrayList<>(); if (!CollectionUtils.isEmpty(userEntities)) { userEntities.forEach(userEntity -> { RetUser retUser = new RetUser(); BeanUtils.copyProperties(userEntity, retUser); retUser.setGender(userEntity.getGender() == false ? 0 : 1); retUser.setStatus(userEntity.getDisabled() == false ? 0 : 1); retUserList.add(retUser); }); } return retUserList; }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。