Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb reactive spring boot DBRef resolution is not supported

I wrote this programe spring boot mongodb reactive

@SpringBootApplication
public class ReactitveMongoDbApplication {

    public static void main(String[] args) {
        SpringApplication.run(ReactitveMongoDbApplication.class, args);
    }

    @Bean
    CommandLineRunner ok(SoscieteRepository sos, TransactionRepository trs) {
        return a -> {
            List<Sosciete> ls = List.of(new Sosciete("SG", "sosciete general", 1235.22),
                    new Sosciete("AB", "AIR BOLL", 478.36), new Sosciete("TO", "TOYOTA", 458.24));
            trs.deleteAll().subscribe(null, null, () -> {
                sos.deleteAll().subscribe(null, null, () -> {
                    ls.forEach(t -> sos.save(t).subscribe(so -> {
                        System.out.println(so);
                        for (int i = 0; i < 10; i++) {
                            Transaction tr = new Transaction();
                            tr.setDate(Instant.now());
                            tr.setSosciete(so);
                            double x = 1 + ((Math.random() * 12) - 6) / 100;
                            tr.setPrice(so.getPrice() * x);
                            trs.save(tr).subscribe(ts -> {
                                System.out.println(ts);
                            });
                        }
                    }));
                });
            });
            System.out.println("done !");
        };
    }
}

interface SoscieteRepository extends ReactiveMongoRepository<Sosciete, String> {
}

@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
class Sosciete {
    @Id
    private String id;
    private String name;
    private double price;
}

interface TransactionRepository extends ReactiveMongoRepository<Transaction, String> {
}

@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
class Transaction {
    @Id
    private String id;
    private Instant date;
    private double price;
    @DBRef(db = "sosciete", lazy = true)
    private Sosciete sosciete;
}

@RestController
class ReactiveRestController {
    @Autowired
    private SoscieteRepository sos;
    @Autowired
    private TransactionRepository trans;

    @GetMapping(value = "/soscietes")
    public Flux<Sosciete> getAllSc() {
        return sos.findAll();
    }

    @GetMapping(value = "/transactions")
    public Flux<Transaction> getAllTr() {
        return trans.findAll();
    }
}

the dependencies:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

aplication.prop file:

spring.data.mongodb.database=webflux
spring.data.mongodb.port=27017
server.port=3000

my code works fine in this link :

http://localhost:3000/soscietes

but in this link:

http://localhost:3000/transactions

the code throws the following error:

java.lang.UnsupportedOperationException: DBRef resolution is not supported!

it's the DBRef annotation that is cause for this error, it does not work at all. It throws the following exception. can we add such a configuration to make it work? thank you in advance

like image 526
abdennour Avatar asked Oct 24 '25 16:10

abdennour


2 Answers

I got the same issue with Spring boot version : 2.7.0

And i changed @DBRef to @DocumentReference and it works

like image 123
Hilmi Reda Avatar answered Oct 26 '25 05:10

Hilmi Reda


I found the solution: i modified the spring boot parent version in pom.xml:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath />
    </parent>

to 2.1.2.RELEASE

like image 26
abdennour Avatar answered Oct 26 '25 06:10

abdennour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!