3

I have the following case.

public enum TestEnum{

    PUUNITONE("puunitone"),
    PUUNITTWO("puunittwo");

    private String name;

    private TestEnum(String name) {
        this.name = name;
    }


    public String getName() {
        return name;
    }

}

in the EJB @stateless class

@PersistenceContext(unitName = TestEnum.PUUNITONE.getName())
private EntityManager entityManager;

I have the following

Compilation Error: Value must be a constant

Now my question:

1. In java are enums constants or not? If yes what is the problem here?
2. Is there a way or workarround to solve this issue by using Enum as constant?

Thanks a lot for any suggestion

Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
  • 1
    Did you try it without "getName()"? Oracle docs says this: "...that enables for a variable to be a set of predefined constants". See here: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html. – mwhs Oct 31 '13 at 08:58
  • 2
    May be a duplicate of this: http://stackoverflow.com/questions/3271659/use-enum-type-as-a-value-parameter-for-rolesallowed-annotation – mwhs Oct 31 '13 at 09:02
  • @mwhs Thanks for your comment. unfortunately it doesn't work. The same error apprears – Festus Tamakloe Oct 31 '13 at 09:41

1 Answers1

6
  1. Yes, enums are constants, however getName() is not a constant : the compiler can't tell that it will always return the same result.
  2. No, you have to use a litteral String or a final static String in the annotation. Only those are constants for the compiler.
WilQu
  • 7,131
  • 6
  • 30
  • 38