标签: spring-data

我可以在没有orm.xml文件的情况下使用Spring Data JPA审计(使用JavaConfig代替)吗?

我正在尝试让Spring Data Auditing在我的Spring 3.2.8/Spring Data 1.5/Hibernate 4项目中运行.

根据Spring Data Auditing文档,我已经@CreatedBy为我的实体添加了等等注释,由AuditorAware实现创建,并在我的JavaConfig中实例化.然而,似乎永远不会开火.

我发现文档有点令人困惑.似乎JavaConfig条目替换了xml条目,但我不确定.

orm.xml我的申请目前没有任何文件.说实话,我甚至不确定在何处/如何配置它,或者为什么我需要它.我的所有实体都在使用注释.我尝试将@EntityListeners(AuditingEntityListener.class)添加到实体,但这没有帮助.

我的当前实体管理器是在没有persistence.xml文件的情况下定义的:

    <!--  entity manager -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
        <property name="packagesToScan" value="com.ia.domain"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.query.substitutions">true '1', false '0'</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
            </props>
        </property>
    </bean>
Run Code Online (Sandbox Code Playgroud)

JavaConfig:

@Configuration
@EnableJpaAuditing
public class AuditConfig {
    @Bean
    public AuditorAware<User> auditorProvider(){
        return new SpringSecurityAuditorAware();
    }
}
Run Code Online (Sandbox Code Playgroud)

实体:

@EntityListeners({AuditingEntityListener.class})
@Entity …
Run Code Online (Sandbox Code Playgroud)

java audit spring jpa spring-data

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

如何混合Spring Data Repositories和Spring Rest Controller

目前,我通过使用@RepositoryRestResource注释它们来将一些Spring数据库公开为RESTful服务,如下所示:

@RepositoryRestResource(collectionResourceRel = "thing1", path = "thing1")
public interface Thing1Repository extends PagingAndSortingRepository<Thing1, String> {}

@RepositoryRestResource(collectionResourceRel = "thing2", path = "thing2")
public interface Thing2Repository extends CrudRepository<Thing2, String> {}
Run Code Online (Sandbox Code Playgroud)

一切都很好.当你点击我的第一个端点时,还会显示我公开的所有Spring Data Repositories,如下所示:

{
   _links: {
      thing1: {
         href: "http://localhost:8080/thing1{?page,size,sort}",
         templated: true
      },
      thing2: {
         href: "http://localhost:8080/thing2"
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

现在我有一些我想公开的端点,这些端点无法由Spring Data Repositories表示,所以我使用的是RestController.

这是一个简单的例子:

@RestController
@ExposesResourceFor(Thing3.class)
@RequestMapping("/thing3")
public class Thing3Controller {

  @Autowired 
  EntityLinks entityLinks;

  @Autowired 
  Thing3DAO thing3DAO;

  //just assume Thing3.class extends ResourceSupport. I know this is wrong, but it makes the example shorter …
Run Code Online (Sandbox Code Playgroud)

java spring spring-data spring-data-rest spring-hateoas

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

Spring Data Elasticsearch:具有相同文档的多索引

我正在使用spring-data-elasticsearch,一开始一切正常.

@Document( type = "products", indexName = "empty" )
public class Product
{
...
}

public interface ProductRepository extends ElasticsearchRepository<Product, String>
{
...
}
Run Code Online (Sandbox Code Playgroud)

在我的模型中,我可以搜索产品.

@Autowired
private ProductRepository repository;
...
repository.findByIdentifier( "xxx" ).getCategory() );
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是 - 我在不同的索引中使用相同的Elasticsearch类型,我想对所有查询使用相同的文档.我可以通过池处理更多连接 - 但我不知道如何实现这一点.

我希望有这样的东西:

ProductRepository customerRepo = ElasticsearchPool.getRepoByCustomer("abc", ProductRepository.class);
repository.findByIdentifier( "xxx" ).getCategory();
Run Code Online (Sandbox Code Playgroud)

是否可以在运行时使用不同的索引创建存储库?

非常感谢Marcel

java elasticsearch spring-data spring-boot spring-data-elasticsearch

14
推荐指数
1
解决办法
4703
查看次数

如何在Spring Data中精美地更新JPA实体?

所以我查看了有关使用Spring Data的JPA的各种教程,这在很多场合都有所不同,我不太确定正确的方法是什么.

假设有以下实体:

package stackoverflowTest.dao;

import javax.persistence.*;

@Entity
@Table(name = "customers")
public class Customer {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;

@Column(name = "name")
private String name;

public Customer(String name) {
    this.name = name;
}

public Customer() {
}

public long getId() {
    return id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)

我们还有一个DTO,它在服务层中检索,然后交给控制器/客户端.

package stackoverflowTest.dto;

public class CustomerDto {

private long id;
private String name; …
Run Code Online (Sandbox Code Playgroud)

java spring jpa spring-data spring-data-jpa

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

在一个采用过时值的事务中执行的Spring本机查询

我正在使用Spring Boot(1.4.4.REALEASE)和Spring Data来管理MySql数据库.我有以下案例:

  1. 我们更新了在一台设备上执行的一次修订RevisionService.
  2. RevisionService保存修订并调用EquipmentService以更新设备状态.
  3. updateEquipmentStatus调用Db存储过程,以便完全评估设备的修订并更新字段.

我已经尝试了一些选项,但没有实现获取设备的更新状态.该updateEquipmentStatus方法保持写入设备的先前状态(不考虑当前修订存储在事务中).代码以这种方式编写:

RevisionService

@Service
public class RevisionService{

    @org.springframework.transaction.annotation.Transactional
    public Long saveRevision(Revision rev){
        //save the revision using JPA-Hibernate
        repo.save(rev);
        equipmentService.updateEquipmentStatus(idEquipment);
    }
}
Run Code Online (Sandbox Code Playgroud)

EquipmentService

@Service
public class EquipmentService{

    @org.springframework.transaction.annotation.Transactional
    public Long updateEquipmentStatus(Long idEquipment){
        repo.updateEquipmentStatus(idEquipment);
    }
}
Run Code Online (Sandbox Code Playgroud)

EquipmentRepo

@Repository
public interface EquipmentRepo extends CrudRepository<Equipment, Long> {

    @Modifying
    @Procedure(name = "pupdate_equipment_status")
    void updateEquipmentStatus(@Param("id_param") Long idEquipment);

}
Run Code Online (Sandbox Code Playgroud)

据我所知,由于这两种方法都是用Spring的事务注释的,因此该updateEquipmentStatus方法应该在当前事务的范围内执行.我也试过了@Transactional注释的 不同选项updateEquipmentStatus,例如@Transactional(isolation=Isolation.READ_UNCOMMITTED)(这不应该是必需的,因为我使用的是同一个事务)@Transactional(propagation=Propagation.REQUIRES_NEW),但是一直没有考虑当前的状态.这就是我的存储过程保存到MySql DB的方式:

CREATE DEFINER=`root`@`localhost` PROCEDURE …
Run Code Online (Sandbox Code Playgroud)

transactions spring-data spring-data-jpa spring-boot

14
推荐指数
1
解决办法
2611
查看次数

@Transient注释,@ org.springframework.data.annotation.Transient注释,临时关键字和密码存储

目前我正在学习Spring框架,主要关注它的安全模块.我已经看过一些与注册和登录相关的指南.我在User类的密码字段中看到了transient关键字或@Transient注释的常见用法.

我的虚拟应用程序使用Spring Boot + Spring MVC + Spring Security + MySQL.

我知道

Java的transient关键字用于表示字段不是序列化的.

JPA的@Transient注释 ......

...指定属性或字段不是持久的.它用于注释实体类,映射的超类或可嵌入类的属性或字段.

和org.springframework.data.annotation的@Transient批注...

将字段标记为映射框架的瞬态字段.因此,该属性将不会被持久化,也不会被映射框架进一步检查.

在我的MySQL数据库中,我有spring_demo模式,它有3个表:

+-----------------------+
| Tables_in_spring_demo |
+-----------------------+
| role                  |
| user                  |
| user_role             |
+-----------------------+
Run Code Online (Sandbox Code Playgroud)

当我在User类的密码字段中使用transient关键字时,它不会存储在MySQL数据库中.(例如:test01)

mysql> select * from user;
+----+--------+------------------+----------+
| id | active | email            | username |
+----+--------+------------------+----------+
|  1 |      1 | test01@gmail.com | test01   |
+----+--------+------------------+----------+ …
Run Code Online (Sandbox Code Playgroud)

jpa spring-security transient spring-annotations spring-data

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

Spring Boot with JOOQ 和 Spring Data JPA 的技术区别

您什么时候将 Spring Data JPA 用于 Spring Boot 和 JOOQ,反之亦然?

我知道 Spring Data JPA 可用于完成基本的 CRUD 查询,但不适用于复杂的连接查询,而使用 JOOQ 使其更容易?

编辑:您可以将 Spring 数据 jpa 与 jooq 一起使用吗?

spring jooq spring-data spring-data-jpa spring-boot

14
推荐指数
2
解决办法
2657
查看次数

spring-data:不是托管类型:类java.lang.Object

spring-data昨天开始使用项目,并试图将测试添加到我创建的存储库中.

项目结构看起来像

persistence/pom.xml
           src/main/java/
                        ApplicationConfig
                        BaseRepository
                        Network
           src/main/test/BaseRepositoryTest
Run Code Online (Sandbox Code Playgroud)

ApplicationConfig模样

@Configuration
@ComponentScan
@EnableJpaRepositories
public class ApplicationConfig {
    @Bean
    public DataSource dataSource() {
        final EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder();
        embeddedDatabaseBuilder.setType(EmbeddedDatabaseType.H2);
        return embeddedDatabaseBuilder.build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setDatabase(Database.H2);
        jpaVendorAdapter.setGenerateDdl(true);

        final LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        localContainerEntityManagerFactoryBean.setPackagesToScan(getClass().getPackage().getName());
        localContainerEntityManagerFactoryBean.setDataSource(dataSource());

        return localContainerEntityManagerFactoryBean;
    }
}
Run Code Online (Sandbox Code Playgroud)

BaseRepository模样

import org.springframework.data.repository.Repository;

public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> {
    List<T> findAll();
}
Run Code Online (Sandbox Code Playgroud)

这 …

java spring spring-data spring-data-jpa

13
推荐指数
4
解决办法
4万
查看次数

在Spring Data Query中过滤子对象

我有以下域模型:

Playlist- > List<PlaylistItem>- >Video

@Entity
class Playlist{
   // id, name, etc
   List<PlaylistItem> playlistItems;
   // getters and setters
}


@Entity
class PlaylistItem{
   // id, name, etc.
   Video video;
   // getters and setters
}


@Entity
class Video{
   // id, name, etc.
   boolean isDeleted;
   // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

我的存储库:

public interface PlaylistRepository extends JpaRepository<Playlist, Long> {
   List<Playlist> findAll();
}
Run Code Online (Sandbox Code Playgroud)

现在,如何返回仅包含现有视频的播放列表,即如果分配给该播放列表项目的数据库中有三个视频,并且其中一个视频的isDeleted设置为true,那么我只需要获取两个项目.

java spring spring-data spring-data-jpa

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

如何在CrudRepository的外部文件中存储@Query sql?

我使用的是spring带有CrudRepositoryS代表数据库连接.

现在我需要一个很长(几行)的SQL查询,我更喜欢在类路径中的文件中维护,而不是直接在代码中.

但我怎么能做到这一点?我的回购看起来如下:

@Query(value = "<my very long sql query>", nativeQuery = true) //how to inject file content?
@Modifying
@Transactional
public void executeSpecificSql();
Run Code Online (Sandbox Code Playgroud)

java spring spring-data spring-data-jpa

13
推荐指数
2
解决办法
7419
查看次数