Java 休眠值拆分为2列。如何绘制地图?
我有一个遗留数据库,我想保留它,但这会导致hibernate出现一些问题 我当前的问题是,我有一个字段Java 休眠值拆分为2列。如何绘制地图?,java,hibernate,mapping,hibernate-5.x,Java,Hibernate,Mapping,Hibernate 5.x,我有一个遗留数据库,我想保留它,但这会导致hibernate出现一些问题 我当前的问题是,我有一个字段private Calendar myTradeDateTime在我的POJO中,这被映射到2列 第一列仅保存日期信息(如“2013-05-14”),第二列仅保存时间信息(如“10:09:12 PM”)。现在,我想使用hibernate访问两个列,只使用1个字段。我想我需要一些@Converter来处理它,但我不知道该怎么做,也找不到关于这个主题的任何信息(一个字段中有两列) 所以我的问题是,在
private Calendar myTradeDateTime代码>在我的POJO中,这被映射到2列
第一列仅保存日期信息(如“2013-05-14”),第二列仅保存时间信息(如“10:09:12 PM”)。现在,我想使用hibernate访问两个列,只使用1个字段。我想我需要一些@Converter
来处理它,但我不知道该怎么做,也找不到关于这个主题的任何信息(一个字段中有两列)
所以我的问题是,在hibernate 5.3中如何将2列映射到1个字段?我认为在hibernate中没有这样的方法。我建议映射这两个列,并创建一个getter和setter,如下所示(除了您已经创建的getter和setter之外):
还有一个get/set方法,如:
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:设置VM时区的zoneoffset。可以通过编程方式获取它(?)
当您使用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,这将不再有效:-(
相反,您可以尝试嵌入功能。在xml中,这类似于:
<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>
正如我在中所解释的,JPA和Hibernate将每个实体属性映射到数据库列。但是您可以使用一个小的解决方法来实现请求的映射
首先需要2个实体属性来映射2个数据库列。如果使用基于字段的访问(对属性进行注释,而不是对setter进行注释),您可以跳过这些属性的getter和setter方法。然后,您可以添加第三个属性,即您将在业务代码中使用的属性。您需要使用@Transient
对其进行注释,并根据其他两个属性计算其值
@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();
}
}
下面是一个映射,它使用这种方法将时间(postedAtTime)和日期(postedAtDate)列映射到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();
}
}
请注意,您需要在JPQL和条件查询中使用postedAtDate
和postedAtTime
属性。但只要您使用Review
entity对象,您就不需要知道这两个内部属性。回答旧问题时,您的答案将对您更有用其他StackOverflow用户,如果您包含一些上下文来解释您的答案如何有帮助。请参阅:。
@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();
}
}
@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();
}
}
@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();
}
}