私のデータベースには3つのテーブルがあります。。私のApplication.java
は正しい順序でデータファイルを解析するので、テーブル上では次のテーブルに役立ちます。私の問題は、SecondaryFoodDescription
クラスのdatabankIDフィールドを設定するときに、Data truncation: Data too long for column 'food' at row 1
が得られるということです。私は同じrepository.saveメソッドを使って、クラス内のfoodGroupフィールドに値を設定するので、これは驚くべきことです(少なくとも私にとって)。 Food
。春のデータ "行の列が長すぎます"
編集:通常、Data truncation: Data too long for column 'foodid' at row 1
のようなエラーは、私が挿入しようとしているものがフィールドには大きすぎることを意味します。しかし、エンティティを挿入しようとしています。どのように私のエンティティは大きすぎることができますか?
誰でも私が間違っていることを発見できますか?
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CommandLineRunner demo(FoodGroupRepository foodGrouprepository, FoodRepository foodRepository,
SecondaryFoodInformationRepository secondaryFoodInformationRepository,
) {
return (args) -> {
FoodGroupParser foodGroupParser = new FoodGroupParser();
FoodGroup foodGroup;
Map<String, String> foodGroupMap = foodGroupParser.returnFoodGroupData();
for (Map.Entry<String, String> entry : foodGroupMap.entrySet()) {
foodGroup = new FoodGroup(entry.getKey(), entry.getValue());
foodGrouprepository.save(foodGroup);
}
FoodDescriptionParser foodDescriptionParser = new FoodDescriptionParser();
List<String[]> listOfFooods = foodDescriptionParser.returnFoodDescriptionData();
Food foodDescription;
SecondaryFoodDescription secondaryFoodDescription;
for (String[] foodItem : listOfFooods) {
foodDescription = new Food(foodItem[0],foodGrouprepository.findOne(foodItem[1]),foodItem[2],foodItem[4],foodItem[5],foodItem[8]);
secondaryFoodDescription = new SecondaryFoodDescription();
foodRepository.save(foodDescription);
secondaryFoodDescription = new SecondaryFoodDescription(foodRepository.findOne(foodItem[0]),foodItem[6],foodItem[7],foodItem[9],foodItem[10],foodItem[11]);
secondaryFoodInformationRepository.save(secondaryFoodDescription);
}
};
}
}
リポジトリインターフェース:
@RepositoryRestResource
public interface FoodGroupRepository extends PagingAndSortingRepository<FoodGroup,String> {
}
@RepositoryRestResource
public interface FoodRepository extends PagingAndSortingRepository<Food, String>{
List<Food> findByLongDescription(@Param("longDescription") String name);
}
@RepositoryRestResource
public interface SecondaryFoodInformationRepository extends PagingAndSortingRepository<SecondaryFoodDescription,Long> {
}
食品グループエンティティ:
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
@Entity
@Table(name = "FOOD_GROUP")
public class FoodGroup implements Serializable{
public FoodGroup(){}
public FoodGroup(String id, String foodName){
this.setFoodGroupCode(id);
this.setFoodName(foodName);
}
@Id
@NotNull
@Size(min = 4,max = 4)
private String foodGroupCode;
@NotNull
@Size(max=60)
private String foodName;
public String getFoodGroupCode() {
return foodGroupCode;
}
public void setFoodGroupCode(String foodGroupCode) {
this.foodGroupCode = foodGroupCode;
}
public String getFoodName() {
return foodName;
}
public void setFoodName(String foodName) {
this.foodName = foodName;
}
}
食品エンティティ:
import javax.persistence.*;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "FOOD_DESCRIPTION")
public class Food implements Serializable {
@Id
@NotNull()
@Column(name = "NUTRITION_DATA_BANK_ID")
@Size(min = 5, max = 5)
private String nutritionDatabankID;
private FoodGroup foodGroupID;
@OneToMany(mappedBy = "foodDescription", cascade = CascadeType.ALL)
private final Set<NutritionDataForSpecificFood> ndesc = new HashSet<>();
public Set<NutritionDataForSpecificFood> getNutrients() {
return Collections.unmodifiableSet(this.ndesc);
}
public void addFood(NutritionDataForSpecificFood specificFood) {
specificFood.setFoodDescription(this);
this.ndesc.add(specificFood);
}
@NotNull
@Column(name = "LONG_DESCRIPTION")
@Size(max = 200)
private String longDescription;
@Size(max = 100)
@Column(name = "COMMON_NAME")
private String commonName;
@Size(max = 65)
@Column(name = "MANUFACTURER_NAME")
private String manufacturerName;
@Size(max = 65)
@Column(name = "SCIENTIFIC_NAME")
private String scientificNameOfFood;
public Food() {
}
public Food(String nutritionDatabankID, FoodGroup foodGroupID, String longDescription, String commonName,
String manufacturerName, String scientificNameOfFood) {
this.nutritionDatabankID = nutritionDatabankID;
this.foodGroupID = foodGroupID;
this.longDescription = longDescription;
this.commonName = commonName;
this.manufacturerName = manufacturerName;
this.scientificNameOfFood = scientificNameOfFood;
}
public FoodGroup getFoodGroupID() {
return foodGroupID;
}
public void setFoodGroupID(FoodGroup foodGroupID) {
}
public String getNutritionDatabankID() {
return nutritionDatabankID;
}
public void setNutritionDatabankID(String nutritionDatabankID) {
this.nutritionDatabankID = nutritionDatabankID;
}
public String getLongDescription() {
return longDescription;
}
public void setLongDescription(String longDescription) {
this.longDescription = longDescription;
}
public String getCommonName() {
return commonName;
}
public void setCommonName(String commonName) {
this.commonName = commonName;
}
public String getManufacturerName() {
return manufacturerName;
}
public void setManufacturerName(String manufacturerName) {
this.manufacturerName = manufacturerName;
}
public String getScientificNameOfFood() {
return scientificNameOfFood;
}
public void setScientificNameOfFood(String scientificNameOfFood) {
this.scientificNameOfFood = scientificNameOfFood;
}
}
二食品情報エンティティ:
import javax.persistence.*;
import javax.validation.constraints.Size;
import java.io.Serializable;
@Entity
@Table(name = "SECONDARY_FOOD_DESCRIPTION")
public class SecondaryFoodDescription implements Serializable {
public SecondaryFoodDescription(){}
public SecondaryFoodDescription(Food food,String refuseDescription, String refusePercentage,
String caloriesFromProteinFactor, String caloriesFromFatFactor,
String caloriesFromCarbsFactor){
this.food = food;
this.refuseDescription = refuseDescription;
this.refuseDescription = refuseDescription;
this.caloriesFromProteinFactor = caloriesFromProteinFactor;
this.caloriesFromFatFactor = caloriesFromFatFactor;
this.caloriesFromCarbsFactor = caloriesFromCarbsFactor;
}
@Id
@GeneratedValue
@Column(name = "ID")
private Long id;
public Long getId() {
return id;
}
private Food food;
@Size(max = 135)
@Column(name = "REFUSE_DESCRIPTION")
private String refuseDescription;
@Column(name = "REFUSE_PERCENTAGE")
private String refusePercentage;
@Column(name = "CALORIES_FROM_PROTEIN_FACTOR")
private String caloriesFromProteinFactor;
@Column(name = "CALORIES_FROM_FAT_FACTOR")
private String caloriesFromFatFactor;
@Column(name = "CALORIES_FROM_CARBS_FACTOR")
private String caloriesFromCarbsFactor;
public String getRefuseDescription() {
return refuseDescription;
}
public void setRefuseDescription(String refuseDescription) {
this.refuseDescription = refuseDescription;
}
public String getRefusePercentage() {
return refusePercentage;
}
public void setRefusePercentage(String refusePercentage) {
this.refusePercentage = refusePercentage;
}
public String getCaloriesFromProteinFactor() {
return caloriesFromProteinFactor;
}
public void setCaloriesFromProteinFactor(String caloriesFromProteinFactor) {
this.caloriesFromProteinFactor = caloriesFromProteinFactor;
}
public String getCaloriesFromFatFactor() {
return caloriesFromFatFactor;
}
public void setCaloriesFromFatFactor(String caloriesFromFatFactor) {
this.caloriesFromFatFactor = caloriesFromFatFactor;
}
public String getCaloriesFromCarbsFactor() {
return caloriesFromCarbsFactor;
}
public void setCaloriesFromCarbsFactor(String caloriesFromCarbsFactor) {
this.caloriesFromCarbsFactor = caloriesFromCarbsFactor;
}
public Food getFood() {
return food;
}
public void setFood(Food food) {
this.food = food;
}
}
"food"列の内容が実際のマッピングよりも長い値を持つことを意味します。データベースに定義されている列の種類は何ですか? –
はい、メッセージの意味を理解していますが、その場合は - エンティティを参照しています。 「Food」クラスのID列は、英数字で、全長は5桁と英字です。 Foodオブジェクトをリポジトリに保存した後(34行目)、36行目に「SecondaryFoodDescription」の「Food」フィールドを入力します – user465001