如何在JPA中填充@Transient字段? [英] How to populate @Transient field in JPA?

查看:579
本文介绍了如何在JPA中填充@Transient字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

daysCalendar类中的瞬态变量. CalendarDay类通过名为calendarId的字段具有对Calendar类的外键引用.

当我获取Calendar对象时,days字段保持为空.如何填充字段?

@Entity
@Table(name = "calendars")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "calendars")
public class Calendar extends Node {

    @Transient
    @OneToMany(fetch = FetchType.EAGER)
    @CollectionTable(name = "calendar_days", joinColumns = @JoinColumn(name = "calendarid"))
    protected List<CalendarDay> days;
    @JsonProperty("Days")
    public List<CalendarDay> getDays() {
       return days;
    }

    @JsonProperty("Days")
    public void setDays(List<CalendarDay> days) {
        this.days = days;
    }
}

@Entity
@Table(name = "calendar_days")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "calendar_days")
public class CalendarDay extends General {

    @ManyToOne
    @JoinColumn(name = "calendarid")
    protected Calendar calendarId;

}

天数"为空. Calendar类中的days变量是瞬态变量.如何在JPA中填充此瞬态变量?

以下是响应:

{"calendars": {
    "rows": [
        {
            "parentId": null,
            "phantomParentId": null,
            "children": null,
            "expanded": false,
            "index": null,
            "leaf": true,
            "Id": 1,
            "Name": "General",
            "DaysPerMonth": 20,
            "DaysPerWeek": 5,
            "HoursPerDay": 8,
            "WeekendsAreWorkdays": false,
            "WeekendFirstDay": 6,
            "WeekendSecondDay": 0,
            "DefaultAvailability": [
                "08:00-12:00",
                "13:00-17:00"
            ],
            **"Days": null**
        }
        ]
        }
}

解决方案

由于@Transient注释days未被保留.因此,从数据库中获取时,它将保留为null.

临时变量在序列化时会被忽略-java关键字transient-或持久存在-JPA注释@Transient.

例如,这些值可以保存从其他变量计算得出或以其他方式导出的数据.在这种情况下,该值只计算一次并存储在变量中,这样就不需要再次计算它,除非某些通过计算得出的值发生了变化.

因此,如果我们有充分的理由假设它是@Transient,则应该在获取自己后以某种方式计算days的值.如果假设您想基于JSON进行操作,则应该(例如)创建一个方法,该方法从JSON获取必填字段,并根据这些值填充days.

如果JSON是序列化类的结果,则无论如何仍需要计算days,仍然假设它应该为@Transient.

但是:由于第一个评论者指出您拥有的JPA注释组合并不健全.也许您想删除@Transient批注,以便从JSON计算一次值后,便可以将它们持久保存到db中. 查看更多信息:

为什么JPA有@Transient注释?

上面还介绍了注释@Transient和关键字transient之间的区别,并且由于您处理JSON,因此阅读以下内容可能会很好:

JPA瞬态注释和JSON

days is a transient variable in Calendar class. CalendarDay class has foreign key reference to Calendar class via the field called calendarId.

When I fetch Calendar object, days field remains null. How can I populate the field?

@Entity
@Table(name = "calendars")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "calendars")
public class Calendar extends Node {

    @Transient
    @OneToMany(fetch = FetchType.EAGER)
    @CollectionTable(name = "calendar_days", joinColumns = @JoinColumn(name = "calendarid"))
    protected List<CalendarDay> days;
    @JsonProperty("Days")
    public List<CalendarDay> getDays() {
       return days;
    }

    @JsonProperty("Days")
    public void setDays(List<CalendarDay> days) {
        this.days = days;
    }
}

@Entity
@Table(name = "calendar_days")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "calendar_days")
public class CalendarDay extends General {

    @ManyToOne
    @JoinColumn(name = "calendarid")
    protected Calendar calendarId;

}

Here Days is null. days variable in Calendar class is transient variable. How can I populate this transient variable in JPA?

Following is the response:

{"calendars": {
    "rows": [
        {
            "parentId": null,
            "phantomParentId": null,
            "children": null,
            "expanded": false,
            "index": null,
            "leaf": true,
            "Id": 1,
            "Name": "General",
            "DaysPerMonth": 20,
            "DaysPerWeek": 5,
            "HoursPerDay": 8,
            "WeekendsAreWorkdays": false,
            "WeekendFirstDay": 6,
            "WeekendSecondDay": 0,
            "DefaultAvailability": [
                "08:00-12:00",
                "13:00-17:00"
            ],
            **"Days": null**
        }
        ]
        }
}

解决方案

Because of @Transient annotation days are not persisted. When fetching from database it will therefore remain null.

Transient variables are such that are ignored when serializing - java keyword transient - or persisted - JPA annotation @Transient.

These values can - just as an example - hold data that is calculated or other way derived from other variables. In such case the value is calculated once and stored in the variable so that it is not needed to be calculated again unless some of the values that it has been calculated from changes.

So if we assume that it is @Transient for a good reason you should somehow calculate the value of days after fetch yourself. If assumed that you want to do it based on the JSON you should then - for example - create a method that gets the required fields from JSON and populates days based on those values.

If the JSON is the result of serializing class you need to anyway calculate days somehow still assuming it should be @Transient.

BUT: as the first commentator states the JPA annotation combination you have is not sane. Maybe you want to remove the @Transient annotation, so that when you calculate the values once from JSON you then persist them to db. See more information:

Why does JPA have a @Transient annotation?

above also about the difference between annotation @Transient and keyword transient and because you deal with JSON also this might be good to read:

JPA Transient Annotation and JSON

这篇关于如何在JPA中填充@Transient字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆