I am using a Comparator to sort a List<Entity> by it's property (of type Date) in viewscoped managed bean.
The problem is that I keep getting the ClassCastException as follows -
java.lang.ClassCastException: pkg.db.BaseArStatus cannot be cast to pkg.db.BaseArStatus.
Why is it not able to cast BaseArStatus to BaseArStatus? Is it because BaseArStatus is an Entity?
The problem is really strange to me because I am not getting the exception every time. Most of the it works fine (runs without any problem) when build and deploy the application but sometimes (even though I am doing the same thing - build and deploy) it fails at runtime with the ClassCastException.
Why is this happening only sometimes and not all the time? Is it because I am using it in managed bean?
This is how the managed bean looks like -
@ManagedBean
@ViewScoped
public class MyBean {
@PersistenceContext(unitName = "myPU")
private EntityManager em;
public void myMethod() {
    List<BaseArStatus> basList = this.fetchAllBaseArStatus();
    Collections.sort(basList, new Comparator<BaseArStatus>() {
        @Override
        public int compare(BaseArStatus o1, BaseArStatus o2) {
            return o1.getMonthDate().compareTo(o2.getMonthDate());
        }
    });
//...
And the entity BaseArStatus -
@Entity
@Table(name = "base_ar_status")
@NamedQueries({
    @NamedQuery(name = "BaseArStatus.findAll", query = "SELECT b FROM BaseArStatus b")})
public class BaseArStatus implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "month_date")
    @Temporal(TemporalType.DATE)
    private Date monthDate;
    @Basic(optional = false)
    @NotNull
    @Column(name = "ar_count")
    private double arCount;
    @Size(max = 50)
    @Column(name = "user_id")
    private String userId;
    @Column(name = "last_update_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastUpdateDate;
    public BaseArStatus() { }
//...
 
     
     
     
    