This is a sample class (Entity class) that maps the "tsrange type" of PostgreSQL in Hibernate. There were few articles about the mapping method of "tsrange type", so I wrote it.
SampleEntity.java
package xxx;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import com.vladmihalcea.hibernate.type.range.PostgreSQLRangeType;
import com.vladmihalcea.hibernate.type.range.Range;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Table(name = "xxx_table")
@TypeDefs({
    @TypeDef(
        name = "jsonb",
        typeClass = JsonBinaryType.class
    ),
    @TypeDef(
        typeClass = PostgreSQLRangeType.class,
        defaultForType = Range.class
    )
])
@XmlRootElement
@Data
@NoArgsConstructor
@NamedQueries([
    @NamedQuery(name = "SampleEntity.findById", query = "SELECT s FROM SampleEntity s WHERE s.id = :id")})
public class SampleEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "id")
    private Integer id;
    @Column(name = "tsrange_col", columnDefinition = "tsrange")
    private Range<Date> tsrangeCol;
}
Recommended Posts