草庐IT

java - 在 MySQL 中存储为 DATE 的 LocalDate 返回不同的结果

coder 2023-10-24 原文

我有一个 Java 类,其字段定义为:

  @Column
  @NotNull
  private LocalDate availabilityDate;

它映射到定义为的列:

availability_date DATE NOT NULL

当我比较通过 Spring Data 保存到 MySQL 数据库之前和之后的日期时,我在 JUnit 中得到不同的结果:

Expecting:
  <[InventoryAvailability(size=000, availabilityDate=2018-02-07, inventoryContextId=3847, quantity=15, lastChange=2018-02-08T14:32:24.770+01:00[Europe/Berlin]),
    InventoryAvailability(size=001, availabilityDate=2018-02-07, inventoryContextId=3847, quantity=15, lastChange=2018-02-08T14:32:26.337+01:00[Europe/Berlin])]>
to contain exactly in any order:
  <[InventoryAvailability(size=001, availabilityDate=2018-02-08, inventoryContextId=3847, quantity=15, lastChange=2018-02-08T14:32:26.337+01:00[Europe/Berlin]),
    InventoryAvailability(size=000, availabilityDate=2018-02-08, inventoryContextId=3847, quantity=15, lastChange=2018-02-08T14:32:24.770+01:00[Europe/Berlin])]>

这怎么可能?我以为只有 TIMESTAMPS 有时区。我正在考虑将其存储为毫秒 LONG 或作为 UTC ZonedDateTime 作为解决方法,但我怀疑我不明白这里的一些重要内容。

最佳答案

我的回答是基于假设您正在使用 java.time.LocalDate 作为您的字段 availabilityDate。我编写了简单的 Spring Boot 应用程序,它使用 Spring Data 和 MySQL 连接器(数据库版本为 5.7.20-log)来重现您的案例。

测试 DateHolderRepositoryTest 失败,因为它不知道如何将 LocalDate 转换为数据库中的 Date。然后我添加了对 hibernate-java8 的依赖,测试变成了绿色(如 https://www.thoughts-on-java.org/hibernate-5-date-and-time/ 中所述)。

所以我的假设是你的 JUnit 测试有问题,或者它可能与你实体的 equals 和 hashCode 方法实现有关(因为 Hamcrest 将在下面使用 equals 来比较类,并且从数据库类加载和创建的类是只是不同的实例,默认的 Object equals 方法将返回 false)。您问题中的 availabilityDate=2018-02-08 显示没有时区的日期。

为了重现我的结果,我添加了测试应用程序的代码。我这是我的 DateHolder 类,它包含 id 和 availabilityDate,并包含基于字段 id 的 hashCode 和 equals 实现:

package hello;

import java.time.LocalDate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;

@Entity
public class DateHolder {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    @Column
    @NotNull
    private LocalDate availabilityDate;

    protected DateHolder() {}

    public DateHolder(LocalDate availabilityDate)
    {
        this.availabilityDate = availabilityDate;
    }

    @Override
    public String toString()
    {
        return String.format("DateHolder[id=%d, availabilityDate='%s']", id, availabilityDate);
    }

    public Integer getId()
    {
        return id;
    }

    public LocalDate getAvailabilityDate()
    {
        return availabilityDate;
    }

    @Override
    public int hashCode()
    {
        return id;
    }

    @Override
    public boolean equals(Object other)
    {
        if (other instanceof DateHolder)
        {
            return this.id == ((DateHolder)other).id;
        }
        else
        {
            return false;
        }
    }
}

这是我的 JPA 存储库:

package hello;
import org.springframework.data.repository.CrudRepository;

public interface DateHolderRepository extends CrudRepository<DateHolder, Long>
{
}

这是主要的 Spring Boot 应用程序类:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class);
    }
}

这是测试,它创建了 2 个 DateHolder,将它们保存在数据库中,并将它们与数据库中的任何内容进行比较(它不考虑顺序)。它还测试从数据库加载的第一个实体的 toString() 方法的返回值。

package hello;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;

import java.time.LocalDate;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class DateHolderRepositoryTest
{
    @Autowired
    private DateHolderRepository repository;

    @Test
    public void testFindByLastName()
    {
        DateHolder dateHolder1 = new DateHolder(LocalDate.parse("2018-02-07"));
        DateHolder dateHolder2 = new DateHolder(LocalDate.parse("2018-02-08"));

        repository.save(dateHolder1);
        repository.save(dateHolder2);

        Iterable<DateHolder> dateHolders = repository.findAll();
        assertThat(dateHolders, containsInAnyOrder(dateHolder2, dateHolder1));

        assertThat(dateHolders.iterator().next().toString(), is("DateHolder[id=1, availabilityDate='2018-02-07']"));
    }
}

关于java - 在 MySQL 中存储为 DATE 的 LocalDate 返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48686911/

有关java - 在 MySQL 中存储为 DATE 的 LocalDate 返回不同的结果的更多相关文章

  1. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  2. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  3. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  4. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  5. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  6. ruby - Ruby 中的隐式返回值是怎么回事? - 2

    所以我开始关注ruby​​,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出

  7. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  8. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  9. ruby-on-rails - ruby 日期方程不返回预期的真值 - 2

    为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998

  10. ruby - 从 String#split 返回的零长度字符串 - 2

    在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"

随机推荐