org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of org.catablog.entity.IdEntity.entityId

尝试做一个SSH+spring security+blazeDS+flex的例子,修改用户信息的时候报了这么个怪异常

Java代码
  1. org.hibernate.PropertyAccessException:IllegalArgumentExceptionoccurredcallinggetteroforg.catablog.entity.IdEntity.entityId
  2. atorg.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:195)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
  3. atorg.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:206)[hibernate-core-3.3.1.GA.jar:3.3.1.GA]
  4. .....
  5. Causedby:java.lang.IllegalArgumentException:java.lang.ClassCastException@156f770
  6. ......


我操作的用户类定义如下

Java代码
  1. @Entity
  2. //表名与类名不相同时重新定义表名.
  3. @Table(name="USERS")
  4. //默认的缓存策略.
  5. @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
  6. publicclassUserextendsIdEntity{
  7. privateStringloginName;
  8. privateStringpassword;//为简化演示,使用明文保存密码.
  9. privateStringname;
  10. privateStringemail;
  11. privateSet<Role>roles=newLinkedHashSet<Role>();//有序的关联对象集合.
  12. publicStringgetLoginName(){
  13. returnloginName;
  14. }
  15. publicvoidsetLoginName(StringloginName){
  16. this.loginName=loginName;
  17. }
  18. publicStringgetPassword(){
  19. returnpassword;
  20. }
  21. publicvoidsetPassword(Stringpassword){
  22. this.password=password;
  23. }
  24. publicStringgetName(){
  25. returnname;
  26. }
  27. publicvoidsetName(Stringname){
  28. this.name=name;
  29. }
  30. publicStringgetEmail(){
  31. returnemail;
  32. }
  33. publicvoidsetEmail(Stringemail){
  34. this.email=email;
  35. }
  36. @Fetch(FetchMode.SUBSELECT)
  37. @LazyCollection(LazyCollectionOption.FALSE)
  38. @ManyToMany
  39. //多对多定义.
  40. @JoinTable(name="USERS_ROLES",joinColumns={@JoinColumn(name="USER_ID")},inverseJoinColumns={@JoinColumn(name="ROLE_ID")})
  41. //集合按id排序.
  42. @OrderBy("entityId")
  43. //集合中对象的id的缓存.
  44. @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
  45. publicSet<Role>getRoles(){
  46. returnroles;
  47. }
  48. publicvoidsetRoles(Set<Role>roles){
  49. this.roles=roles;
  50. }
  51. //非持久化属性.
  52. @Transient
  53. publicStringgetRoleNames()throwsException{
  54. returnReflectionUtils.fetchElementPropertyToString(roles,"name",",");
  55. }
  56. //非持久化属性.
  57. @Transient
  58. @SuppressWarnings("unchecked")
  59. publicList<Long>getRoleIds()throwsException{
  60. returnReflectionUtils.fetchElementPropertyToList(roles,"id");
  61. }
  62. }


entityId是我所有VO的公共父类,把ID的定义抽象出来了,从上面的异常消息可以看出是参数类型转换的时候报了类型转换非法异常,抛异常的是Hibernate的BasicPropertyAccessor,这可是Hibernate的基本组件,我在修改用户信息之前就已经查询过一次了,如果是我的entityId的类型有问题,那查询的时候就会调用BasicPropertyAccessor的get方法,那个时候就该报错了,也不该撑到这会儿。Debug了一下才发现User实例里的Roles集合里面全是ASObject?!怪不得,ASObject当然没有getter setter了。
刚开始纳闷儿怎么我的actionScript User类传到java这边就能转成对应的User类,User里的Role集合一过来就变成了ASObject?后来才明白,ActionScript不支持泛型的,Role类放进User里的Roles ArrayCollection的时候类型就没了,传到java那边儿当然识别不出Role的类型,就只能用ASObject替代了,这还真是个问题,延迟加载已经够让我头大的了,这个怎么说也得搞定!

你可能感兴趣的:(Hibernate)