Reputation: 177
I have an entity with an 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;
}
of course the matching data on the db:
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
Note: address id is really a String!
Inserting, and reading works. But my goal is to select Customer
by Address.id
.
My goal:
interface CustomerJpaRepository extends JpaRepository<Customer, Long> {
XXXX // <-- insert solution here
}
My ideas for XXXX:
Customer findByAddressWithId(String addressId);
Of course, this does not work, because the syntax is wrong. But I can't even find a description of the complete syntax for ElementCollections.
@Query(value = "select c from Customer c join Address a where a.id = '?1'")
Customer findByStreet(String addressId);
This one does not work because a.id
can't be found.
Unfortunately, none of the above work.
Does anybody have any solution for XXXX
?
Upvotes: 0
Views: 1323
Reputation: 748
Embeddable classes representation don’t have a persistent identity of their own. Entity classes will have persistent identity of thier own.
Instances of the Address
embeddable class here share the identity of the entity that owns it, that is Customer
.
As embeddable classes exist only as the state of another entity, consider making Address as Entity if you require a persistent id Addressid
for Address
.
Otherwise, also check persistent provider specific annotations like CollectionId
CollectionId.html and define addressid as a collectionid
.
Upvotes: 1