2017-08-12 67 views
1

我有一個像這樣的SQL查詢: "Select UIProfileID from UserTable where UPPER(UserID) = UPPER('?1')"如何將SQL查詢轉換爲Spring JPA查詢

我想將其轉換爲Spring JPA。 我想寫getUIProfileId()並返回Integer。但我不知道如何實施。由於用戶表沒有UIProfileId列,因此它已從UIProfileTable表中加入。請幫我解決它。 目前,我有表:

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

你可以試試這個:

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

這裏User是域名類名和u是用戶的參考。與u我們將訪問用戶的字段而不是的列名是用@Column or @JoinColumn指定的例如:@JoinColumn(name = "UIProfileID", nullable = false)

+0

嗨,Ajit Soman。它將返回UIProfile對象,不能轉換爲java.lang.Integer –

+0

@ Ajit Soman。我想獲得UIProfileTable @Column的ID(name =「UIProfileID」,長度= 11,可空= false) private Integer id ;. –

+0

我已編輯我的答案 –