编辑:关于此问题的第二和第四个答案提供了解决此问题的解决方案有关Spring-Data-JPA注释的setMaxResults?
目标:使用Spring Data JPA存储库和Spring Query注释,按属性z获取最大/最小元素.
到目前为止我有什么
@Query("SELECT xelement FROM x xelement ORDER BY xelement.z")
public List<X> findFirstElement();
Run Code Online (Sandbox Code Playgroud)
问题:此查询获取所有元素(这不是真正有效).如果我直接使用EntityManager,我可以使用设置结果的数量
entityManager.setMaxResults(1)
Run Code Online (Sandbox Code Playgroud)
只获得第一个元素.
问题:如何使用@Query注释指定最大结果数?
想法:使用大小为0的PageRequest还是要走的路?
约束:我知道"FindFirstBy ...."查询功能,但我想/必须使用@Query注释.
在JodaTime中有没有办法构造一个Date/DateTime,它总是比任何其他Date/DateTime更小/更大?有点像
DateTime bigBang = DateTime.xxx();
DateTime endOfUniverse = DateTime.yyy();
Run Code Online (Sandbox Code Playgroud)
约束:我不想使用标准的Java Date库.
我试图在visual studio中打开一个解决方案并收到以下错误消息:"Microsoft.VisualStudio.Editor.Implementation.EditorPackage"包未正确加载".它还告诉我查看日志文件...这里是日志文件(活动监视器日志)中的(希望)相关错误条目:
...
194错误提供的类型必须是枚举.参数名称:enumType C:\ Users\uhofstetter\AppData\Local\Microsoft\VisualStudio\11.0\ComponentModelCache\Microsoft.VisualStudio.Default.cache Microsoft.VisualStudio.CommonIDE.ExtensibilityHosting.VsShellComponentModelHost 2015/11/25 17:48:41.548 195错误从缓存文件加载程序集缓存时发生错误:C:\ Users\uhofstetter\AppData\Local\Microsoft\VisualStudio\11.0\ComponentModelCache\Microsoft.VisualStudio.Default.cache Microsoft.VisualStudio.CommonIDE.ExtensibilityHosting.VsShellComponentModelHost 2015/11/25 17:48:41.549 196 ERROR提供的类型必须是Enum.参数名称:enumType C:\ PROGRAM FILES(X86)\ MICROSOFT VISUAL STUDIO 11.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\EDITOR\Microsoft.VisualStudio.Platform.VSEditor.dll Microsoft.VisualStudio.CommonIDE.ExtensibilityHosting.VsShellComponentModelHost 2015/11/25 17:48:41.552 197警告临时关闭应用程序当前会话的此程序集的缓存C:\ PROGRAM FILES(X86)\ MICROSOFT VISUAL STUDIO 11.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\EDITOR\Microsoft.VisualStudio. Platform.VSEditor.dll Microsoft.VisualStudio.CommonIDE.ExtensibilityHosting.VsShellComponentModelHost 2015/11/25 17:48:41.553
...
235 ERROR SetSite程序包失败[Microsoft.VisualStudio.Editor.Implementation.EditorPackage] {E269B994-EF71-4CE0-8BCD-581C217372E8} 80131500 VisualStudio 2015/11/25 17:49:20.531 236错误结束程序包加载[Microsoft.VisualStudio. Editor.Implementation.EditorPackage] {E269B994-EF71-4CE0-8BCD-581C217372E8} 80131500 VisualStudio 2015/11/25 17:49:20.551
有谁知道这里出了什么问题?
顺便说一句:我使用的是Visual Studio Professional 2012
我原本期望ftp同步机制更新已更改的文件.但是,从我在这里看到的,只有在文件尚不存在的情况下才会下载该文件.就目前而言,即使时间戳/内容已更改,也不会在本地保存文件.
所以这是我到目前为止所发现的:
类org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer
@Override
public void synchronizeToLocalDirectory(final File localDirectory) {
final String remoteDirectory = this.remoteDirectoryExpression.getValue(this.evaluationContext, String.class);
try {
int transferred = this.remoteFileTemplate.execute(new SessionCallback<F, Integer>() {
@Override
public Integer doInSession(Session<F> session) throws IOException {
F[] files = session.list(remoteDirectory);
if (!ObjectUtils.isEmpty(files)) {
List<F> filteredFiles = filterFiles(files);
for (F file : filteredFiles) {
try {
if (file != null) {
copyFileToLocalDirectory(
remoteDirectory, file, localDirectory,
session);
}
}
catch (RuntimeException e) {
if (AbstractInboundFileSynchronizer.this.filter instanceof ReversibleFileListFilter) {
((ReversibleFileListFilter<F>) AbstractInboundFileSynchronizer.this.filter)
.rollback(file, filteredFiles);
}
throw …Run Code Online (Sandbox Code Playgroud) 如何将Kotlin中的列表分成两个子列表?目前我这样做:
val (first, rest) = listOf("one", "two", "three")
Run Code Online (Sandbox Code Playgroud)
但这样做,首先是"一个",休息是"两个".我希望他们成为first =["first"]和rest = ["two", "three"].
这甚至可以使用这种"析构函数"语法吗?
我正在寻找一个基于Java的数据结构,它管理基于收集时间/日期的间隔(最好是Joda时间),这样对于添加到集合的每个间隔,数据结构返回添加间隔的子间隔,还没有在数据结构中并巩固间隔.
现在就集合理论而言,这将是容易的,即返回值将是"待添加"\"现有的",并且所得到的结构将是"待加"的"现有"联合.当然,我可以使用一组离散点来模拟日期/时间间隔,但这似乎并不真正有效.
所以我正在寻找一个现有的数据结构,它已经使用间隔提供了开箱即用的这些设置操作.
只是为了澄清,这里是我正在寻找的例证.
Run Code Online (Sandbox Code Playgroud)// case 0 // existing ********************************************* // to be added ******** // return value --empty-- // result ********************************************* // // case 1 // existing ***************************************** // to be added ************ // return value **** // result ********************************************* // // case 2 // existing *************************************** // to be added **** // return value **** // result **** *************************************** // // case 3 // existing ***************************************** // to be added …
我有以下SQL脚本(initDB.sql)
CREATE TABLE FFShareHistorical ( ID int NOT NULL AUTO_INCREMENT,
PX_LAST Double DEFAULT NULL,
PX_OPEN Double DEFAULT NULL,
PX_HIGH Double DEFAULT NULL,
PX_LOW Double DEFAULT NULL,
PRIMARY KEY (ID))
Run Code Online (Sandbox Code Playgroud)
并希望使用Spring的ScriptUtils(4.1.4.RELEASE)执行它,即
Resource rc = new ClassPathResource("initDB.sql");
ScriptUtils.executeSqlScript(dataSource.getConnection(), rc);
Run Code Online (Sandbox Code Playgroud)
问题是看起来断行被解释为SQL语句的终止,即错误堆栈跟踪看起来像这样
org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource [initDB.sql]: CREATE TABLE FFShareHistorical ( ID int NOT NULL AUTO_INCREMENT, ; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE FFSHAREHISTORICAL ( ID INT NOT …Run Code Online (Sandbox Code Playgroud) 定义一个名为myfunction的函数有什么区别
"myfunction" <- function(<arguments>){<body>}
Run Code Online (Sandbox Code Playgroud)
和
myfunction <- function(<arguments>){<body>}
Run Code Online (Sandbox Code Playgroud)
此外:通常围绕这样一个功能的评论是什么,即
#myfunction{{{
"myfunction" <- function(<arguments>){<body>}
#}}}
Run Code Online (Sandbox Code Playgroud)
他们只是为了文件还是真的有必要(如果是这样的话)?
编辑:我被问到一个例如评论的例子
#myfunction{{{
Run Code Online (Sandbox Code Playgroud)
使用:例如https://github.com/cran/quantmod/blob/master/R/getSymbols.R
对于我的应用程序,我想为几个实体使用两种不同的hibernate缓存策略.因此(afaik,请纠正我,如果我错了)在实体上使用注释
@Cache(usage=ConditionalStrategy)
public class MyEntity{
...
}
Run Code Online (Sandbox Code Playgroud)
不会起作用,因为"ConditionalStrategy"必须是一个常量字段(为了与注释一起使用).
我已经了解了如何使用hibernate.cfg文件为每个实体配置缓存策略(请参阅https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html#performance-cache-mapping)但我想使用Spring LocalContainerEntityManagerFactoryBean的JPA属性直接设置它们,即
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
Properties jpaProps = new Properties();
// what to put here to configure the caching strategies per entity?
jpaProps.put(..., ....)
factory.setJpaProperties(jpaProbs);
Run Code Online (Sandbox Code Playgroud)
我如何设置JPA属性以复制基于注释的配置?这甚至可能吗?
对于那些面临同样问题的人来说更新:如果有人遇到同样的问题,也可以考虑使用Spring Cache(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html)抽象而不是hibernate注释(这是我最后所做的)
当我运行时maven install -o -X,maven 将输出(一个 jar)放入 .m2 文件夹中。问题是,在我的例子中,这会生成一个名为“maven-metadata-local.xml”的元数据文件。当我从另一个 Maven 构建引用此工件时,maven 抱怨找不到元数据文件“maven-metadata.xml”(在完全相同的位置)。
所以基本上我要问的问题是:
第二个pom中的依赖是:
<dependencies>
<dependency>
<groupId>a.b.c</groupId>
<artifactId>d</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
jar 在本地 .m2 目录中的位置是
.m2/a/b/c/d/1.0.0-SNAPSHOT/d-1.0.0-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
[DEBUG] =======================================================================
[DEBUG] Cannot access central (https://repo1.maven.org/maven2) in offline mode and the metadata ch.*****.*****:*****-server:1.0.0-SNAPSHOT/maven-metadata.xml has not been downloaded from it before
[DEBUG] Cannot access central (https://repo1.maven.org/maven2) in offline mode and the metadata ch.*****.*****:*****-server:1.0.0-SNAPSHOT/maven-metadata.xml has not been downloaded from it before
[DEBUG] Could not find metadata ch.*****.*****:*****-project:1.0.0-SNAPSHOT/maven-metadata.xml in local (/home/*****/.m2/repository)
[DEBUG] …Run Code Online (Sandbox Code Playgroud) 如果在未使用@Configuration注释的类中声明@Bean方法,则将它们称为在“精简”模式下进行处理。在@Component或什至在简单的旧类中声明的Bean方法将被视为“精简版”,其包含类的主要目的有所不同,而@Bean方法只是其中的一种奖励。例如,服务组件可以通过每个适用组件类上的其他@Bean方法将管理视图公开给容器。在这种情况下,@ Bean方法是一种简单的通用工厂方法机制。
与完整的@Configuration不同,lite @Bean方法无法声明Bean间的依赖关系。相反,它们对包含组件的内部状态以及可能声明的自变量进行操作。因此,这样的@Bean方法不应调用其他@Bean方法。每个这样的方法实际上只是用于特定bean引用的工厂方法,而没有任何特殊的运行时语义。这里的积极副作用是,不必在运行时应用CGLIB子类,因此在类设计方面没有任何限制(即,包含类仍然可以是最终类,等等)。
常规Spring组件中的@Bean方法的处理方式与Spring @Configuration类中的@Bean方法不同。不同之处在于,使用CGLIB不能增强@Component类,以拦截方法和字段的调用。CGLIB代理是一种调用@Configuration类中@Bean方法中的方法或字段的方法,以创建Bean元数据引用来协作对象。这样的方法不是用普通的Java语义调用的,而是经过容器以提供通常的生命周期管理和Spring bean的代理,即使通过@Bean方法的编程调用引用其他bean时也是如此。 相反,在普通@Component类内的@Bean方法中调用方法或字段具有标准Java语义,而无需特殊的CGLIB处理或其他约束。
我希望下面的代码抛出异常/ bean1.bean2为null,并且不会执行init方法。但是,以下代码可以正常运行并显示:
Should never be invoked
Expected null but is ch.litebeans.Bean2@402bba4f
Run Code Online (Sandbox Code Playgroud)
因此,对我而言,精简bean的行为与从@Configuration注释类构造的bean相同。有人可以指出在哪种情况下不是这种情况吗?
。
package ch.litebeans;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Bean1 bean1 = ctx.getBean(Bean1.class);
System.out.println("Expected null but is " + bean1.getBean2());
}
}
package ch.litebeans;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = {"ch.litebeans"})
public class ApplicationConfig {}
package ch.litebeans;
import org.springframework.context.annotation.Bean; …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个没有指定特定基本图像操作系统的docker文件(如FROM ubuntu:14:04).构建映像时使用的默认操作系统是否有任何规范(而不是指定基本的os-image)?
使用H2,
Environment.HBM2DDL_AUTO, "create"
Run Code Online (Sandbox Code Playgroud)
如果数据库尚不存在,则创建数据库.
但是,在Postgres中,不会创建不存在的DB,因此会抛出类似"DB不存在"之类的异常.有没有办法配置Postgres按需创建一个不存在的数据库?
以下配置文件可用于重现该问题:
使用H2工作正常:
package test.postgressql;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.cfg.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@PropertySource("file:C:/springconfig/qpmlib.properties")
@ComponentScan(basePackages = {"test.postgressql"})
@EnableJpaRepositories(basePackages = { "test.postgressql" })
@EnableTransactionManagement
public abstract class H2DBConfig {
@Autowired
org.springframework.core.env.Environment env;
public static final String DB_NAME = getNewDBName();
@Bean
public DataSource dataSource() {
DriverManagerDataSource dmds = new …Run Code Online (Sandbox Code Playgroud)