lundi 20 avril 2015

Spring mapping using hibernate

I am joining two table using one to many relationship

my first table is role and second is roleCompany. where role_id is reference key in roleCompany table.

But when I am writting query to get all companies for specific id it showing me following error

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/myproject] threw exception [Request processing failed; nested exception is org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.test.myproject.domain.entity.RoleEntity.id] with root cause
java.lang.IllegalArgumentException: Can not set java.lang.Long field com.test.myproject.domain.entity.RoleEntity.id to java.lang.Long
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)

My RoleEntity Class is :

@Entity
@Table(name = "roles")
public class RoleEntity {

    @Id
    @Getter
    @Setter
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Long id;

    @Getter
    @Setter
    @Column(name = "role", nullable = false)
    private String role;

    @Getter
    @Setter
    @OneToMany(mappedBy = "roles")
    List<RoleCompanyEntity> companyentities;    
}

and my RoleCompanyEntity is

@NamedQueries({
    @NamedQuery(name = RoleCompanyEntity.FIND_ROLE_COMPANY_BY_ROLE_ID, query = "select r.companyId, r.companyName from RoleCompanyEntity r where r.roles  =:roleId")
})


@Entity
@Table(name = "role_company")
public class RoleCompanyEntity {

    public static final String FIND_ROLE_COMPANY_BY_ROLE_ID = "findRoleCompanyByRoleId";

    @Id
    @Getter
    @Setter
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "company_id", unique = true, nullable = false)
    private Long companyId;

    @Getter
    @Setter
    @Column(name = "company_name", unique = true, nullable = false)
    private String companyName;


    @Getter
    @Setter
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="role_id", nullable=false)
    private RoleEntity roles;

}

Aucun commentaire:

Enregistrer un commentaire