Hibernate自定义主键生成器

1:Entity实体配置

 /**
 * Test.java
 * 创建:刘彦亮 2015年4月8日
 */
package com.saving.ecm.resource.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

/**
 * @author lyl
 * @version 1.0
 */
@XmlRootElement
@Entity
@Table(name = "test")
public class Test {
	@Id
	@GeneratedValue(generator = "idGenerator")
	@GenericGenerator(name = "idGenerator", strategy = "com.saving.ecm.resource.entity.KeyUtils",
	parameters = { @Parameter(name = "dataCenterID", value = "123456") ,@Parameter(name = "idLength", value = "10")})
	private String code;

	@Column(name = "name", nullable = false, columnDefinition = "varchar(48) default '' comment '名称'")
	private String name;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


}

2:自定义主键生成器

/**
 * AssignedSequenceGenerator.java
 * 创建:刘彦亮 2015年4月8日
 */
package com.saving.ecm.resource.entity;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.AbstractUUIDGenerator;
import org.hibernate.id.Configurable;
import org.hibernate.type.Type;

/**
 * @author lyl
 * @version 1.0
 */
public class KeyUtils extends AbstractUUIDGenerator implements Configurable {

	public String dataCenterID;
	public String idLength;

	public void configure(Type type, Properties params, Dialect d) {
		this.dataCenterID = params.getProperty("dataCenterID");
		this.idLength = params.getProperty("idLength");
	}

	public Serializable generate(SessionImplementor session, Object obj) {
		// Date today = new Date();
		// SimpleDateFormat formatDate = new SimpleDateFormat("yyyyMMddhhmmss");
		// String time = formatDate.format(today);
		return dataCenterID + "|" + idLength;
	}

}

3:执行save

public void insertTest() throws Throwable {
		IPersistAction action = null;
		try {
			Test t = new Test();
			t.setName("dasdasd");
			TestDAO tDao = (TestDAO) TransactionManagerFactory.getDAO(TestDAO.class);
			action = PersistenceFactory.createPersistenceAction(PersistenceFactory.PersistenceUnitNative);
			action.transact();
			tDao.saveEntity(action.getEntityManager(), t);
			action.flush();
		} catch (Exception ex) {
			ex.printStackTrace();
			if (action != null)
				action.rollback();
		} finally {
			if (action != null)
				action.close();
		}

	}

4:查看结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十五楼亮哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值