代码之家  ›  专栏  ›  技术社区  ›  XtremeBaumer

休眠值分为两列。如何绘制地图?

  •  0
  • XtremeBaumer  · 技术社区  · 6 年前

    我有一个遗留数据库,我想保持原样,但这会导致hibernate出现一些问题。

    我现在的问题是,我有这个领域 private Calendar myTradeDateTime; 在我的POJO中,它被映射到2列。

    第一列仅包含日期信息(例如“2013-05-14”),第二列仅包含时间信息(例如“10:09:12 PM”)。现在我想使用hibernate来访问这两个列中的1字段。我想我需要一些 @Converter 但我不知道该怎么做,我也找不到关于这个主题的任何信息(两列一个字段)。

    3 回复  |  直到 6 年前
        1
  •  1
  •   B_St    6 年前

    当您使用hibernate本机API和hbm.xml版它仍然是这样工作的:

        <property name="dateMitUltimo" type="org.UTDateUltimo">
            <column name="DAT_ULTIMO" />
            <column name="SL_ULTIMO" />
        </property>
    

    使用这样的用户类型:

    public class UTDateUltimo
        implements CompositeUserType {
    

    使用hibernate的JPA-API,这不再有效:-(

    <entity class="de.parcit.base.db.jpa.TestEmbedded" name="TestEmbedded">
        <table name="TEMBED" />
    
        <attributes>
            <!-- domain="ID" type="long" -->
            <id name="id" type="long">
                <!-- <generated-value strategy="TABLE" /> -->
            </id>
    
            <embedded name="dateMitUltimo" />
    
        </attributes>
    </entity>
    
    <embeddable class="de.parcit.base.db.jpa.DateJPA" access="FIELD">
    
        <convert converter="de.parcit.base.db.jpa.DateConverter" attribute-name="date" />
    
        <attributes>
    
            <!-- domain="Datum_Ultimo" -->
            <basic name="date">
                <column name="DAT_ULTIMO" />
            </basic>
    
            <!-- domain="SL_SI" -->
            <basic name="month">
                <column name="SL_ULTIMO" />
            </basic>
        </attributes>
    </embeddable>
    

    public class DateConverter
        implements AttributeConverter<Date, java.sql.Date> {
    

    更新:

    “embedded”也可以与注释一起使用。 Java - JPA @Basic and @Embedded annotations

        2
  •  1
  •   Thorben Janssen    6 年前

    this article JPA和Hibernate将每个实体属性映射到数据库列。但是您可以使用一个小的解决方案来实现请求的映射。

    首先需要2个实体属性来映射2个数据库列。如果使用基于字段的访问(注释属性而不是setter),则可以跳过这些属性的getter和setter方法。然后,您可以添加第三个属性,即您将在业务代码中使用的属性。你需要用 @Transient 并根据其他2个属性计算其值。

    下面是一个映射,它使用这种方法将时间(postedAtTime)和日期(posteDate)列映射到 LocalDateTime postedAt 属性。

    @Entity
    public class Review {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        private String comment;
    
        private LocalDate postedAtDate;
    
        private LocalTime postedAtTime;
    
        @Transient
        private LocalDateTime postedAt;
    
        public Long getId() {
            return id;
        }
    
        public String getComment() {
            return comment;
        }
    
        public void setComment(String comment) {
            this.comment = comment;
        }
    
        public LocalDateTime getPostedAt() {
            if (postedAt == null) {
                this.postedAt = LocalDateTime.of(this.postedAtDate, this.postedAtTime);
            }
            return postedAt;
        }
    
        public void setPostedAt(LocalDateTime postedAt) {
            this.postedAt = postedAt;
            this.postedAtDate = postedAt.toLocalDate();
            this.postedAtTime = postedAt.toLocalTime();
        }
    }
    

    请注意,您需要使用 postedAtDate postedAtTime Review 实体对象,你不需要知道这两个内部属性。

        3
  •  0
  •   Daniel Chaves    6 年前

    private LocalDate tradeDate;
    private LocalTime tradeTime;
    

    public Calendar getMyTradeDateTime(){
        LocalDateTime dateTime = LocalDateTime.of(this.tradeDate, this.tradeTime);
        Calendar c = Calendar.getInstance();
        return c.setTimeInMillis(dateTime.toEpochSecond(ZoneOffset.of("-03:00"))*1000);
    }
    

    PS:设置虚拟机时区的zoneoffset。可以通过编程方式获取(?)。

    public void setMyTradeDateTime(Calendar c) {
       LocalDateTime dateTime = LocalDateTime.ofInstant(c.toInstant(), ZoneId.systemDefault());
        this.tradeDate = dateTime.toLocalDate();
        this.tradeTime = dateTime.toLocalTime();
    }
    
        4
  •  -1
  •   David Buck Richard Ferris    5 年前
    @Entity
    public class Review {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        private String comment;
    
        private LocalDate postedAtDate;
    
        private LocalTime postedAtTime;
    
        @Transient
        private LocalDateTime postedAt;
    
        public Long getId() {
            return id;
        }
    
        public String getComment() {
            return comment;
        }
    
        public void setComment(String comment) {
            this.comment = comment;
        }
    
        public LocalDateTime getPostedAt() {
            if (postedAt == null) {
                this.postedAt = LocalDateTime.of(this.postedAtDate, this.postedAtTime);
            }
            return postedAt;
        }
    
        public void setPostedAt(LocalDateTime postedAt) {
            this.postedAt = postedAt;
            this.postedAtDate = postedAt.toLocalDate();
            this.postedAtTime = postedAt.toLocalTime();
        }
    }