Reputation: 1676
@Entity
public class Person {
private Integer id = null;
private String name = null;
private String price = null;
private Date created = null;
// String Representation:
@Override
public String toString() {
return name;
// I want to return name and price
}
I want to return name
and price
in toString function ? This is right to return more than one string in toString function. If you make relation in other's Entity ManyToMany ?
Please suggest me if I am doing right or wrong as I want to show these fields in other entity where I make relations.
Thanks!
Upvotes: 2
Views: 2212
Reputation: 12401
You can do it like this:
return name+" "+price;
You can create another method to return both.
you can return String array
as well so that you don't need to split the string if you need to perform any operation on name and price.
Upvotes: 1
Reputation: 50568
You can use a StringBuilder
and build up your composed String
efficiently from both the name, the price and whatever you want.
Here the documentation.
Anyway, the response is no, you cannot send back two strings, but you can return a string that is a composition of the others.
Upvotes: 0
Reputation: 505
Usually the toString()
method returns a string-representation of the object and not the object's members themself. So if you need a representation of name and price you could do
return "Name: " + name + ", Price: " + price;
If you really want to receive the members name
and price
you should generate getters for those and use them in the caller.
Another possibility is to "wrap" the two strings in some sort of data class.
Upvotes: 3
Reputation: 122026
This is right to return more than one string in toString function. If you make relation in other's Entity ManyToMany ?
That could be
@Override
public String toString() {
return "Name :" +name + " Price : "+price;
}
If you still have more Objects related to it, just append in the last. So that you won't loose information.
Upvotes: 1