我有一个小项目需要修改Spring,其中有两个实体具有一对多的关联:1 Restaurant
-> N Dishes
。为此,我有以下PostgreSQL模式:
create table if not exists restaurants (
restaurant_id uuid primary key,
name varchar(512) not null,
description varchar(1024) not null,
address varchar(512) not null,
photo_url varchar(1024)
);
create table if not exists dishes (
dish_id uuid primary key,
name varchar(512) not null,
description varchar(1024),
photo_url varchar(1024),
restaurant_id uuid references restaurants(restaurant_id) not null,
price int not null check (price > 0)
);
与下列联合行动机构合作:
@Entity
@Table(name = "restaurants")
class Restaurants(
@Id
var restaurantId: UUID,
var name: String,
var description: String,
var photoUrl: String?,
) {
@OneToMany(mappedBy = "restaurant")
@JoinColumn(name = "restaurant_id", nullable = false)
var dishes: MutableList<Dishes> = mutableListOf()
}
@Entity
@Table(name = "dishes")
class Dishes(
@Id
var dishId: UUID,
var name: String,
var description: String,
var photoUrl: String?,
var price: Int,
@ManyToOne(optional = false)
@JoinColumn(name = "restaurant_id", nullable = false)
var restaurant: Restaurants
)
我将RestaurantsRepository
定义为:
interface RestaurantsRepository: R2dbcRepository<Restaurants, UUID> {
fun findByRestaurantId(restaurantId: UUID): Mono<Restaurants>
}
我遇到的问题是,当我调用findByRestaurantId
时,我有以下例外:
org.springframework.r2dbc.BadSqlGrammarException: executeMany; bad SQL grammar [SELECT restaurants.restaurant_id, restaurants.name, restaurants.description, restaurants.photo_url, restaurants.dishes FROM restaurants WHERE restaurants.restaurant_id = $1]; nested exception is io.r2dbc.postgresql.ExceptionFactory$PostgresqlBadGrammarException: [42703] column restaurants.dishes does not exist
at org.springframework.r2dbc.connection.ConnectionFactoryUtils.convertR2dbcException(ConnectionFactoryUtils.java:235) ~[spring-r2dbc-5.3.21.jar:5.3.21]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
为什么@OneToMany
字段包含在SQL查询中?
发布于 2022-07-10 14:29:22
您正在尝试将Spring R2DBC (R2dbcRepository)与JPA注释结合使用。这是行不通的:这是两种不同的技术。R2DBC不支持@ManyToOne
或@JoinColumn
,因此只忽略注释。
https://stackoverflow.com/questions/72928978
复制相似问题