entityMananger 不使用 Date 参数
entityMananger not working with Date parameter
在 Hibernate 中使用 EntityManager 方法。每当使用日期字段查询时,我都希望它查找具有完整日期和时间的日期,但它只使用日期而不是时间。
where cou0_.expiry_date>'22-Jan-16'
spring 配置
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_caches}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
</props>
</property>
</bean>
道具
hibernate.hbm2ddl.auto = update
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
hibernate.c3p0.min_size = 5
hibernate.c3p0.max_size = 10
hibernate.c3p0.timeout = 300
hibernate.c3p0.max_statements = 50
hibernate.c3p0.idle_test_period = 3000
hibernate.generate_statistics = true
hibernate.show_sql=true
hibernate.use_sql_comments=true
列声明为
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "expiry_date")
private Date expiryDate;
查询码
Query query = getEntityManager().createQuery("from **** where c.expiryDate > :expiryDate");
Date now = new Date();
now.setHours(8);
System.out.println("Date--"+now);
query.setParameter("expiryDate", now,TemporalType.TIMESTAMP);
List<****> entity = query.getResultList();
问题不在于实体管理器。我用间谍来跟踪查询。间谍有可配置的 属性 以格式显示日期。我没有配置它,所以它想使用默认格式,即 dd-MMM-yyyy
在 Hibernate 中使用 EntityManager 方法。每当使用日期字段查询时,我都希望它查找具有完整日期和时间的日期,但它只使用日期而不是时间。
where cou0_.expiry_date>'22-Jan-16'
spring 配置
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_caches}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
</props>
</property>
</bean>
道具
hibernate.hbm2ddl.auto = update
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
hibernate.c3p0.min_size = 5
hibernate.c3p0.max_size = 10
hibernate.c3p0.timeout = 300
hibernate.c3p0.max_statements = 50
hibernate.c3p0.idle_test_period = 3000
hibernate.generate_statistics = true
hibernate.show_sql=true
hibernate.use_sql_comments=true
列声明为
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "expiry_date")
private Date expiryDate;
查询码
Query query = getEntityManager().createQuery("from **** where c.expiryDate > :expiryDate");
Date now = new Date();
now.setHours(8);
System.out.println("Date--"+now);
query.setParameter("expiryDate", now,TemporalType.TIMESTAMP);
List<****> entity = query.getResultList();
问题不在于实体管理器。我用间谍来跟踪查询。间谍有可配置的 属性 以格式显示日期。我没有配置它,所以它想使用默认格式,即 dd-MMM-yyyy