sunitk / multitenancy-dynamic-tenant

Spring Boot 2 JPA Hibernate with database per tenant with dynamic configuration of tenants, secured by Spring Security
Apache License 2.0
99 stars 76 forks source link

new user is not getting save from code #13

Closed ShaluKashyap1991 closed 3 years ago

ShaluKashyap1991 commented 4 years ago

Hi Sunit,

Hope you are doing well.

While i was working in this project i found that when i was trying to add new tenant from Login Controller wherein i found that new user is not getting saved in databse.

Can you please check and get back to me if you can help.

@RequestMapping("/user/index") public String userIndex(Model model) { getLoggedInUsername().ifPresent(f -> { model.addAttribute("userName", f); }); getTenantName().ifPresent(d -> { model.addAttribute("tenantName", d); });

    User user = new User();
    user.setUsername("Shalu");
    user.setTenant("tenant_5");
    user.setPassword("hello");
    user.setActive(true);
    userService.save(user);

    return "user/index";
}

Thank you:)

sunitk commented 4 years ago

Hi,

You cannot just save entities from a controller as you need transaction support etc. You should create a Service Layer which talks to the Repository layer.

So in your User entity please annotate it with @Entity.Then create a repository like UserRepository with the annotation @Repository at the class level. Finally you can write a Service layer which can call your repository method.

Your service layer needs to be @Autowired into your MVC COntroller where you can access the user methods.

Regards, Sunit Katkar https://sunitkatkar.blogspot.com

On Mon, Sep 21, 2020 at 11:38 AM Shalu Kashyap notifications@github.com wrote:

Hi Sunit,

Hope you are doing well.

While i was working in this project i found that when i was trying to add new tenant from Login Controller wherein i found that new user is not getting saved in databse.

Can you please check and get back to me if you can help.

@RequestMapping https://github.com/RequestMapping("/user/index") public String userIndex(Model model) { getLoggedInUsername().ifPresent(f -> { model.addAttribute("userName", f); }); getTenantName().ifPresent(d -> { model.addAttribute("tenantName", d); });

User user = new User();
user.setUsername("Shalu");
user.setTenant("tenant_5");
user.setPassword("hello");
user.setActive(true);
userService.save(user);

return "user/index";

}

Thank you:)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sunitk/multitenancy-dynamic-tenant/issues/13, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOCB4DXEONET4SU5M3ISTLSG6MR5ANCNFSM4RU3SAJA .

ShaluKashyap1991 commented 4 years ago

Thank you so much sunit for response.

I have done it

Sent from my iPhone

On 10-Oct-2020, at 9:59 AM, Sunit Katkar notifications@github.com wrote:

 Hi,

You cannot just save entities from a controller as you need transaction support etc. You should create a Service Layer which talks to the Repository layer.

So in your User entity please annotate it with @Entity.Then create a repository like UserRepository with the annotation @Repository at the class level. Finally you can write a Service layer which can call your repository method.

Your service layer needs to be @Autowired into your MVC COntroller where you can access the user methods.

Regards, Sunit Katkar https://sunitkatkar.blogspot.com

On Mon, Sep 21, 2020 at 11:38 AM Shalu Kashyap notifications@github.com wrote:

Hi Sunit,

Hope you are doing well.

While i was working in this project i found that when i was trying to add new tenant from Login Controller wherein i found that new user is not getting saved in databse.

Can you please check and get back to me if you can help.

@RequestMapping https://github.com/RequestMapping("/user/index") public String userIndex(Model model) { getLoggedInUsername().ifPresent(f -> { model.addAttribute("userName", f); }); getTenantName().ifPresent(d -> { model.addAttribute("tenantName", d); });

User user = new User(); user.setUsername("Shalu"); user.setTenant("tenant_5"); user.setPassword("hello"); user.setActive(true); userService.save(user);

return "user/index"; }

Thank you:)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sunitk/multitenancy-dynamic-tenant/issues/13, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOCB4DXEONET4SU5M3ISTLSG6MR5ANCNFSM4RU3SAJA .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

sunitk commented 3 years ago

Closing this now.