0

Consider this class:

@Entity
@Table(name = "USER_TABLE")
@Inheritance(strategy = InheritanceType.JOINED)
public class User {

    @Id
    @GenericGenerator(name = "system-uuid", strategy = "uuid2")
    @GeneratedValue(generator="system-uuid")
    @Column(name = "id", unique = true)
    @Type(type= "org.hibernate.type.PostgresUUIDType")
    private UUID id;

    @Column(name = "USERNAME")
    private String userName;
}

and its simple child:

@Entity
@Table(name = "CUSTOMER")
public class Customer extends User{
        //some extra fields
}

I have another class having relation to user:

@Entity
@Table(name = "USER_ACTIVITY")
public class UserActivity {

    @ManyToOne
    @JoinColumn(name = "USERID")
    private User user;

    //id and other fields
}

This is the tricky part. I want to fetch all objects of type UserActivity which relates to a "Customer". I tried something like this:

public interface UserActivityRepository extends PagingAndSortingRepository<UserActivity,UUID> {
    @Query("select u from UserActivity u join Customer.id")
    List<UserActivity> findAllByUserIsNotNullOrderByCreatedDesc(Pageable pageable);
}

But hibernate tells me that I've specified an invalid path.

Could you help me on this?

0

2 Answers 2

1

You can explicitly join with Customer or filter by User ids that belong to Customer ids:

  1. select u from UserActivity u, Customer c where u.user.id = c.id
  2. select u from UserActivity u where u.user.id in (select id from Customer)

Pick any you like better, as optimizers in modern databases will optimize both queries so that performance is the same in both cases.

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

Comments

0

Use @DiscriminatorColumn(name = "TYPE") in your User entity.

@Entity
@Table(name = "USER_TABLE")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE")
@DiscriminatorValue("USER")
public class User {

    ...

}

Define a @DiscriminatorValue("CUSTOMER") in your Customer entity

@Entity
@Table(name = "CUSTOMER")
@DiscriminatorValue("CUSTOMER")
public class Customer extends User {

    ...

}

Query your repository like so

@Query("select u from UserActivity u join u.user user where TYPE(user) = 'CUSTOMER'")
List<UserActivity> findAllByUserIsNotNullOrderByCreatedDesc();

Note: @DiscriminatorValue annotation is not mandatory. JPA infers the column name based on the entity type.

4 Comments

Type takes the entity name, not the DiscriminatorValue. stackoverflow.com/questions/3765948/…
Yes it does. That's why TYPE(user). user is the mapped entity.
That's great that a provider supports it, but JPA requires the entity class as the input parameter, separating the values used in the discriminator column or even the mechanism used in determining classes from the actual type you are looking for. Your answer about the type is correct, but you don't need to specify the Discriminator annotations at all and just use the class.
Yeah i tested the above without @DiscriminatorValue("USER") and its still running fine. Thanks for the update @Chris

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.