Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Service Builder: Overriding toCompare generates wrong code
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?
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?
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>
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>
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.
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.
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.
Sorry David H Nebinger,
i am learning liferay portal
please give me suggestion,
Regards
Ram
i am learning liferay portal
please give me suggestion,
Regards
Ram
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.
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.
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.
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.
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.
You just shouldn't override the java methods.
Thanks for the quick reply David. I'll follow that approach.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™