Message Boards

Service Builder: Overriding toCompare generates wrong code

Subi Aili, modified 8 Years ago.

Service Builder: Overriding toCompare generates wrong code

New Member Posts: 14 Join Date: 7/24/15 Recent Posts
Hello everyone,

I hope someone can help me with this issue.

What I have done so far:
After defining the tables and columns in the file service.xml, the service layer is generated from it through the command mvn liferay:build-service, which is performed successfully. In the following process, I write my own methods into the generated {Entityname}Impl.java classes and after that I let the service builder regenerate it through the same maven command as described before.
Now the Problem:
In the generated {Entityname}Impl.java classes I override the methods equals(Object object) and compareTo({Entityname} entity). After the regeneration, the classes {Entityname}Clp.java and {Entityname}Wrapper.java have the methods equals and compareTo each two times defined which of course throws a duplicated methode exception.

Is there a solution for this or do I have to take the compareTo and equals methods the service builder is forcing me to use?
thumbnail
Ramalingaiah Dudidmetle, modified 8 Years ago.

RE: Service Builder: Overriding toCompare generates wrong code

Expert Posts: 486 Join Date: 8/16/14 Recent Posts
Hi Sototu Alekido,
this for only service.xml inside past code --->deploy---> service builder --->then it will come for table and src code
this useful for you
<entity name="LMSBook" local-service="true" remote-service="false"> <!-- PK fields -->

<column name="bookId" type="long" primary="true" id-type="increment" /> <!-- UI fields -->

<column name="bookTitle" type="String" />

<column name="author" type="String" /> <!-- Audit fields -->

<column name="createDate" type="Date" />

<column name="phone" type="String" />

<column name="email" type="String"/>

</entity>
Sototu Alekido, modified 8 Years ago.

RE: Service Builder: Overriding toCompare generates wrong code

New Member Posts: 14 Join Date: 7/24/15 Recent Posts
Thanks Ramalingaiah Dudidmetle,

I edited the generated code after their generation, if I unterstood rightly.

I think I will resolve this problem for me by accepting the enforcement of the service builder and rebuild the parts of the projects depending on this service.
thumbnail
David H Nebinger, modified 8 Years ago.

RE: Service Builder: Overriding toCompare generates wrong code

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
Ramalingaiah Dudidmetle:
Hi Sototu Alekido,
this for only service.xml inside past code --->deploy---> service builder --->then it will come for table and src code


Um, it wasn't useful at all because you didn't address the OP's question, you just threw out a post with some crap on it.
thumbnail
Ramalingaiah Dudidmetle, modified 8 Years ago.

RE: Service Builder: Overriding toCompare generates wrong code

Expert Posts: 486 Join Date: 8/16/14 Recent Posts
Sorry David H Nebinger,

i am learning liferay portal
please give me suggestion,

Regards
Ram
thumbnail
David H Nebinger, modified 8 Years ago.

RE: Service Builder: Overriding toCompare generates wrong code (Answer)

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
Sototu Alekido:
In the generated {Entityname}Impl.java classes I override the methods equals(Object object) and compareTo({Entityname} entity). After the regeneration, the classes {Entityname}Clp.java and {Entityname}Wrapper.java have the methods equals and compareTo each two times defined which of course throws a duplicated methode exception.

Is there a solution for this or do I have to take the compareTo and equals methods the service builder is forcing me to use?


You can't and really shouldn't override the equals() and compareTo() methods. These have special meanings with regards to the lower level Hibernate stuff and even the JVM has certain expectations with respect to their implementations and access. Besides, they can be called all over the place when you least expect and wouldn't want all of these extra service invokes any time they're used.
Sototu Alekido, modified 8 Years ago.

RE: Service Builder: Overriding toCompare generates wrong code

New Member Posts: 14 Join Date: 7/24/15 Recent Posts
David H Nebinger:
Sototu Alekido:
In the generated {Entityname}Impl.java classes I override the methods equals(Object object) and compareTo({Entityname} entity). After the regeneration, the classes {Entityname}Clp.java and {Entityname}Wrapper.java have the methods equals and compareTo each two times defined which of course throws a duplicated methode exception.

Is there a solution for this or do I have to take the compareTo and equals methods the service builder is forcing me to use?


You can't and really shouldn't override the equals() and compareTo() methods. These have special meanings with regards to the lower level Hibernate stuff and even the JVM has certain expectations with respect to their implementations and access. Besides, they can be called all over the place when you least expect and wouldn't want all of these extra service invokes any time they're used.


Thank you David H Nebinger,

that was the clear answer I couldn't distill from the google searches prior.
Enrique Valdes Lacasa, modified 4 Years ago.

RE: Service Builder: Overriding toCompare generates wrong code

New Member Posts: 3 Join Date: 11/5/19 Recent Posts
Thanks everyone for the feedback. I ran into the same question that this thread proposes, so I'm glad I found it. Now, since David pointed out that  "You can't and really shouldn't override the equals() and compareTo() methods."...my question is:
Is there a preferred way in Liferay (and maybe within the Service Builder code) for comparing objects other than, for example, going field by field checking their value?
Thanks beforehand.
thumbnail
David H Nebinger, modified 4 Years ago.

RE: Service Builder: Overriding toCompare generates wrong code

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
You are free to add methods to your model classes. So you could add a "compareToLimited()" sort of thing that implements what you want and then leverage it the way you want to.


You just shouldn't override the java methods.
Enrique Valdes Lacasa, modified 4 Years ago.

RE: Service Builder: Overriding toCompare generates wrong code

New Member Posts: 3 Join Date: 11/5/19 Recent Posts
Thanks for the quick reply David. I'll follow that approach.