我使用hibernate與春天,h2和liquibase,我試圖通過以這個blog post爲例來爲我的實體做一個自定義的字符串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>
btw是否有可能避免參數sequence_name,以便hibernate可以自己處理它?
如果有人能幫助我,謝謝!
類似的討論發生在:http://stackoverflow.com/a/15470272/2128503 – Mamun