我正在使用带有 spring、h2 和 liquibase 的 hibernate,并且我正在尝试通过以这篇博客文章为例,为我的实体制作一个自定义的 String id 生成器,但我遇到了一个错误:Caused by: org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String
这是我的 SequenceStyleGenerator 代码:
public class CTCIDGenerator extends SequenceStyleGenerator {
@Override
public Serializable generate(SessionImplementor session, Object obj) {
if (obj instanceof Identifiable) {
Identifiable identifiable = (Identifiable) obj;
Serializable id = identifiable.getId();
if (id != null) {
return id;
}
}
return "CTC"+super.generate(session, obj);
}
}
我的实体代码:
@Entity
@Table(name = "contact")
public class Contact implements Serializable, Identifiable<String> {
private static final long serialVersionUID = 1L;
@Id
@GenericGenerator(
name = "assigned-sequence",
strategy = "net.atos.seirich.support.domain.idgenerator.CTCIDGenerator",
parameters = @org.hibernate.annotations.Parameter(
name = "sequence_name",
value = "hibernate_sequence"
)
)
@GeneratedValue(generator = "assigned-sequence", strategy = GenerationType.SEQUENCE)
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
还有 liquibase XML:
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<property name="autoIncrement" value="true" dbms="mysql,h2,postgresql,oracle"/>
<property name="floatType" value="float4" dbms="postgresql, h2"/>
<property name="floatType" value="float" dbms="mysql, oracle"/>
<changeSet id="20160513091901-1" author="jhipster">
<createTable tableName="contact">
<column name="id" type="longvarchar" autoIncrement="${autoIncrement}">
<constraints primaryKey="true" nullable="false"/>
</column>
</changeSet>
</databaseChangeLog>
顺便说一句,是否可以避免参数 sequence_name 以便休眠可以自己处理?
如果有人可以帮助我,谢谢!