Home > Mobile >  Spring Boot Jpa, cannot map only foreign key
Spring Boot Jpa, cannot map only foreign key

Time:09-26

How can map only id(foreign key) with @ManyToOne? I don't want whole object to be mapped..

@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());
}

}

CodePudding user response:

Then you need to map it as a simple column instead of entity relationship:

@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;

You may remove @Column if the name of the property matches the column name.

  • Related