我依赖于hibernate模式生成作为工作流的一部分,因为每次提交对模型的更改都相当大。我使用生成的模式来完成用于测试环境的脚本。
正在使用的数据库是PostgreSQL。我在其中一个列中使用自定义postgres类型INET
(我持久化IPv4和IPv6值。我需要它提供的类型检查。)
为此,我使用自定义hibernate UserType
(InetAddressType.java
。)
在实体中,我使用hibernate特定的TypeDef
来注册自定义UserType
,并使用Type
中的Lamp.java
注释ip值列。
@Entity
@TypeDef(name = "inet", typeClass = InetAddressType.class)
public class Lamp {
@Type(type = "inet")
public InetAddress getIpAddress() {
return ipAddress;
}
自定义用户类型适用于从外部hibernate生成的模式。但是,当涉及到自动生成时,hibernate决定正确的类型是UUID
:
create table lamp (id int8 not null, ip_address uuid, primary key (id))
有没有办法指示hibernate使用INET而不是UUID?
Hibernate版本是5.2.17.Final
。
只需使用@Column
注释和columnDefinition
参数,例如:
@Column(columnDefinition = "inet")