1

I have a SQL query like this: "Select UIProfileID from UserTable where UPPER(UserID) = UPPER('?1')".

I want to convert it to Spring JPA. I want to write getUIProfileId() and return Integer. But I don't know how to implement. Because User table doesn't have UIProfileId column that it was joined from UIProfileTable table. Please help me solve it. Currently, I have tables:

User.java

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "UserTable")
public class User {

  @Column(name = "UserID", length = 32, nullable = false)
  @Id
  private String name;

  @ManyToOne
  @JoinColumn(name = "DomainID", nullable = false)
  private Domain domain;

  @Column(name = "Password", length = 32, nullable = false)
  private String password;

  @ManyToOne
  @JoinColumn(name = "UIProfileID", nullable = false)
  private UIProfile uiProfile;

  @Column(name = "ResPerpage", nullable = false)
  private Integer resperpage;

  @Column(name = "DefaultTab")
  private Integer defaulttab;

  @ManyToOne
  @JoinColumn(name = "AdminProfile")
  private AdminProfiles adminProfile;

  @Column(name = "LanguageId")
  private Integer languageId;
}

UIProfile.java

@Entity
@Getter
@Setter
@Table(name = "UIProfileTable")
public class UIProfile implements Serializable {

  @Id
  @Column(name = "UIProfileID", length = 11, nullable = false)
  private Integer id;

  @Column(name = "UIProfileName", length = 32, nullable = false)
  private String name;

  @OneToMany(mappedBy = "id.uiProfile")
  private List<UIProfileTopLevel> topLevels;
}

UserRepository.java

public interface UserRepository extends Repository<User, String> {

  Optional<User> findOne(String name);

  @Query("Select UIProfileID from User where UPPER(UserID) = UPPER('admin')")
  Integer getUIProfileId();

}

1 Answer 1

1

You can try this:

@Query("SELECT u.uiProfile.id from User u where UPPER(u.name)=UPPER('admin')")
Integer getUIProfileId();

Here User is the domain class name and u is the reference of User. with u we will access User's field NOT the column name which are specified with @Column or @JoinColumn Ex : @JoinColumn(name = "UIProfileID", nullable = false).

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

8 Comments

Hi, Ajit Soman. It will be returning UIProfile object cannot be cast to java.lang.Integer
@ Ajit Soman. I want to get Id of UIProfileTable @Column(name = "UIProfileID", length = 11, nullable = false) private Integer id;.
@ Ajit Soman I think it will return a UIProfile why it arises List here.
Ok i have updated my answer . i have'nt noticed @ManyToOne Mapping
Hi, Ajit Soman UIProfile is the entity class. You were SELECT u.uiProfile it will return UIProfile. My problem is how to we get Id of UIProfileID column of UIProfileTable table from User entity(UserTable table)
|

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.