Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Masui Masanori
Masui Masanori

Posted on

     

[Micronaut] Accessing SQL Server 3

Intro

I will try inserting and update in this time.

Inserting and updating data

In simple cases, I can override method like below.

CategoryRepository.java

packagemicronaut.sample.posts;importio.micronaut.data.annotation.Join;importio.micronaut.data.annotation.Query;importio.micronaut.data.model.query.builder.sql.Dialect;importio.micronaut.data.r2dbc.annotation.R2dbcRepository;importio.micronaut.data.repository.reactive.ReactiveStreamsCrudRepository;importmicronaut.sample.posts.dto.SearchCategory;importreactor.core.publisher.Flux;importreactor.core.publisher.Mono;@R2dbcRepository(dialect=Dialect.SQL_SERVER)publicinterfaceCategoryRepositoryextendsReactiveStreamsCrudRepository<Category,Long>{...// Insert@OverrideMono<Category>save(Categorycategory);// Update@OverrideMono<Category>update(Categorycategory);}
Enter fullscreen modeExit fullscreen mode

Because "save" and "update" don't differetiate whether the model class data is newly created or retrieved from the DB, So I must use them properly.

PostService.java

packagemicronaut.sample.posts;importjava.time.LocalDateTime;importio.micronaut.transaction.annotation.Transactional;importjakarta.inject.Singleton;importmicronaut.sample.posts.dto.SearchCategory;importmicronaut.sample.users.UserRepository;importreactor.core.publisher.Flux;importreactor.core.publisher.Mono;@SingletonpublicclassPostService{privatefinalPostRepositoryposts;privatefinalCategoryRepositorycategories;privatefinalUserRepositoryusers;publicPostService(PostRepositoryposts,CategoryRepositorycategories,UserRepositoryusers){this.posts=posts;this.categories=categories;this.users=users;}...publicMono<String>updateCategory(StringcategoryName){returncategories.findByName(categoryName).flatMap(c->{c.setLastUpdateDate(LocalDateTime.now());// Don't do this because this insert new "Category" data.// I should write "categories.update(c)";returncategories.save(c);}).switchIfEmpty(Mono.defer(()->{Categoryc=newCategory();c.setName(categoryName);c.setLastUpdateDate(LocalDateTime.now());returncategories.save(c);})).map(c->"OK");}}
Enter fullscreen modeExit fullscreen mode

Insert data into multiple tables

Unlike Entity Framework Core, they don't automatically insert or update data into relational tables.
Therefore, each data must be inserted or updated in order.

PostService.java

packagemicronaut.sample.posts;importjava.time.LocalDateTime;importio.micronaut.transaction.annotation.Transactional;importjakarta.inject.Singleton;importmicronaut.sample.posts.dto.SearchCategory;importmicronaut.sample.users.UserRepository;importreactor.core.publisher.Flux;importreactor.core.publisher.Mono;@SingletonpublicclassPostService{...publicMono<String>addSamplePost(Stringtitle,Stringcontents,StringcategoryName){returncategories.findByName(categoryName).map(c->{PostnewPost=newPost();newPost.setTitle(title);newPost.setContents(contents);newPost.setCategories(c);newPost.setLastUpdateDate(LocalDateTime.now());returnnewPost;}).switchIfEmpty(Mono.defer(()->{Categoryc=newCategory();c.setName(categoryName);c.setLastUpdateDate(LocalDateTime.now());// Insert new "Category" datareturncategories.save(c).map(newCategory->{PostnewPost=newPost();newPost.setTitle(title);newPost.setContents(contents);newPost.setCategories(newCategory);newPost.setLastUpdateDate(LocalDateTime.now());returnnewPost;});})).flatMap(p->users.findById(1L).map(u->{p.setUsers(u);returnp;})).flatMap(p->// Insert new "Post" dataposts.save(p).map(pst->"OK: "+pst.getId()));}}
Enter fullscreen modeExit fullscreen mode

Transaction

When the program cases some exceptions on inserting and updating data, I want to rollback.
To do this, I can add a "@Transactional" annotation.

...// Add this@Transactional()publicMono<String>updateCategory(StringcategoryName){returncategories.findByName(categoryName).flatMap(c->{c.setLastUpdateDate(LocalDateTime.now());returncategories.update(c);}).switchIfEmpty(Mono.defer(()->{Categoryc=newCategory();c.setName(categoryName);c.setLastUpdateDate(LocalDateTime.now());returncategories.save(c);})).flatMap(c->{// Intentionally raise an exception to validate the rollbackc.setName(null);c.setLastUpdateDate(LocalDateTime.now());returncategories.update(c);}).map(c->"OK");// Don't add this line// .onErrorResume(e -> Mono.just(e.getMessage()));}}
Enter fullscreen modeExit fullscreen mode

In this code, I must not add "onErrorResume".
If I add it, rollback is not performed because exception occurrence cannot be detected.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Programmer, husband, fatherI love C#, TypeScript, Go, etc.
  • Location
    Wakayama, Japan
  • Joined

More fromMasui Masanori

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp