我有一个带有elementCollection的实体:
@Entity
class Customer {
@Column(name = "id")
int id;
String name;
@ElementCollection
@CollectionTable(
joinColumns = @JoinColumn(name = "customer_id")
Set<Address> addresses;
}
@Embeddable
class Address {
@Column(name = "address_id")
String id;
String data;
}当然,数据库上的匹配数据是:
databaseChangeLog:
- changeSet:
id: 1
author: me
changes:
- createTable:
tableName: customer
columns:
- column:
name: id
type: bigint
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: name
type: varchar(255)
- createTable:
tableName: address
columns:
- column:
name: customer_id
type: bigint
constraints:
nullable: false
- column:
name: address_id
type: varchar(30)
- column:
name: data
type: varchar(255)
- addForeignKeyConstraint:
baseTableName: address
baseColumnNames: customer_id
referencedTableName: customer
referencedColumnNames: id
constraintName: fk_customer_id
- addUniqueConstraint:
tableName: address
name: unique_address_id
columnNames: address_id注意: address id实际上是一个字符串!
插入和阅读作品。但我的目标是通过Customer选择Address.id。
我的目标是:
interface CustomerJpaRepository extends JpaRepository<Customer, Long> {
XXXX // <-- insert solution here
}我对XXXX的看法:
Customer findByAddressWithId(String addressId);当然,这是行不通的,因为语法是错误的。但我甚至找不到对ElementCollections的完整语法的描述。
@Query(value = "select c from Customer c join Address a where a.id = '?1'")
Customer findByStreet(String addressId);因为找不到a.id,所以这个不能工作。
不幸的是,上述任何一项都不起作用。有谁有XXXX的解决方案吗?
发布于 2020-11-04 10:29:20
可嵌入类表示没有自己的持久标识。实体类将具有自己的持久标识。Address嵌入类的实例在这里共享拥有它的实体的标识,即Customer。由于可嵌入类仅作为另一个实体的状态存在,如果需要为Addressid提供持久id Address,请考虑将地址设置为实体。否则,也要检查持久提供程序特定的注释,如CollectionId CollectionId.html,并将addressid定义为collectionid。
https://stackoverflow.com/questions/64677832
复制相似问题