2016-11-15 87 views
0

我創建自定義查詢獲取發票數據日期,但它返回null。我確定我爲數據庫中存在的查詢設置了完全相同的日期。彈簧數據JPA - 帶「@Param Date」的自定義@Query不起作用

我使用自定義查詢,因爲我想要更先進的查詢來編寫。但是這個簡單的查詢存在問題。下面是我的示例代碼:

@Query("select i from Invoice i where " + 
     "i.expirationDate = :expDate") 
Invoice findCompanyInvoiceByDate(@Param("expDate") Date expDate); 

我想這個代碼,但它不也行:

Invoice findByExpirationDate(Date expirationDate); 

我也試過日期和@Param之前添加@Temporal(TemporalType.DATE),但結果是零。

+0

你的日期還涉及時間,小時,分鐘,秒,毫秒等?你確定這些值是完全相同的嗎? – pleft

+0

您可以添加數據庫中的日期格式的詳細信息以及java端的dat eformat expDate嗎? – developer

+0

在MySQL數據庫中,我將字段設置爲「datetime」。當我從數據庫中檢索它時,它們的值完全相同。 –

回答

1

您應該在日期欄中使用@Temporal(TemporalType.TIMESTAMP)。如果仍然不夠(仍然返回null),則還要在@Column註釋中添加columnDefinition

整個工作示例is here注意so-40613171分支。對不起,存儲庫名稱奇怪,類命名,它使用了很多案例研究)。粗例如:

Employee.java

@Entity 
public class Employee { 

    @Id 
    private Integer id; 
    private String name; 
    private String surname; 

    @Temporal(TemporalType.TIMESTAMP) 
    @Column(name = "birth_date", columnDefinition = "DATETIME") 
    private Date birthDate; 

    // Other fields, getter setter, etc. 
} 

EmployeeRepository.java

public interface EmployeeRepository 
extends JpaRepository<Employee, Integer> { 

    @Query("from Employee e where e.birthDate = :birthDate") 
    List<Employee> findEmployeeDataByBirthDate(@Param("birthDate") Date birthDate); 
} 

示例數據

final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
Company companySun = companyRepository.save(new Company(42, "Sun microsystems")); 
Company companyGoogle = companyRepository.save(new Company(43, "Google")); 

employeeRepository.save(new Employee(101, "James", "Gosling", dateFormat.parse("1970-01-01 17:05:05"), companySun)); 
employeeRepository.save(new Employee(102, "Paul", "Sheridan", dateFormat.parse("1970-01-01 17:05:05"), companySun)); 
employeeRepository.save(new Employee(103, "Patrick", "Naughton", dateFormat.parse("1970-01-01 17:05:05"), companySun)); 

employeeRepository.save(new Employee(201, "Lary", "Page", dateFormat.parse("1970-01-01 17:01:05"), companyGoogle)); 
employeeRepository.save(new Employee(202, "Sergey", "Brin", dateFormat.parse("1970-01-02 17:02:05"), companyGoogle)); 

測試代碼片段

@Test 
public void employeService_findByBirthDate() throws ParseException { 
    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    List<Employee> result = this.employeeService.findByBirthDate(dateFormat.parse("1970-01-01 17:05:05")); 

    Assert.assertEquals(3, result.size()); 
} 

如果你運行它,則通過測試。

HTH

+1

這個解決方案對我不起作用,我不知道爲什麼,當然,這是非常有用的例子,應該考慮到,謝謝。 。 –

0

我發現是這個問題here有用的答案。

如果您使用「日期」類型,我認爲問題的代碼應該可以工作,但對於「日期時間」,您應該在查詢中使用「之間」或類似的東西。我發現,對我的作品的解決方案,這是代碼:

@Query("select i from Invoice i where " + 
     "i.expirationDate >= :todayMidnight and i.expirationDate < :tomorowMidnight " + 
     "order by expirationDate DESC") 
List<Invoice> findCompanyInvoiceByDate(@Param("todayMidnight") Date todayMidnight, @Param("tomorowMidnight") Date tomorowMidnight); 
0

我只是用日期作爲字符串和使用nativeQuery = true。它對我很好。

@Query(value ="select * from table st where st.created_date >= ?1", nativeQuery = true) 
    List<YourObject> findAllByCreatedDate(@Param("createdDate") @DateTimeFormat(iso = ISO.DATE) String createdDate);