检索日期不等于数据库中的持久日期

时间:2016-07-10 13:36:10

标签: java mysql hibernate datetime jpa

我是Java和Hibernate的新手。当我坚持使用Date并从MySQL中检索它时,它在持久化之前不再等于日期“格式”(尽管值似乎相同)。只是好奇 - 我该如何解决这个问题?

这是映射属性:

import javax.persistence.*;
import javax.persistence.Entity;
import java.util.Date;

// ...

@Temporal(TemporalType.TIMESTAMP)
private Date registeredTime;

public Organization(String name) {
    this.name = name;
    this.registeredTime = new Date();
}
// ...

这是测试:

@Test
public void testSaveAndFind() {

    Organization organization = new Organization("Acme Inc");

    long newId = organizationRepository.saveOrganization(organization);

    Assert.assertFalse("Id should not be 0 after saved.", 0L == organization.getId());
    Assert.assertEquals("Entity id and save return id should match.", organization.getId(), newId);

    Organization foundOrganization = organizationRepository.find(newId);

    Assert.assertEquals(
            "Found organization id should be the same as saved organization id.",
            organization.getId(),
            foundOrganization.getId()
    );
    Assert.assertEquals(
            "Found organization name should be the same as saved organization name.",
            organization.getName(),
            foundOrganization.getName()
    );
    Assert.assertEquals(
            "Found organization registered time should be the same as saved organization registered time.",
            organization.getRegisteredTime(),
            foundOrganization.getRegisteredTime()
    );
}

运行测试时出现此故障:Found organization registered time should be the same as saved organization registered time. expected:<Sun Jul 10 15:11:17 CEST 2016> but was:<2016-07-10 15:11:17.0>

This question似乎解决了同样的问题,但答案并没有真正解决问题。我希望有人可以帮忙!

0 个答案:

没有答案