小编rod*_*757的帖子

spring data jpa本机查询与连接

如何在 spring data jpa 中执行本机查询,同时获取子实体?如果我在子实体对象上有 Eager FetchType,则 spring 数据正在执行 2 个查询。1 为父实体,1 为子实体。

有没有办法只执行 1 个本地查询来获取父实体和子实体?

家长:

@Entity
public class Parent {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;

   @Temporal(TemporalType.TIMESTAMP)
   private Date ts;

   @ManyToOne(fetch=FetchType.LAZY)
   private Child child;
}
Run Code Online (Sandbox Code Playgroud)

孩子:

@Entity
public class Child {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;

   @OneToMany(fetch=FetchType.LAZY, mappedBy="parent")
   private Parent parent;
}
Run Code Online (Sandbox Code Playgroud)

询问:

public interface ParentRepository extends Repository<Parent, Integer> {
    @Query(value = "SELECT * from parents p inner join children c on c.id=p.childId where TIMESTAMPDIFF(SECOND, …
Run Code Online (Sandbox Code Playgroud)

mysql hibernate jpa spring-data spring-data-jpa

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

从Spring JPA packagesToScan中排除Entity

有没有办法使用Spring的LocalContainerEntityManagerFactoryBean和Hibernate在包中排除实体?如果重要的话,我正在使用spring-data.我想用另一个数据源管理被排除的实体.我知道你可以使用hibernateSessionFactory(在扫描PackagesToScan时忽略一些类)来做到这一点,但我更愿意使用jpa.

这是我的配置:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd ">

    <context:property-placeholder location="classpath:app.properties" ignore-unresolvable="true"/>

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${class}" />
        <property name="url" value="${url}" />
        <property name="username" value="${user}" />
        <property name="password" value="${password}" />
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="packagesToScan">
                <value>com.mypackage.new.entities</value>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.id.new_generator_mappings">true</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
            </props>
        </property>
    </bean>

    <!-- Enables …
Run Code Online (Sandbox Code Playgroud)

spring hibernate spring-data spring-data-jpa

5
推荐指数
0
解决办法
3318
查看次数

ElasticSearch:检查嵌套对象数组是否为空

如何获取没有字段中任何对象的所有文档?

我有以下映射:

"properties" : {
  "name": {
    "type": "text"
  },
  "nestedArray": {
    "type": "nested",
    "properties": {
      "prop1": {
        "type": "text"
      },
      "prop2": {
        "type": "text"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望得到"nestedArray"为空或不存在的所有文档.

我正在使用elasticSearch 5.0

elasticsearch

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