Hi, I am dusting off some old prototype code that last worked on Hibernate JPA1.0, Glassfish 3.x to Hibernate 4.3.7-Final, Glassfish 4 and JPA2.1. I am seeing an odd problem where an @Entertity class cannot be cast to it's interface (this previously worked for many many months).
Here is a small example:
@Entity @Table(name = "accounts") public class UserAccountBean implements Serializable,UserAccount {
/** * */ private static final long serialVersionUID = 6603302682644224769L;
@Id @SequenceGenerator(name = "usersIdSequence", sequenceName = "users_id_seq", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "usersIdSequence") @Column(name = "id", nullable = false) private Integer id;
... various fields and accessors etc. }
public interface UserAccount extends Serializable {
Employee getEmployee(); Customer getCustomer();
.. more accessors
}
And the calling code (showing problem):
@Override public UserAccount authenticateUser(final String username, final String password) { Query query = em.createQuery("select u from UserAccountBean u where u.username = :username"); query.setParameter("username", username); List<UserAccount>userList = query.getResultList();
boolean test = userList != null; test = userList.size() == 1; // this works and shows 1 entry was retrieved - using a debugger I can see it is correct
test = userList.get(0).getPassword().equals(password); // this goes BANG! on the get(0) where it performs a cast returning from the container
}
Warning: A system exception occurred during an invocation on EJB UserAccountServiceBean, method: public <package>.UserAccount <package>.UserAccountServiceBean.authenticateUser(java.lang.String,java.lang.String) Warning: javax.ejb.EJBException
Caused by: java.lang.ClassCastException: <package>.UserAccountBean cannot be cast to <package>.UserAccount
Any ideas? has something changed in this area? looks pretty simple and standard to me...
</scratching head>
|