【问题标题】:Spring data join mysql entity with collection mongodbSpring数据将mysql实体与集合mongodb连接起来
【发布时间】:2017-01-07 11:38:25
【问题描述】:

我正在用弹簧数据做一个项目。我的架构中有两个表:

  • Movement:此表必须包含 hu 的每个动作。在生产中,这张表会有很多记录,所以我会将运动数据放在 mongodb 数据库中。

我了解到可以使用更多数据源。但是可以使用 mysql 数据源和 mongodb 数据源吗?如果是,可以将 HU 链接到运动(加入)吗?运动集合有 hu_id 列。

【问题讨论】:

    标签: spring mongodb hibernate spring-data spring-mongo


    【解决方案1】:

    是的,可以使用两种数据源,一种是 SQL,另一种是 NOSQL。

    但我觉得在两个实体之间建立联系是不可能的,而且听起来也不对。

    不管怎样,我已经尝试过这种方法,

    Entity1.java :(SQL 实体)

    @Entity
    @Table(name="ENTITY1")
    public class Entity1 implements Serializable{   
    
        @Id
        private long id;
    }
    

    Entity2.java :(NOSQL 实体)

    @Document(collection="test")
    
    public class Entity2  implements Serializable{
    
        @org.springframework.data.annotation.Id
        private long Id;
    
        //storing reference of entity1 
        @Field("Entity1REF")
        private long entity1Id;
    
    }
    

    Entity1Repository:

    public interface Entity1Repository extends JpaRepository<Entity1, Long> ()
    

    Entity2Repository:

    public interface Entity2Repository extends MongoRepository<Entity2, Long>{
    

    在对实体执行 CRUD 操作时:

    利用适当的 repos 来执行。

        @Autowired
        private Entity1Repository entity1Rep;
    
        @Autowired
        private Entity2Repository entity2Rep;
    
    
    
           public void init(){
                Entity1 en1=new Entity1(100);
                en1=entity1Rep.save(en1);
                Entity2 en2=new Entity2(1000,en1.getId());
                entity2Rep.save(en2);
    }
    

    请通过这个项目尝试过: https://github.com/BarathArivazhagan/Spring-MongoDB-Samples/tree/tree/Spring-Data-Mongo-SQL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 2015-06-18
      • 2020-05-04
      • 2020-06-13
      • 2012-05-15
      • 2019-08-03
      • 2020-06-29
      相关资源
      最近更新 更多