0

I have a strange behavior. I have an entity which uses @Embeddables to store a type information (TerminalType). This types should have only fixed values. Therefore I created constants like CLIENT, BROWSER, EXTRENAL.

@Embeddable
@Audited
public class TerminalType extends DomainValue {

   private static final long serialVersionUID = 1L;

   public static final TerminalType CLIENT = new TerminalType("CLIENT");

   public static final TerminalType BROWSER = new TerminalType("BROWSER");

   public static final TerminalType EXTERNAL = new TerminalType("EXTERNAL");

   protected TerminalType() {

   }

   protected TerminalType(String id) {
      this.value = id;
   }

   @Column(name = "VALUE")
   private String value;

   /* only getter for the value ... */

}

This class is then used in an entity Terminal

@Entity
@Audited
public class Terminal  {

   private static final long serialVersionUID = 1L;

   @Id
   protected String terminalName;


   @NotNull
   @Embedded
   @AttributeOverride(name = "value", column = @Column(name = "TERMINAL_TYPE"))
   protected TerminalType type;

   protected Terminal() {

   }

   /* setter and getter ... */

}

The problem is that the values constants sometimes change. The application can run for hours, but after a while the constant CLIENT has the value "BROWSER".

I do not have any idea what/who changes the constant? Any idea will help! Thanks!

MarkusL
  • 1
  • 1
  • Have you considered using an Java `Enum` instead of a standard Java class? – V G Sep 10 '14 at 10:04
  • Yes... but Enums cannot be extended. We are providing a software product which should be extendable by our customers. So maybe a new TerminalType like MOBILE a customer wants to add. This cannot be done with Enums.... – MarkusL Sep 10 '14 at 13:11
  • Try adding a breakpoint in the setter to find who is changing it. – Nikos Paraskevopoulos Sep 10 '14 at 13:18
  • Yes, that would be easy, but I added a wrong comment. for the embeddable only a getter for the value exists. No setter at all. I already debugged hibernate and see that Hibernate uses Reflection to set this value. Is it in general a good idea to use Embeddeable constants? – MarkusL Sep 10 '14 at 13:53
  • I ran into the same problem when using static final of `@Embeddable`with Eclipselink instead of Hibernate. Workaround: introduce factory methods like `TerminalType.client() { return new TerminalType("CLIENT"); }` – jensfischerhh Feb 02 '17 at 09:52

0 Answers0