首页 > 解决方案 > 加入后的规范无法找到具有给定名称的属性

问题描述

我有像这个实体模态这样的模态

可以通过一个登录或在Client其中切换登录Accounts。AAccount可以通过一个或另一个不同的方式登录Clients

关系Client - Accountmany-to-many,所以我创建了一个实体ClientAccount来存储客户和帐户 ID。

我的目标是输入keyword,程序可以通过branch nameonClientuser name, first name, last name, emailon 进行搜索Account

我使用specification join ClientAccount通过ClientAccount实体但程序找不到的属性Client,它只是找到的属性Account并抛出异常

java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [brandName] on this ManagedType

我尝试谷歌搜索和研究堆栈溢出,但无法找出根本原因并解决我的问题。请告诉我我错在哪里以及如何解决它。

这是我的代码:

    public class Client {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "brand_name", nullable = false, unique = true)
    private String brandName;
    
    @Column(name = "status")
    private String status;
    
    @Column(name = "date_expire", nullable = false)
    private Long dateExpire;
    
    // bi-directional many-to-one association to ClientAccount
    @OneToMany(mappedBy = "client")
    @JsonManagedReference
    private Set<ClientAccount> clientAccounts = new HashSet<ClientAccount>();
}
    public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(columnDefinition = "bytea", unique = true, nullable = true)
    private String email;
    @Column(columnDefinition = "text", name = "user_name")
    private String userName;
    @Column(columnDefinition = "text", name = "first_name")
    private String firstName;
    @Column(columnDefinition = "text", name = "last_name")
    private String lastName;
    
    // bi-directional many-to-one association to ClientAccount
    @OneToMany(mappedBy = "account")
    @JsonManagedReference
    private Set<ClientAccount> clientAccounts;
}
    public class ClientAccount {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "account_id")
    private Long accountId;
    @Column(name = "client_id")
    private Long clientId;
    
    // bi-directional many-to-one association to Client
    @ManyToOne
    @MapsId("clientId")
    @JsonBackReference
    @JoinColumn(name = "client_id", insertable = true, updatable = true)
    private Client client;

    // bi-directional many-to-one association to Account
    @ManyToOne
    @MapsId("accountId")
    @JsonBackReference
    @JoinColumn(name = "account_id", insertable = true, updatable = true)
    private Account account;
}
    @Override
    public Predicate toPredicate(Root<Client> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
        try {
        if (params.size() == 0) {
            return null;
        }
        List<Predicate> predicates = new ArrayList<>();
        
        
        Join<Client, ClientAccount> joinClient = root.join("clientAccounts");
        Join<ClientAccount, Account> joinAccount = joinClient.join("account");
        
        predicates.add(criteriaBuilder.equal(joinAccount.get("brandName"), "Stack over flow"));
        
        return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        
        }catch(Exception e) {
            log.error("Exception: ", e);
        }
        return null;
    }

标签: joinmany-to-manypredicatespecificationscriteriaquery

解决方案


推荐阅读