Skip to content

[Feature Request] - let objects have their own toString() method #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
phenzler opened this issue Feb 9, 2025 · 2 comments
Open

[Feature Request] - let objects have their own toString() method #913

phenzler opened this issue Feb 9, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@phenzler
Copy link

phenzler commented Feb 9, 2025

Reason

In the java the toString() method is intended to return a string representation of the object.
As jimmer uses the toString() method for returning the objects data as JSON, this feature cannot be used anymore.

Description

Make it possible that each object can return it's own domain specific string representation and use a jimmer specific method to get the JSON representation of the objects data.
For example:
String toJson()
or
String asJson()

Existing solutions

No response

@phenzler phenzler added the enhancement New feature or request label Feb 9, 2025
@babyfish-ct
Copy link
Owner

  1. This will affect many users, I need discuss with them: What do you think of #913? #916
  2. This will affect thousands of unit tests.
  3. Jimmer entities object are not static projo, It looks more like JSON, not classic java object.
  4. I think the implementation style of toString is not important.

@phenzler
Copy link
Author

Here is what ChatGPT says:


The toString() method in Java is primarily intended to return a human-readable representation of an object. However, its exact purpose depends on the context in which it's used.

Common Intentions of toString()

  1. Human-Readable Representation (Preferred)

    • The primary use case is to provide a concise, meaningful description of an object that is useful for debugging, logging, or UI display.
    • Example:
      public class Person {
          private String name;
          private int age;
      
          @Override
          public String toString() {
              return "Person{name='" + name + "', age=" + age + "}";
          }
      }
    • This is not necessarily all the object's data but a summary that makes sense in a human context.
  2. Dumping All Data (Less Common, but Possible)

    • Sometimes, toString() is overridden to output all fields for debugging or serialization-like purposes.
    • Example:
      @Override
      public String toString() {
          return "Person{name=" + name + ", age=" + age + ", address=" + address + "}";
      }
    • However, if the object has many fields, this can lead to long and unwieldy output.

Best Practices

  • Use for debugging, logging, or concise object description.
  • Avoid including sensitive information (e.g., passwords, tokens).
  • If you need full object data, use a structured approach like JSON serialization (Jackson, Gson) rather than relying on toString().

I understand that the jimmer entity object handles data as JSON. But that are implementation details and should in my opinion not affect the use of the object by the outside world.
The use of an ORM is to store, manage and query 'business objects' and their data and isolate him from the technical implementations that is needed for doing this. So in my opinion it should be up to the develper to deside what the output of the business object is in his system.

As I saw, you have already the tools to let the developer decide the implementation of the body of the toString() method:

@Formula(dependencies = {"name", "firstName"}) // ❹
default String toString() { return name() + ' ' + firstName(); }

Sure one could argue that the developer could create a method such as 'String getDisplay()' in his objects.
But to use this method in his system, he needs either a common superclass that all his objects extend or an Interface such as IDisplayable that all of his objects implement to make sure that every object implements that getDisplay() method.
That's more work than just using the method toString() that each java object has and that is exactly intended for this purpose.

I understand that it is quite a pain to refactor this in the current code base, but I think it is worth the effort regarding that the life of the developer using Jimmer get's a lot easier, because he does not have to build a separate mechanism for the display of it's business objects as strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants