Spring 启动 Jpa,不能只映射外键

Spring Boot Jpa, cannot map only foreign key

如何将id(外键)映射到@ManyToOne?我不想映射整个对象..

@Id
private UUID id;
private String name;
private double price;
private String image;
private String ingredients;
private String description;

@ManyToOne
private CategoryImpl category;

public FoodDto mapToDto() {
    return new FoodDtoImpl(this.getId(), this.getName(), this.getPrice(), this.getImage(),
            this.getIngredients(), this.getDescription(),this.getCategory());
}

public void generateId(){
    this.setId(UUID.randomUUID());
}

}

那么你需要将其映射为一个简单的列而不是实体关系:

@Id
private UUID id;
private String name;
private double price;
private String image;
private String ingredients;
private String description;
@Column(name="CATEGORY_ID". // name is just an example
private UUID categoryId;

如果 属性 的名称与列名匹配,您可以删除 @Column