首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring数据:查询ElementCollection

Spring数据:查询ElementCollection
EN

Stack Overflow用户
提问于 2020-11-04 09:58:53
回答 1查看 983关注 0票数 0

我有一个带有elementCollection的实体:

代码语言:javascript
复制
@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;
}

当然,数据库上的匹配数据是:

代码语言:javascript
复制
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

我的目标是:

代码语言:javascript
复制
interface CustomerJpaRepository extends JpaRepository<Customer, Long> {
  XXXX // <-- insert solution here
}

我对XXXX的看法:

  1. 使用spring数据JPA:
代码语言:javascript
复制
  Customer findByAddressWithId(String addressId);

当然,这是行不通的,因为语法是错误的。但我甚至找不到对ElementCollections的完整语法的描述。

  1. 使用自定义查询:
代码语言:javascript
复制
  @Query(value = "select c from Customer c join Address a where a.id = '?1'")
  Customer findByStreet(String addressId);

因为找不到a.id,所以这个不能工作。

不幸的是,上述任何一项都不起作用。有谁有XXXX的解决方案吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-04 10:29:20

可嵌入类表示没有自己的持久标识。实体类将具有自己的持久标识。Address嵌入类的实例在这里共享拥有它的实体的标识,即Customer。由于可嵌入类仅作为另一个实体的状态存在,如果需要为Addressid提供持久id Address,请考虑将地址设置为实体。否则,也要检查持久提供程序特定的注释,如CollectionId CollectionId.html,并将addressid定义为collectionid

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64677832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档