2017-05-06 50 views
0

我有實體的這個例子:如何使用CrudRepository從數據庫中獲取'現在時間'?

@Entity 
public class User { 

    private String name; 
    private Date creationDate; 

    public User(String name) { 
     this.name = name; 
     this.creationDate= Calendar.getInstance().getTime(); 
    } 

} 

和回購的這個例子:

public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> { 

    public User findByName(String name); 

} 

但是,當我需要做的:

User user = new User(); 

我不能從再得到creationDate:

Calendar.getInstance().getTime(); 

因爲有些人正在改變操作系統的時間,並且創建的時間被持續錯誤。所以我需要一些:

SELECT SYSDATE FROM DUAL; 
SELECT now(); 

從用戶設置creationDate。

我需要一個新的回購獲得時間,或者我可以使用此UserRepository從數據庫中獲取creationDate?我試過創建一個UtilsRepository,在那裏我可能會從數據庫中得到日期:

public interface UtilsRepository extends JpaRepository<Date, String> { 

    @Modifying 
    @Transactional(readOnly = false) 
    @Query("select now()") 
    public Date getDataBaseDate(); 

} 

但它不工作。我收到這個錯誤,我認爲這不是最好的方法。發生

Exception in Application init method 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 
Caused by: java.lang.RuntimeException: Exception in Application init method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:912) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'utilsRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.util.Date 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:757) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:841) 
    ... 2 more 
Caused by: java.lang.IllegalArgumentException: Not a managed type: class java.util.Date 
    at org.hibernate.jpa.internal.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:210) 
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68) 
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:67) 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:152) 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:99) 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:266) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252) 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) 
    ... 18 more 
+2

我會說,如果「有人」是允許的,並如果改變生產服務器的時間,你會遇到比創建日期錯誤更大的問題。從數據庫獲取當前tme不會有太大變化,因爲這些人可能也會更改數據庫服務器上的時間。 –

+0

爲什麼該方法被標註爲修改並且只讀爲false? –

+0

@JBNizet,在這種情況下,有必要這樣做。 – laccf

回答

0

我解決了這一點做用

@Query(value = "SELECT SYSDATE FROM DUAL", nativeQuery = true) 

,在UserReposistory:

@Transactional(readOnly = false) 
@Query("select CURRENT_TIMESTAMP from User") 
public Date getCurrentTime(); 
1

Caused by: java.lang.IllegalArgumentException: Not a managed type: class java.util.Date因爲日期是不是一個實體,所以你不能使用你的UtilsRepository原樣。你應該能夠在您的UserRepository

+0

我做了這樣的事情,@Sean。稍後我會發布解決方案。謝謝。 – laccf

相關問題