We run a JAVA web application on a Wildfly 8.0.0 server. We have an email template editor form which uses Mustache Fields to put some variable data into the template. The mail also have an enum field which specifies the type of the template. The entity structure is something like this:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DTYPE", discriminatorType = DiscriminatorType.STRING, length = 255)
@DiscriminatorOptions(force = true)
@DiscriminatorValue("MailTemplate")
@Table(name = "MailTemplate")
public abstract class MailTemplate extends MyAbstractEntity // AbstractEntity holds the common id field for all entities
{
   private String body;
   private String subject;
}
public class CustomMailTemplate extends MailTemplate implements java.io.Serializable {
    @Enumerated(EnumType.STRING)
    @Column(name = "templateType")
    private CustomTemplateType templateType;
}
public enum CustomTemplateType implements java.io.Serializable {
    OneTemplateType,
    AnotherTemplateType,
    AndAThirdOneTemplateType
}
We use JPA to persist our entities into an Oracle database. This is the controller structure which handles the various type of our templates:
public abstract class AbstractMailTemplateEditController<T extends MailTemplate, TYPE> {
    @Getter @Setter
    protected T entity;
    @Getter @Setter
    protected String subject;
    @Getter @Setter
    protected String body;
    @Getter @Setter
    protected TYPE templateType;    
}
public class CustomMailTemplateEditController extends AbstractMailTemplateEditController<CustomMailTemplate, CustomTemplateType> implements Serializable {
    // this is in a service which injected, it's just for the code example
    @PersistenceContext
    private EntityManager em;
    public void saveTemplate(){
        // let's assume now that our entity property exists
        entity.setBody(body);
        entity.setSubject(subject);
        entity.setTemplateType(templateType); // SOMETHING WRONG HAPPENS HERE!
        em.merge(entity);
    }
}
And here's the very strange part:
after a server restart, we can save our templates without any problem. After a 'T' time we can't save the templates, and we get the following error message:
java.lang.String cannot be cast to CustomTemplateType
I started to debug this and in the entity.setTemplateType(templateType); line I saw that templateType is a String, but this only happens after some time after the server restart. After a restart in the same place the templateType is a CustomTemplateType and I can save the template.
How can it happen and what can I do to fix this?
Update: Based on Tobias Liefke's answer I checked how we use this controller in the view and I found the following:
<h:selectOneMenu value="#{bean.templateType}" styleClass="form-control" id="template-type" >
    <f:selectItems value="#{bean.mailTemplateTypes}" var="item" itemLabel="#{msg[item]}" itemValue="#{item}" />
    <f:selectItem itemLabel="Special template" itemValue="#{null}" />
</h:selectOneMenu>
The #{bean.mailTemplateTypes} is a list of enums. So could it be a JSF bug? I mean after a while (days, weeks without server restart) JSF starts to pass the selected mailTemplateType as a String into the #{bean.templateType} value. Is it possible? I checked this Question: How to use enum values in f:selectItem(s) and based on BalusC's answer JSF has a builtin converter for enum. But that's the only part where we set the templateType variable.
 
     
    