简体   繁体   English

无法使用JPA保留哈希图

[英]Cannot persist hashmap with JPA

I am struggling trying to persist a map to SQLserver with the code below and keep getting the following error: 我正在努力尝试使用以下代码将映射持久化到SQLserver,并不断出现以下错误:

* Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'OnlineReport_availabilities'. *由以下原因引起:com.microsoft.sqlserver.jdbc.SQLServerException:无效的对象名称“ OnlineReport_availabilities”。 * *

I use Play framework JPA for this have have managed to persist Maps before with similar code. 我使用Play框架JPA,为此已经成功使用类似的代码保存了Maps。 I tried manually creating the table in the db to see if there was any issue with the name but all seems ok. 我尝试在数据库中手动创建表,以查看名称是否存在任何问题,但是一切正常。

I am doing something non conventional or incorrect here? 我在这里做非常规或不正确的事情?


   @Entity
    public class OnlineReport extends Model {

    @Temporal(TemporalType.DATE)
    public Date date;

    @ElementCollection
    public Map<Date, Double> availabilities;

    public OnlineReport(){
        this.date = new Date();
        this.availabilities = new HashMap<Date, Double>();         
    }

    public void addAvailability(double availability){
        TreeSet<Date> set = new TreeSet(availabilities.entrySet());

        Date lastEntry = null;
        if(!set.isEmpty())
            lastEntry = set.last();

        Date now = new Date();
        if(lastEntry != null){
            //Add availibility every 10mn
            if(DateUtil.getMinutesBetween(lastEntry, now) >= 10){
                availabilities.put(now, availability);
                save();
            }
        } else {
            availabilities.put(now, availability);
            save();
        }
    }
}


Update. 更新。

Running JPA in debugSQL I have noticed the following errors: 在debugSQL中运行JPA我注意到以下错误:

ERROR ~ Unsuccessful: create table OnlineReport_availabilities (OnlineReport_id numeric(19,0) not null, availabilities double precision null, availabilities_KEY datetime null, primary key (OnlineReport_id, availabilities_KEY)) 错误〜不成功:创建表OnlineReport_availabilities(OnlineReport_id数字(19,0)不为null,可用性为双精度null,availability_KEY datetime为null,主键(OnlineReport_id,availabilities_KEY))

ERROR ~ Cannot define PRIMARY KEY constraint on nullable column in table 'OnlineReport_availabilities'. 错误〜无法在表'OnlineReport_availabilities'中的可为空的列上定义PRIMARY KEY约束。

I am under the impression I am missing some annotation definition of 'availabilities'. 我的印象是我缺少“可用性”的一些注释定义。

Likely you do not have such a table or it is not accessible via connection in use. 您可能没有这样的表,或者无法通过使用中的连接对其进行访问。 Are object (tables, fields etc) names case sensitive in db? 对象(表,字段等)名称在db中是否区分大小写?

So, after your update we know why you do not have table. 因此,更新后,我们知道为什么没有表格。 Looks like it fails to create not null -constraint to the column that is part of the key. 看起来它无法创建不是null的约束到作为键一部分的列。 Dialect fails for SQL Server, because your mapping is minimal, but correct. 对于SQL Server,方言失败,因为您的映射很小,但是正确。 You can try to force it by adding following annotation: 您可以尝试通过添加以下注释来强制执行此操作:

@MapKeyColumn(columnDefinition = "datetime not null")

Of course having datetime as part of the key will produce problems if it is possible that there is more than one entry per millisecond. 当然,如果有可能每毫秒有一个以上的条目,那么将日期时间作为键的一部分会产生问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM