getnstanceName() deprecated - why, and what to use instead?

I have an entity that is a detail entity of a master, and in the master I have a Transient field that needs to return the instance name of the first detail line. Easy to do, except the Studio is saying getInstanceName() is deprecated.

What should I be using instead?

Just to add details, here is the code of the transient property:

    @Transient
    @MetaProperty(related = "diagnoses")
    public String getPrimaryDiagnosis() {
        if (!diagnoses.isEmpty()) {
            return diagnoses.iterator().next().getCode().getInstanceName();
        }
        else {
            return "NONE";
        }
    }

diagnoses is the composition collection. If I take off the getInstanceName() and use toString() instead, I get the fullyqualifiedclassname-UUID-[detached] type of string instead of what I need.

Hi,

First, if something is deprecated in Java world, it doesn’t mean that you must not use it.
You can proceed with using getInstanceName() method and not worry until some point in the future when (if) you will have to migrate your project to newer CUBA version.

Second, you can do Ctrl+Left mouse click on the getInstanceName() usage place in the code and navigate to this method’s definition.

You will see this:

    /**
     * @deprecated Use {@link MetadataTools#getInstanceName(com.haulmont.chile.core.model.Instance)} instead.
     */
    @Deprecated
    @Override
    public String getInstanceName() {
        return InstanceUtils.getInstanceName(this);
    }

Now you see what JavaDoc suggests to use instead: MetadataTools#getInstanceName().

Then you can search this class by name (Ctrl + N) and see that MetadataTools is marked as @Component. It means it’s a Spring bean that can be injected to other Spring beans or screens, or obtained with AppBeans.get() method call.

2 Likes