The best replacement for static final


#1

in Java we use something like

@NamedQuery(name = Entity.FIND_BY_NAME,
  query = “select o from Entity o where o.name = ?1”)
public class Entity {
  public static final String FIND_BY_NAME = “Entity.byName”;

  public static Query findByName(EntityManager em, String name) {
  return em.createNamedQuery(FIND_BY_NAME).setParameter(1, name);
  }
}

but in Kotlin I can’t find appropriate way to replace such kind of constants:
package level values may cause naming conflicts.


#2

This should work:

@NamedQuery(name = Entity.FIND_BY_NAME,
            query = "select o from Entity o where o.name = ?1")
public class Entity {
    companion object {
        public val FIND_BY_NAME: String = "Entity.byName"
    public fun findByName(em: EntityManager,

                           name: String): Query {
           return em.createNamedQuery(FIND_BY_NAME).setParameter(1, name)
  }
  }
}


If your Java class had only static fields and methods you could write:

@NamedQuery(name = Entity.FIND_BY_NAME,            query = "select o from Entity o where o.name = ?1") public object Entity {   public val FIND_BY_NAME: String = "Entity.byName"

  public fun findByName(em: EntityManager,
                          name: String): Query {
  return em.createNamedQuery(FIND_BY_NAME).setParameter(1, name)
  }
}


#3

Thank you!