join - 加入后的规范无法找到具有给定名称的属性
问题描述
我有像这个实体模态这样的模态
可以通过一个登录或在Client
其中切换登录Accounts
。AAccount
可以通过一个或另一个不同的方式登录Clients
。
关系Client - Account
是many-to-many
,所以我创建了一个实体ClientAccount
来存储客户和帐户 ID。
我的目标是输入keyword
,程序可以通过branch name
onClient
或user name
, first name
, last name
, email
on 进行搜索Account
。
我使用specification
join
Client
和Account
通过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;
}
解决方案
推荐阅读
- postgresql - 如何将 heroku postgres 备份文件传输到 docker postgres 容器(文件放在哪里?)
- matplotlib - 如何从其他脚本绘制图形实例
- javascript - 将 2 个不同的不和谐通道相互连接
- android - ViewModel 检索空列表,但不应为空 (mutableStateOf)
- microsoft-graph-api - Microsoft Graph API:在创建私人频道时无法与所有者一起添加私人频道成员
- reactjs - React Material UI Select native - 未找到值时不显示为空
- flutter - 在颤动中为DataTable的DataRows添加边距
- amazon-ec2 - 消息:[/Resources/AWSEBCloudwatchAlarmHigh/Type/Dimensions/0/Value/Ref] 模板中不允许使用“null”值
- php - 在 WooCommerce 中显示不包括产品类别的订单项目名称
- python - 将数组从 c++ 返回到 python