0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Hibernateでtsrange型のマッピング方法

Last updated at Posted at 2020-12-24

Hibernateで、PostgreSQLの「tsrange型」をマッピングするサンプルクラス(Entityクラス)です。
「tsrange型」のマッピング方法についての記事が少なかったので、記載してみました。

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;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?