标签: spring-data

MongoDB的Morphia,Mongo4j和Spring数据如何比较?

我对它们如何相互比较感兴趣,哪些更成熟,哪些功能更多,更适合某些用例.

我自己的用例是创建一个实时监控服务(想想Chartbeat),但是如果你可以谈论其他用例,请做 - 毕竟这个Q&A可能对其他人有用.

orm spring mongodb morphia spring-data

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

Spring数据国际化最佳实践

我有一个带有spring数据,jpa和hibernate的spring mvc项目.我有一个多语言数据库.我设计了我的数据库和实体.我正在寻找一种按语言查询表格的最佳实践.我是否必须编写自定义jpa查询,或者是否有通用的方法来查询我的表.

如果我在db或实体设计上有错误,请警告我.

数据库:

CREATE TABLE translation (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (id));

CREATE TABLE translation_text (
  translation_id BIGINT UNSIGNED NOT NULL,
  lang VARCHAR(2),
  text VARCHAR(1000));

ALTER TABLE translation_text
ADD FOREIGN KEY (translation_id)
REFERENCES translation(id);

CREATE TABLE category (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  category_name BIGINT UNSIGNED NOT NULL,
  PRIMARY KEY (id));

ALTER TABLE category
ADD FOREIGN KEY (category_name)
REFERENCES translation(id);
Run Code Online (Sandbox Code Playgroud)

LocalizedString实体:

@Embeddable
public class LocalizedString {

    private String lang;
    private String text;

    //Constructors and …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa spring-data

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

滚动索引(动态索引名称)在Spring Data Elasticsearch中

我有一个用例,其中我想根据特定条件在单独的索引中索引我的文档.例如,我想将发票文档存储到部门名称后缀的索引.

@Document(indexName="store_{department}", indexStoreType="invoice")
public class InvoiceES{

    // fields
    @Id
    private String id;
    @Field
    private String department;
}
Run Code Online (Sandbox Code Playgroud)

是否可以使用Spring Data实现这一目标?

如果没有,是否计划在即将发布的Spring Data版本中?

elasticsearch spring-data

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

如何使用Spring Data JPA Repository从2个表中查询?

我有2个表说学生和老师,并说学生与老师有多对一的关系,并说,teacherId作为外键.

我怎样才能以某种方式使用spring数据JPA repo方法 - findByTeacherName如果我想查询下面的内容,

select * from Student S, Teacher T 
    where T.teacherName = 'SACHIN' and S.teacherId = T.teacherId
Run Code Online (Sandbox Code Playgroud)

注意:这里我只想使用查询StudentRepository,这是使用与StudentHibernateMapping类有关系的TeacherHibernateMapping类创建的

任何帮助将不胜感激.

java hibernate spring-data

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

如何混合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
查看次数

如何使用JPA和Spring Data执行存储过程?

我试图Terminal_GetTicket在我的数据库中调用存储过程,但不断收到以下异常:

PropertyReferenceException: No property getTicket found for type TicketInfo
Run Code Online (Sandbox Code Playgroud)

我用一个非常简单的测试实体交叉验证了我的配置,一切似乎都运行正常,但是对于实际情况,有些事情是错误的.

这是我的域实体(TicketInfo):

@Entity
@NamedStoredProcedureQuery(name = "TicketInfo.getTicket", procedureName = "Terminal_GetTicket", resultClasses = TicketInfo.class, parameters = { 
    @StoredProcedureParameter(mode = ParameterMode.IN, name = "sys_id_game", type = Integer.class)})
public class TicketInfo {

    @Id @GeneratedValue
    private Long id;
    private String idTicket;
    private Integer externalTicketCode;
    private Short sequenseAlert;
    private Integer dlTimeStamp;
Run Code Online (Sandbox Code Playgroud)

所有实例变量都正确定义了它们的getter和setter,并且存储过程总共有5个输出参数匹配的属性TicketInfo.

此外,这是我的存储库界面:

public interface TicketInfoRepository extends CrudRepository<TicketInfo, Long> {
    @Transactional(timeout = 5)
    @Procedure
    TicketInfo getTicket(Integer sys_id_game);
}
Run Code Online (Sandbox Code Playgroud)

另外,这是我的context.xml文件(对于Spring): …

java jpa spring-data spring-data-jpa

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

我应该将托管实体传递给需要新事务的方法吗?

我的应用程序加载了应该处理的实体列表.这发生在使用调度程序的类中

@Component
class TaskScheduler {

    @Autowired
    private TaskRepository taskRepository;

    @Autowired
    private HandlingService handlingService;

    @Scheduled(fixedRate = 15000)
    @Transactional
    public void triggerTransactionStatusChangeHandling() {
        taskRepository.findByStatus(Status.OPEN).stream()
                               .forEach(handlingService::handle);
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的HandlingService过程中,每个任务都在使用REQUIRES_NEW传播级别进行曝光.

@Component
class HandlingService {

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void handle(Task task) {
        try {
            processTask(task); // here the actual processing would take place
            task.setStatus(Status.PROCCESED);
        } catch (RuntimeException e) {
            task.setStatus(Status.ERROR);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

代码的工作原理只是因为我在TaskScheduler类上启动了父事务.如果我删除@Transactional注释,则不再管理实体,并且对任务实体的更新不会传播到db.我发现使调度方法具有事务性是不自然的.

从我看到我有两个选择:

1.保持现在的代码.

  • 也许它只是我,这是一个正确的方法.
  • 此变体对数据库的访问次数最少.

2. @Transactional从Scheduler中删除注释,传递任务的id并在HandlingService中重新加载任务实体.

@Component
class HandlingService {

    @Autowired …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa spring-data spring-data-jpa

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

Spring boot与spring batch和jpa集成

我正在将spring boot项目与spring批处理和数据jpa项目集成在一起.与作业和数据配置相关的所有内容都是正确的,除了将我的作业编写器结果保存在数据库中.在我读取文件并处理它之后,我无法将其写入mysql数据库.没有错误但也没有插入.有趣的是我的数据源已配置.因为在插入之前,我可以从数据库中获取样本记录.请帮我解决这个问题.

我的application.properties:

spring.datasource.url = jdbc:mysql://localhost:3306/batchtest?  characterEncoding=UTF-8&amp;autoReconnect=true
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)

批量配置:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;

@Bean
public ResourcelessTransactionManager transactionManager() {
    return new ResourcelessTransactionManager();
}

@Bean
public JobRepository jobRepository(ResourcelessTransactionManager transactionManager) throws Exception {
    MapJobRepositoryFactoryBean mapJobRepositoryFactoryBean = new MapJobRepositoryFactoryBean(transactionManager);
    mapJobRepositoryFactoryBean.setTransactionManager(transactionManager);
    return mapJobRepositoryFactoryBean.getObject();
}

@Bean
public SimpleJobLauncher jobLauncher(JobRepository jobRepository) {
    SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
    simpleJobLauncher.setJobRepository(jobRepository);
    return simpleJobLauncher;
}
@Bean
public FlatFileItemReader<Person> reader() {
    FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>(); …
Run Code Online (Sandbox Code Playgroud)

spring spring-batch spring-data spring-boot

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

在一个采用过时值的事务中执行的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
查看次数