【问题标题】:Hibernate custom type definitionsHibernate 自定义类型定义
【发布时间】:2009-07-28 16:27:13
【问题描述】:

有人可以建议如何在 Hibernate 中定义和映射(使用注释)自定义类型吗?

例如,给定:

@Entity
public class Line {
    private Point startPoint;
    private Point endPoint;
}

public class Point
{
    private double x;
    private double y;
}

与其让 Point 作为对象持久化,我更希望 Line 像这样持久化:

startPointX , startPointY , endPointX , endPointY

这样做的适当方法是什么?

问候

马蒂

【问题讨论】:

    标签: java hibernate persistence


    【解决方案1】:

    自定义类型可能不是处理这个问题的最佳方式,因为Point 是一个多值对象。相反,您可以使用@Embeddable

    @Entity
    public class Line {
    
        @Embedded 
        @AttributeOverrides( {
            @AttributeOverride(name="x", column = @Column(name="startPointX") ),
            @AttributeOverride(name="y", column = @Column(name="startPointY") )
        } )
        private Point startPoint;
    
        @Embedded 
        @AttributeOverrides( {
            @AttributeOverride(name="x", column = @Column(name="endPointX") ),
            @AttributeOverride(name="y", column = @Column(name="endPointY") )
        } )
        private Point endPoint;
    }
    
    @Embeddable
    public class Point
    {
        private double x;
        private double y;
    }
    

    列映射会变得很棘手,因为您需要override them to stop the two points from clashing

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 2012-12-23
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 2015-01-12
      • 2018-10-05
      相关资源
      最近更新 更多