1 Reply Latest reply on Jul 13, 2012 10:56 AM by Adam Warski

    org.hibernate.PropertyAccessException: could not set a field value by reflection setter of com.motohive.domain.model.post.VehiclePost.account

    Nabeel Ahmed Newbie

      HI, I am working on Jboss 7.1.1 and Hibernate 4.x

       

      I have the model mentioned below, i am trying to create, update and query entities, but getting the exception mentioned below

       

      @Entity

      @Table(name = "T_ACCOUNT")

      public class Account {

       

        @Column(name = "ACT_TITLE", nullable = false, length = 100)

                private String title;

       

                @OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.MERGE,

                                    CascadeType.REMOVE, CascadeType.PERSIST }, mappedBy = "account")

                private List<ItemPost> itemPosts;

       

                public Account() {

                }

       

           // getter setter for title here.

       

                public List<ItemPost> getItemPosts() {

                               return itemPosts;

                }

       

                public void setItemPosts(List<ItemPost> itemPosts) {

                          this.itemPosts = itemPosts;

                }

       

      }

       

       

      @MappedSuperclass

      public abstract class Post {

       

        @Id

        @GeneratedValue(strategy=GenerationType.IDENTITY)

        @Column(name="ID")

        protected Long id;

       

        @Column(name="TITLE", nullable=true, length=100)

                protected String title;

       

        public abstract Account getAccount();

                public abstract void setAccount(Account account);

      }

       

      @Entity

      @Table(name="T_ITEM_POST")

      @Inheritance(strategy=InheritanceType.JOINED)

      @DiscriminatorColumn(name = "IP_CLASSIFICATION", discriminatorType = DiscriminatorType.STRING, length = 20, columnDefinition = "IP_CLASSIFICATION")

      public class ItemPost extends Post {

       

                @Column(name = "IP_MAKE", nullable = false, length=30)

                protected String make;

       

        @ManyToOne(targetEntity=ItemPost.class, fetch=FetchType.EAGER)

                @JoinColumn(name="IP_ACT_ID", updatable=false, nullable=false)

                private Account account;

       

        public String getMake() {

                               return make;

                }

       

                public void setMake(String make) {

                               this.make = make;

                }

       

        @Override

                public Account getAccount() {

                               return account;

                }

       

                @Override

                public void setAccount(Account account) {

                               this.account = account;

                }

      }

       

      @Entity

      @Table(name="T_VEHICLE_POST")

      @DiscriminatorValue("VEHICLE")

      @PrimaryKeyJoinColumn(name="VP_ID", referencedColumnName="IP_ID")

      public class VehiclePost extends ItemPost {

       

        @Column(name="VP_MILAGE_COVERED")

                private Integer mileage;

         

        public Integer getMileage(){

              return mileage;

        }

         public void setMileage(Integer mileage){

            this.mileage = mileage;

        }

      }

       

       

       

      Caused by: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of com.motohive.domain.model.post.VehiclePost.account

                at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:150) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:707) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:371) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4232) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:168) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1006) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.Loader.doQuery(Loader.java:883) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.Loader.loadEntity(Loader.java:2058) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3697) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:439) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:420) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:251) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:954) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.internal.SessionImpl.get(SessionImpl.java:870) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:460) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:256) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:86) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:781) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:766) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:770) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:867) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

                ... 122 more

      Caused by: java.lang.IllegalArgumentException: Can not set com.motohive.domain.model.account.Account field com.motohive.domain.model.post.ItemPost.account to com.motohive.domain.model.post.VehiclePost

                at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164) [rt.jar:1.7.0_04]

                at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168) [rt.jar:1.7.0_04]

                at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) [rt.jar:1.7.0_04]

                at java.lang.reflect.Field.set(Field.java:680) [rt.jar:1.7.0_04]

                at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:138) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

                ... 148 more

       

      is there anything missing or wrong in it?

      Any help ?