JPA update date timestamp

时间:2019-04-23 15:13:12

标签: java database jpa orm eclipselink

Whenever I insert or update the entity with JPA, I want to set the update date column on database systimestamp value. Note I want to use the time of the database node, not the time of the application server. Is this possible with JPA or EclipseLink annotations?

2 个答案:

答案 0 :(得分:1)

some JPA providers支持这种功能,遗憾的是Eclipselink并不是其中之一。

幸运的是,自定义的EclipseLink属性转换器机制允许您在转换期间访问Session,因此以下解决方法有效:

@Converter(name = "database-timestamp", converterClass = DatabaseTimestampFieldConverter.class)
@Entity
public class AuditedEntity {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    ... 

    @Convert("database-timestamp")
    private Timestamp updatedDate;

    @PreUpdate
    protected void preUpdate() {
        updatedDate = null; // needed to trigger the conversion; if you don't want the extra method here, use @EntityListeners instead
    }
}

其中DatabaseTimestampFieldConverter定义为:

public class DatabaseTimestampFieldConverter implements Converter {

    @Override
    public Object convertObjectValueToDataValue(Object objectValue, Session session) {
        return session.executeQuery(new ValueReadQuery("SELECT CURRENT_TIMESTAMP"));
    }

    @Override
    public Object convertDataValueToObjectValue(Object dataValue, Session session) {
        return dataValue;
    }

    @Override
    public boolean isMutable() {
        return true;
    }

    @Override
    public void initialize(DatabaseMapping mapping, Session session) {

    }

}

或者,您可以尝试在auditing example from the docs之上构建。但是,它使用硬编码的数据库列名而不是字段级注释。

当然,使用数据库提供的机制(例如触发器)可能是一种性能更高的解决方案。

答案 1 :(得分:-1)

您可以使用Public Sub GetRates() 'install https://github.com/VBA-tools/VBA-JSON/blob/master/JsonConverter.bas and add to project 'VBE > Tools > References > Microsoft Scripting Runtime Library Dim json As Object, body As String Dim ws As Worksheet, results(), headers() body = "{""startDate"":""23.03.2019"",""endDate"":""21.04.2019"",""isStateAuth"":""0""}" Set ws = ThisWorkbook.Worksheets("Sheet1") With CreateObject("MSXML2.XMLHTTP") .Open "POST", "http://www.nbrm.mk/services/ExchangeRates.asmx/GetEXRates", False .setRequestHeader "User-Agent", "Mozilla/5.0" .setRequestHeader "Content-Type", "application/json; charset=UTF-8" .setRequestHeader "Referer", "http://www.nbrm.mk/kursna_lista-en.nspx" .setRequestHeader "Content-Length", Len(body) .send body Set json = JsonConverter.ParseJson(.responseText) Dim ratesParent As Object, rates As Object, rate As Object, header As Object Set ratesParent = json("d") Set header = ratesParent.item(1)("ExchangeRates").item(1) ReDim results(1 To 10000, 1 To header.Count) ReDim headers(1 To header.Count) Dim key As Variant, c As Long, r As Long headers = header.keys For Each rates In ratesParent For Each rate In rates("ExchangeRates") 'dictionaries r = r + 1: c = 1 For Each key In rate.keys results(r, c) = rate(key) c = c + 1 Next Next Next With ws .Cells(1, 1).Resize(1, UBound(headers) + 1) = headers .Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results End With End With End Sub 来获取数据库时间