Spring data join mysql entity with collection mongodb

467 views Asked by At

i m doing a project with spring data. I have two table in my schema:

  • Hu
  • Movement: this table must contain each movement of hu. In production this table will have a lot of record so i will put the movement data on mongodb databale.

I've read that it is possible to use more datasource. But it is possible to use a mysql datasource and a mongodb datasource? If yes is possible to link HU to movement ( a join ) ? Movement collection have hu_id column.

1

There are 1 answers

0
Barath On

yes it is possible to use two datasources one is SQL and the other one is NOSQL.

But I feel linking between two entities is not possible and it doesnt sound right.

Anyway, I have tried this approach where

Entity1.java :(SQL entity)

@Entity
@Table(name="ENTITY1")
public class Entity1 implements Serializable{   

    @Id
    private long id;
}

Entity2.java :(NOSQL entity)

@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>{

While performing CRUD operations on the entity:

Make use of appropriate repos to perform.

    @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);
}

Please go through this project tried : https://github.com/BarathArivazhagan/Spring-MongoDB-Samples/tree/tree/Spring-Data-Mongo-SQL