Search code examples
hibernatespring-data-jpajpa-2.0

java.lang.IllegalArgumentException: Can not set java.lang.Long field com.mypackage.User.id to java.lang.Long


I am using Java 8 with JPA2/Hibernate5 and MySQL.

  tables
 +--------+    +----------------+    +--------------+ 
 | User   |    |  OrgMember     |    | Organization |
 +--------+    +----------------+    +--------------+ 
 | ID     |    | ID             |    |  ID          |
 | NAME   |    | userId         |    +--------------+ 
 +--------+    | orgId          |
               +----------------+    

Here is the join table definition

@Entity         
@Table(name = "org_members")
public class OrgMember implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userId", referencedColumnName = "id")
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "orgId", referencedColumnName = "id")
    private Organization organization;

Here is the definition for the User table:

@Entity
@Table(name = "user")
public class User implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

Finally, here is the definition for the Organization table

@Entity
@Table(name = "organization")
public class User implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

I am using the Spring 5 data repository for the OrgMemberRepository

 @Repository("organizationMemberRepo")
 public interface OrganizationMemberRepository extends JpaRepository<OrgMember, Long>
 {
     List<OrgMember> findByUser(Long userId);
     List<OrgMember> findByOrganization(Long orgId);
 }

And it is the unit tests that are breaking:

 @Test
 public void testFindByOrganization()
 {
     Long orgId = 1L;
     List<OrgMember> orgMemberList = 
             organizationMemberRepository.findByOrganization(orgId);
     assertNotNull(orgMemberList);
     assertEquals(true, orgMemberList.size() > 0);
 }

 @Test
 public void testFindByUser()
 {
     Long userId = 3L;
     List<OrgMember> orgMemberList = organizationMemberRepository.findByUser(userId);
     assertNotNull(orgMemberList);
    assertEquals(true, orgMemberList.size() > 0);
 }

I am sure this should be a simple task, and I am sure there is a simple fix. I'll play around with this a bit more, and make sure I am looking for the right things.

Thanks!


Solution

  • Did you tried like below

    @Repository("organizationMemberRepo")
     public interface OrganizationMemberRepository extends JpaRepository<OrgMember, Long>
     {
         List<OrgMember> findByUserId(Long userId);// or findByUser_Id
         List<OrgMember> findByOrganizationId(Long orgId);// or findByOrganization_Id
     }
    

    Because In this case the query generation during the interface to implementation will be User.Id and Organization.Id and there the Long argument will work as expected.

    And for the user keyword issue that may come, try to change it from @Table(name = "user") to @Table(name = "`user`")