@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "recipe_id", "ingredient_id" }))
public class RecipeIngredient {
public static enum Unit {
KILOGRAM, GRAM, MILLIGRAM, OUNCE, POUND, MILLILITER, LITER,
TEASPOON, TABLESPOON, FLUID_OUNCE, CUP, PINT, QUART, GALLON,
PIECE, SLICE, CLOVE, STICK, CAN, UNSPECIFIED,
BOTTLE, PACK, DASH, PINCH, DROP;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long recipeIngredientId;
@ToString.Exclude
@ManyToOne(optional = false)
@JoinColumn(
name = "recipe_id",
referencedColumnName = "recipeId",
nullable = false,
foreignKey = @ForeignKey(name = "recipe_id_fkey")
)
private Recipe recipe;
@ToString.Exclude
@ManyToOne(optional = false, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(
name = "ingredient_id",
referencedColumnName = "ingredientId",
nullable = false,
foreignKey = @ForeignKey(name = "ingredient_id_fkey")
)
private Ingredient ingredient;
@Enumerated(EnumType.STRING)
private Unit unit;
@NotNull
@Positive
@Column(nullable = false)
private Double quantity;
}
< /code>
@Test
public void shouldFailToDeleteIngredient(){
Recipe recipe = getRecipe();
testEntityManager.persist(recipe);
testEntityManager.flush();
assertThrows(PersistenceException.class, () -> {
ingredientRepository.deleteAll();
ingredientRepository.flush();
testEntityManager.flush(); // Forces DB constraint to be evaluated
});
}
< /code>
- Versuchte manuell in den Postgres DB zu delet. geworfen. < /li>
Es gibt fremde wichtige Einschränkungen in meiner Posgres-Tabelle, wenn ich den Server selbst ausführe- ich würde also annehmen, dass es für H2 das gleiche ist? Unsicher.