cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How do i save data to a database (Customer/User)

Former Member
0 Likes
2,715

Hi guys,

I am new to hybris development, and I have created a custom extension in my hybris. I don't really understand how to use an existing service to persist my Customer data to the database.

I have tried using the ModelService, but i do not know how to initialize it.

It would be great if anyone can help me understand how data can be persisted in Hybris Commerce.

Thank You,

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Likes

Hi All,

I solved my issue, to initialize the modelService, we can use the code below. Also if we are performing CRUD operations, we need to provide the Uid of the model, before save().

 final ModelService modelService = Registry.getApplicationContext().getBean("modelService", ModelService.class);

Answers (2)

Answers (2)

Former Member
0 Likes

Have you added the dependency in spring.xml file?

Former Member
0 Likes

Hi Ashish,

Do you mean the CustomerModel dependency in my extension spring dependency xml?

If so, then no i have not. I will try again.

arvind-kumar_avinash
Active Contributor
0 Likes

Hi - you need to inject ModelService as shown below:

 @Resource
 private ModelService modelService;

and then after setting the values in your model object, you need to save the model using modelService.save(your-model-object)

If you still get NPE, please inject ModelService as given below:

 @Autowired
 private ModelService modelService;

and use the following in your Spring Configuration file:

 <context:annotation-config/>
 <context:component-scan  base-package="your-package-name"/>

You can find many examples in Test classes. A File Search in ee with the parameters as given in the screenshot will return the classes you can refer for your requirement.

Former Member
0 Likes

Hi Avinash,

Thanks for getting back on this. I have tried the method above, but i am still getting a null pointer exception on line 35. Here is my code, please let me know what am I doing wrong here.

arvind-kumar_avinash
Active Contributor
0 Likes

I am not sure if you are really getting the NPE at line#35. Could you please replace line#31 with CustomerModel customer = modelService.create(CustomerModel.class); and see if you get the NPE at line#31? If yes, probably your server has started with some errors.

Former Member
0 Likes

Hi Avinash,

Thank you for your quick reply. I tried again with your suggestion and NPE occurs on line 31 with the modelService.create().

Do i need to maintain dependencies in my extension dependency.xml if I am using CustomerModel? Maybe there is a runtime error occuring because of this?

arvind-kumar_avinash
Active Contributor
0 Likes

Did you try injecting ModelService as given below?

  @Autowired
  private ModelService modelService;

Do i need to maintain dependencies in my extension dependency.xml if I am using CustomerModel? Maybe there is a runtime error occuring because of this?

No. This problem is because ModelService is not getting instantiated.
Former Member
0 Likes

Yes, I changed it to the autowired annotation.

Former Member
0 Likes

Hi Avinash,

I have updated in my comment's above how I initialize the modelService.

Thank you,