0

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!

3 Answers 3

1

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`")

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, the two methods you suggest did work for me. I kept the original two methods, but then I just passed in an Object to both and that worked also. Thanks!
@tjholmes66 ya the object level will also work depends on the equals implementation of User
0

user is a reserved keyword in SQL.

Try by changing it to something else and see if the error goes away.

Comments

0

Yep, as described by @MindhunMohan I changed the definitions and it worked.

The methods I have now are as follows:

@Repository("organizationMemberRepo")
public interface OrganizationMemberRepository extends JpaRepository<OrgMember, Long>
{
    List<OrgMember> findByUser(User user);
    List<OrgMember> findByOrganization(Organization org);

    List<OrgMember> findByUserId(Long userId);
    List<OrgMember> findByOrganizationId(Long orgId);
}

These methods worked now, and the unit tests for these now work also.

Thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.