小编Tho*_*unk的帖子

优化Spring-Data JPA查询

我正在寻找框架生成的查询的可能优化.据我了解,该过程如下:

  • 你可以声明你的域对象是POJO和增加几个注解像@Entity,@Table,@ManyToOne等等.

  • 你声明你的存储库,例如每个接口

使用(2),您可以使用多种方法来描述查询:例如,按方法名称或 @Query

如果我写一个像这样的查询:

@Query("select t from Order t LEFT join fetch t.orderPositions where t.id = ?1")
Page<Order> findById(Pageable pageable, String id);
Run Code Online (Sandbox Code Playgroud)

自动生成SQL查询,其中订单的每一列都被解析,并且顺序依次为orderpositions和依赖obejcts/tables.好像我写道:

select * from order
Run Code Online (Sandbox Code Playgroud)

因此,如果我需要来自几个连接对象的一些信息,那么查询可能会非常昂贵:而且更有趣的是非常无效.我偶然发现了一个缓慢的查询,MySQL-explain告诉我,在生成的查询中,优化器无法使用索引,这很糟糕.

当然(我知道)我必须处理权衡,生成的 SQL不像手动编写那样最优,并且具有编写较少样板代码的优点.

我的问题是:什么是改善查询,查询执行的好策略?

我自己想过一些选择:

1)是否可以为不同的目的定义几个"实体",例如Order访问订单的完整特征以及FilteredOrder更少的列和没有解决方案Join-columns?两者都会引用相同的表,但是一个将使用所有列而另一个只使用一些.

2)使用@Query(... native="true")我想要使​​用的所有列的选择.这样做的好处是,我不会将我的域对象加倍,并且会使用数百个对象Filtered丢弃我的代码库.分页怎么样?使用pageable结合@Query( ...native="true")仍然有可能(恐怕没有).

3)最后但在我看来"最糟糕"/样板解决方案:使用JDBCTemplates并在较低级别执行操作.

还有其他选择,我还没想过?感谢您对该主题的任何启发:]

更新: 我们目前的战略如下

1)在可能的情况下,我使用select new 正如我所见 …

java spring-data-jpa

15
推荐指数
1
解决办法
1万
查看次数

为什么JavaScript解析器接受JSON作为函数体?

前几天,我遇到了一些奇怪的代码:

var OrderSupplement = function() {
      oid: null;
      code: "";
      description: "";
      startdate: "";
      enddate: "";
      gender: null;
      cardowner: null;
      box: null;
      divisor: 0;
      created: null;
      createdBy: "";
};
Run Code Online (Sandbox Code Playgroud)

代码的意图是明确的对我说:这是试图定义一个构造函数,以创建一个实例OrderSupplement.

我对这种语法感到困惑.

编写代码的伙伴表示它运行良好 - 虽然它显然没有 ; 至少它不应该是什么.他实例化一个新的实例OrderSupplement和如设置OID到一个非空值,并在以后恢复它.当然,在设置之前检索一个值会揭开错误.

上面代码的效果如下:

var OrderSupplementJson = function() {}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

为什么上面的代码被接受并且不会抛出任何类型的(语法)错误?

javascript json

10
推荐指数
1
解决办法
170
查看次数

Spring与JMS + ActiveMQ的集成:重新连接后,消息仍保留在JDBC消息存储库中

我正在尝试使用Spring Integration和ActiveMQ Message Broker配置JMS.我的出站通道应该由JDBC消息存储支持,以防止数据丢失,例如代理或我的应用程序脱机.

我的配置似乎到目前为止工作,但JDBC消息存储不像我期望的那样.如果我断开代理断开连接,发送到出站通道的消息将按预期保留,但在重新连接后,它们将保留在DB中并且不会发送到队列.但是,重新连接后发送的其他消息到达队列,如果我重新启动应用程序,最后也会发送持久消息...

应用程序的context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jms="http://www.springframework.org/schema/jms"
   xmlns:int="http://www.springframework.org/schema/integration"
   xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
   xmlns:oxm="http://www.springframework.org/schema/oxm"
   xmlns:j2ee="http://www.springframework.org/schema/jee"
   xmlns:amq="http://activemq.apache.org/schema/core"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
   http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
   http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
   http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
   http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

<!-- ActiveMQ Configuration -->
<jms:annotation-driven/>
<int:annotation-config/>

<!-- Factory for ActiveMQ connections from JNDI -->
<j2ee:jndi-lookup jndi-name="java:comp/env/jms/connectionFactory"
                  id="jmsConnectionFactory"
                  expected-type="org.apache.activemq.ActiveMQConnectionFactory"
                  proxy-interface="javax.jms.ConnectionFactory"
                  lookup-on-startup="false"
                  resource-ref="true"
                  cache="true"/>


<bean id="connectionFactory" class="org.apache.activemq.jms.pool.PooledConnectionFactory" init-method="start" destroy-method="stop">
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
    <property name="maxConnections" value="8"/>
    <property name="reconnectOnException" value="true"/>
</bean>


<!-- JAXB-marshaller from spring-oxm used to handle XML-payload of messages --> …
Run Code Online (Sandbox Code Playgroud)

java activemq-classic spring-integration spring-jms

6
推荐指数
1
解决办法
1610
查看次数

Windows上的多个并行MSys / MingW安装

在安装MSysgit时,我的机器上还安装了MSys(顾名思义)。当我另外安装DevKit for Ruby时,我的计算机上又安装了MSys的另一个副本。当安装Windows的Haskell平台时,我的机器上又安装了MSys的另一个副本。有什么方法可以一次安装MSys / mingw,并告诉所有其他软件在Path中进行查找吗?

windows bash shell

5
推荐指数
1
解决办法
302
查看次数

Spring-Data-JPA分页

我使用Datatables jQuery插件来显示orderpositions列表.在调试我的应用程序时,我偶然发现了以下不一致之处.

为了检索数据,我使用@Query-Notation进行自定义查询:

@Query("select op from OrderPosition  " +
        "op where op.order.created >= ?1" +
        " and op.depot in ?2" +
        " and op.order.deliveryAddress.deliveryMode in ?3" +
        " and op.pickStatus in ?4 and op.order.id like ?5 ")
Page<OrderPosition> findOrderpositionsByFilterStatus(Date date, List<Integer>depots, List<DeliveryMode> deliveryModes, List<ArticleStatusType> pickStatus, String sSearch, Pageable p);
Run Code Online (Sandbox Code Playgroud)

发生的错误如下:

我有一套81个Orderpositions测试数据.在前端,我可以按几个标准过滤.其中一个标准是deliveryMode(快速标准).我一次显示10个条目.express交付总数为6.当通过页面进行分页时,仅express显示2 .明确过滤时,express我得到全部6.

当我添加某种排序时,例如:

@Query("select op from OrderPosition  " +
        "op where op.order.created >= ?1" +
        " and op.depot in …
Run Code Online (Sandbox Code Playgroud)

java spring jpql spring-data-jpa

5
推荐指数
1
解决办法
1355
查看次数

在Java8中使用Predicate <T>实现过滤器

为了掌握Java8,我试图实现一个简单的Repo来存储不同对象的列表.

我的出发点是:

public class MyRepo {

    Map<Class, List<?>> repo =new HashMap<>();

    public List<?> get(Class c){
        List<?> result= repo.get(c);
        return (result==null)?new ArrayList<>():(List<?>) result;
    }

    public void put(Class c,List<?> l){
        repo.put(c, l);
    }
}    
Run Code Online (Sandbox Code Playgroud)

所以我能够插入列表并检索列表.

我的下一步是实现一个通用的过滤方法:

public List<?> find(Class c, Predicate<?> predicate){
    List<?> result= repo.get(c);
    return (result==null)?new ArrayList<>():(List<?>)result.stream().filter(predicate);
}
Run Code Online (Sandbox Code Playgroud)

结果

java: incompatible types: java.util.function.Predicate<capture#1 of ?> cannot be converted to java.util.function.Predicate<? super capture#2 of ?>
Run Code Online (Sandbox Code Playgroud)

如何实现通用的查找方法?

我不得不承认,我不确定,设计与

Map<Class, List<?>> repo =new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

是个不错的选择.也许有更好的解决方案.

java java-8

2
推荐指数
1
解决办法
2676
查看次数

在Python中创建一个具有不同增量的数组

在Python中,我知道如何创建一个从某个值a开始的数组,将其上的每个组件增加一些m,直到达到某个值b:

array = numpy.arange(a, b, m)

但是,如果我想让m改变怎么办?具体来说,我想要数组 1,2,...9,10,20,...90,100,200,...900,1000..

因此,每十个组件将我的增量增加 10 倍。实现这一目标的最简单方法是什么?如果必须的话,我可以使用循环并“手动”执行此操作,但是有更好的方法吗?

python arrays numpy

2
推荐指数
1
解决办法
2万
查看次数