Foren

Inserting Data into Database with SericeBuilder generated classes

Ravi Kiran, geändert vor 12 Jahren.

Inserting Data into Database with SericeBuilder generated classes

Junior Member Beiträge: 53 Beitrittsdatum: 08.04.12 Neueste Beiträge
Hi , the below is my table inside MYSQL Database

CREATE TABLE Article (ID INTEGER PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(256) NOT NULL,
Email VARCHAR(256) ,
Mobile int ,
Description Text );





I defined appropiate parameters inside ServiceBuilder.xml file and ran build.xml file .

It generate various impl , persisteence classes in the specified package .



Inside the class ArticleLocalServiceImpl , for inserting the Data in mYSQL Database ,
i added a method by name insertData with the DTO as a parameter , Now please tell me do i need to add JDBC code inside this method for inserting Data in Database ?? Will it be a right procedure ??

please let me know




public class ArticleLocalServiceImpl extends ArticleLocalServiceBaseImpl {

public void insertData(Users user) {

}

}
thumbnail
David H Nebinger, geändert vor 12 Jahren.

RE: Inserting Data into Database with SericeBuilder generated classes

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
No, it is the wrong procedure. Service builder already generated all of the necessary code.

Assuming that your entity is named Article, in your code that leverages SB you'd use the following:


  Article article = ArticleLocalServiceUtil.createArticle(0);
  article.setMobile(...);
  article.setName(...);
  article.setDescription(...);
  article.setEmail(...);

  ArticleLocalServiceUtil.addArticle(article);
Ravi Kiran, geändert vor 12 Jahren.

RE: Inserting Data into Database with SericeBuilder generated classes

Junior Member Beiträge: 53 Beitrittsdatum: 08.04.12 Neueste Beiträge
David H Nebinger:
No, it is the wrong procedure. Service builder already generated all of the necessary code.

Assuming that your entity is named Article, in your code that leverages SB you'd use the following:


  Article article = ArticleLocalServiceUtil.createArticle(0);
  article.setMobile(...);
  article.setName(...);
  article.setDescription(...);
  article.setEmail(...);

  ArticleLocalServiceUtil.addArticle(article);



That was very very helpful .

So what i understand is , inside the ArticleLocalServiceImpl class , inside the insertData method i need to add this code know ??


public class ArticleLocalServiceImpl extends ArticleLocalServiceBaseImpl {

public void insertData(Users user) {

Article article = ArticleLocalServiceUtil.createArticle(0);
article.setMobile(...);
article.setName(...);
article.setDescription(...);
article.setEmail(...);


}

}

And from Struts Action class i will create ArticleLocalServiceUtil object and call insertData Method ??


Please correct me if i am wrong here .
thumbnail
David H Nebinger, geändert vor 12 Jahren.

RE: Inserting Data into Database with SericeBuilder generated classes

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
No, you don't add anything to ArticleLocalServiceImpl.

The code I provided you'd use in your struts action add the article.

You only add things to ArticleLocalServiceImpl that were not generated by SB itself. For example, say you wanted to create a method like:

public Article addArticle(final String name, final String email, final int mobile);


Inside of the method you can encapsulate the different steps:

public Article addArticle(final String name, final String email, final int mobile) {
  Article article = createArticle(0);
  article.setName(name);
  article.setEmail(email);
  article.setMobile(mobile);

  return updateArticle(article);
}


This is a handy utility function that you might add, but it is not required, and it is not code that SB would generate.
Kiran Jai, geändert vor 12 Jahren.

RE: Inserting Data into Database with SericeBuilder generated classes

Junior Member Beiträge: 38 Beitrittsdatum: 09.04.12 Neueste Beiträge
David ,
Thanks for the help you were providing .

So from your post i understood that for performing the insert operation first we need to add this method inside ArticleLocalServiceImpl

public Article addArticle(final String name, final String email, final int mobile)
{
// Nothing should be done here
}


Because how can we add public Article addArticle(final String name, final String email, final int mobile); declaration to ArticleLocalServiceImpl as its a class and not a interface .


and run the build.xml file keeping Service Builder Option .


And the way we call this from the Struts Action class is


Article article = ArticleLocalServiceUtil.createArticle(0);
article.setMobile(...);
article.setName(...);
article.setDescription(...);
article.setEmail(...);

ArticleLocalServiceUtil.addArticle(article);




And this will insert the Data into Database . Am i correct ?? Please specify

And i could not able to understood this statement from you

Inside of the method you can encapsulate the different steps:

please tell me know what do you mean by the above statement ??



I am following this tutorial for using Liferay Service Builder

http://liferay-blogging.blogspot.in/2011/04/how-to-create-services-in-liferay.html and once you see Extend your LocalServiceUtil class Heading there .
thumbnail
David H Nebinger, geändert vor 12 Jahren.

RE: Inserting Data into Database with SericeBuilder generated classes

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
Kiran Jai:
So from your post i understood that for performing the insert operation first we need to add this method inside ArticleLocalServiceImpl

public Article addArticle(final String name, final String email, final int mobile)
{
// Nothing should be done here
}


Because how can we add public Article addArticle(final String name, final String email, final int mobile); declaration to ArticleLocalServiceImpl as its a class and not a interface .


My god no. I thought I was clear in my initial response. Your ArticleLocalServiceImpl would contain:

public Article addArticle(final String name, final String email, final int mobile) {
  Article article = createArticle(0);
  article.setName(name);
  article.setEmail(email);
  article.setMobile(mobile);

  addArticle(article);

  return article;
}


Once you re-run service builder, this new method will be added to ArticleLocalServiceUtil.

After this, your struts level code can call ArticleLocalServiceUtil.addArticle(name, email, mobile); to add the new article to the database.

And the way we call this from the Struts Action class is

Article article = ArticleLocalServiceUtil.createArticle(0);
article.setMobile(...);
article.setName(...);
article.setDescription(...);
article.setEmail(...);

ArticleLocalServiceUtil.addArticle(article);


And this will insert the Data into Database . Am i correct ?


This is another way for calling SB generated code from your struts action class. If you go this route, you do not have to add anything to ArticleLocalServiceImpl and you don't have to rerun service builder.

And i could not able to understood this statement from you

Inside of the method you can encapsulate the different steps:

please tell me know what do you mean by the above statement ??


In the second example above, your struts action class needs to know that it must first call the createArticle() method followed by the addArticle() method. This exposes the details of how the record is added to the database.

In the first example above, your struts action is just calling addArticle(name, email, mobile), and the details about how it is done is encapsulated (hidden) within the method implementation. Hiding details like this will often make your struts level code easier to read and maintain.

I am following this tutorial for using Liferay Service Builder: http://liferay-blogging.blogspot.in/2011/04/how-to-create-services-in-liferay.html and once you see Extend your LocalServiceUtil class Heading there .


And that's along the same lines as what I've just presented. His "addNewHelloWorld()" method encapsulates the details of what needs to occur. So to apply what he did to my example, either your struts action code would call ArticleLocalServiceUtil.addNewHelloWorld(...) (because you've encapsulated the details), or your struts action code would have to replicate the steps that he has in the addNewHelloWorld() method - the counter service invocation, the create/populate/add sequence, etc.
Kiran Jai, geändert vor 12 Jahren.

RE: Inserting Data into Database with SericeBuilder generated classes

Junior Member Beiträge: 38 Beitrittsdatum: 09.04.12 Neueste Beiträge
I cant ask for more from you . I really appreciate your effort for writing such a long response to make us understand the concept better . Thanks David once again . Before the response from you i thought of spending half part of night on this to make it work .
Roshan Qureshi, geändert vor 12 Jahren.

RE: Inserting Data into Database with SericeBuilder generated classes

Regular Member Beiträge: 159 Beitrittsdatum: 24.08.10 Neueste Beiträge
Hi Kiran,

I think David have given all answers and in a very good way.
Just want to let you know in simple that in your code you just have to use Util classes
For example you should call
<YourClass>LocalServiceUtil.<your method>
from your portlet's processAction() or render() as per your requirement

Let me know if still any confusion.
Ravi Kiran, geändert vor 12 Jahren.

RE: Inserting Data into Database with SericeBuilder generated classes

Junior Member Beiträge: 53 Beitrittsdatum: 08.04.12 Neueste Beiträge
So nice of you Thanks , i am able to insert Data now emoticon

But Without this Forums support , a new comer to Liferay will definitely struggle , because nothing much tutorials in Internet . ( Now its better since version 6 , what worse time may Liferay 4 version developers would have faced at that time )

For example , who would imagine in case of Service Builder Generated classes who would imagine to pick up only those two classes emoticon
thumbnail
Hitoshi Ozawa, geändert vor 12 Jahren.

RE: Inserting Data into Database with SericeBuilder generated classes

Liferay Legend Beiträge: 7942 Beitrittsdatum: 24.03.10 Neueste Beiträge
Liferay provides educational services.
Daniel Fernandez, geändert vor 6 Jahren.

RE: Inserting Data into Database with SericeBuilder generated classes

Junior Member Beiträge: 29 Beitrittsdatum: 29.03.17 Neueste Beiträge
Ravi Kiran:
So nice of you Thanks , i am able to insert Data now emoticon

But Without this Forums support , a new comer to Liferay will definitely struggle , because nothing much tutorials in Internet . ( Now its better since version 6 , what worse time may Liferay 4 version developers would have faced at that time )

For example , who would imagine in case of Service Builder Generated classes who would imagine to pick up only those two classes emoticon



How did you do the inserts into database?? would you mind if you put your code here?? or explain what do did you do?
thumbnail
Hitoshi Ozawa, geändert vor 12 Jahren.

RE: Inserting Data into Database with SericeBuilder generated classes

Liferay Legend Beiträge: 7942 Beitrittsdatum: 24.03.10 Neueste Beiträge
It's all written in Liferay's Developer's documentation. The documentation gives clear explanation with step by step example.

http://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/service-build-5