我将实体持久化到mysql数据库。我的要求是将ID存储为BR01,BR02,...等,而不是存储为1,2,..等等。
如何将ID存储为BR01,BR02,..等?
我知道mysql数据库不支持序列。
我的实体类如下:
package com.kabira.hrm.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
@Entity
@Table(name="branch")
public class Branch
{
private Long id;
private String country;
private String state;
private String city;
private String address;
private String phoneNumber;
@Id
@GeneratedValue
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getCountry()
{
return country;
}
public void setCountry(String country)
{
this.country = country;
}
public String getState()
{
return state;
}
public void setState(String state)
{
this.state = state;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city = city;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getPhoneNumber()
{
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber)
{
this.phoneNumber = phoneNumber;
}
@Override
public String toString()
{
return "Branch [id=" + id + "]";
}
}
答案 0 :(得分:1)
您可以扩展org.hibernate.id.enhanced.SequenceStyleGenerator
并将其与注释@GenericGenerator一起使用。
答案 1 :(得分:0)
您可能想查看这样的解决方案。 Hibernate: How specify custom sequence generator class name using annotations?通过这种方式,您可以创建自己的密钥生成器。
答案 2 :(得分:0)
编写自定义生成器类并将逻辑放在那里以从db获取max id,然后从该id解析数字并将该数字转换为整数并增加该数字并再次连接所需的前缀并返回连接值
在这里写下这个逻辑
import org.hibernate.id.IdentifierGenerator;
public class MyGenerator implements IdentifierGenerator {@
Override
public Serializable generate(SessionImplementor session, Object object){
// your logic comes here.
}
}