Ziel: Um eine @Entity, wo die ID ist eine zusammengesetzte Primärschlüssel mit @Embeveted.JPA-Compound-Primärschlüssel mit @EmbededId
Problem: Aufgrund meiner aktuellen Implementierung, erhalte ich folgendes Ergebnis:
[
{
"id": 1,
"name": "Recipe 1",
"instruction": "Test Instruction",
"note": "Note 1",
"show": true,
"createDate": null,
"modify_date": null,
"ingredient": [
{},
{}
]
}
]
aber ich möchte diese haben: zu
[
{
"id": 1,
"name": "Recipe 1",
"instruction": "Test Instruction",
"note": "Note 1",
"show": true,
"createDate": null,
"modify_date": null,
"ingredient": [
{ingredient_id: 1,
amount: 10},
{ingredient_id: 2,
amount: 20}
]
}
]
Kann jemand bitte helfen Sie mir zu sehen, Ich habe in meiner recipeIngredient Klasse falsch gemacht? Danke im Voraus.
Die folgenden sind meine Implementierung:
RecipeIngredientId.java
@Embeddable
public class RecipeIngredientId implements Serializable {
@Column(name = "recipe_id", nullable = false)
private int recipeId;
@Column(name = "ingredient_id", nullable = false)
private int ingredientId;
public RecipeIngredientId() {}
public RecipeIngredientId(int recipeId, int ingredientId) {
this.recipeId = recipeId;
this.ingredientId = ingredientId;
}
}
RecipeIngredient.java
@Entity
@Table(name = "recipe_ingredient")
public class RecipeIngredient implements Serializable
{
@EmbeddedId
private RecipeIngredientId id;
@ManyToOne
@JoinColumn(name="ingredient_id", insertable = false, updatable = false)
private Ingredient ingredient;
@ManyToOne
@JoinColumn(name = "recipe_id", insertable = false, updatable = false)
private Recipe recipe;
private double amount;
public RecipeIngredient() {}
public RecipeIngredient(Recipe recipe, Ingredient ingredient, double amount){
this.recipe = recipe;
this.ingredient = ingredient;
this.amount = amount;
}
}
Recipe.java:
@Entity
public class Recipe {
private int id;
@NotNull
private String name;
private String instruction;
private String note;
@NotNull
private boolean show;
@CreationTimestamp
@Temporal(TemporalType.DATE)
@Column(name = "create_date")
private Date createDate;
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "modify_date")
private Date modify_date;
private Set<RecipeIngredient> recipeIngredients;
public Recipe() {}
public Recipe(String name, String instruction, String note, boolean show) {
this.name = name;
this.instruction = instruction;
this.note = note;
this.show = show;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInstruction() {
return instruction;
}
public void setInstruction(String instruction) {
this.instruction = instruction;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public boolean isShow() {
return show;
}
public void setShow(boolean show) {
this.show = show;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getModify_date() {
return modify_date;
}
public void setModify_date(Date modify_date) {
this.modify_date = modify_date;
}
@OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL)
public Set<RecipeIngredient> getIngredient() {
return recipeIngredients;
}
public void setIngredient(Set<RecipeIngredient> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
}
Ingredient.java
@Entity
public class Ingredient {
private int id;
@NotNull
@Column(unique=true)
private String name;
private Set<RecipeIngredient> recipeIngredients;
public Ingredient() {}
public Ingredient(String name) {
this.name = name;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy = "ingredient", cascade = CascadeType.ALL)
public Set<RecipeIngredient> getRecipeIngredients() {
return recipeIngredients;
}
public void setRecipeIngredients(Set<RecipeIngredient> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
}
Sie haben 'RecipeIngredientId', aber nicht' RecipeIngredient' eingeschlossen. Außerdem müssen Sie entweder alle Felder oder alle Getters kommentieren, nicht die Mischung. es sei denn, Sie verwenden die "Access" -Anmerkung usw. –
Hallo @BrianVosburgh, Entschuldigung, ich habe gerade festgestellt, dass es eine fehlende Klasse gibt, die ich vergessen habe hinzuzufügen. Bitte beachten Sie das Update – SL07
Ich habe eine Lösung, die funktionieren kann, aber es wird es wirklich schwer für Sie, das JSON richtig deserialisieren und es in der Datenbank beibehalten, wenn das ist, was Sie beabsichtigen. Der Serialisierungsteil wird jedoch einfach sein. Das heißt, vorausgesetzt, Sie verwenden Jackson dafür. – coladict