Spring Data"数据太长,不适用于行和#34;

时间:2016-09-01 21:57:58

标签: java spring spring-boot spring-data

我的数据库中有3个表。Application.java。我的SecondaryFoodDescription以正确的顺序解析数据文件,以便在桌面上对下一个文件有用。我的问题是,在Data truncation: Data too long for column 'food' at row 1类中设置databankID字段时,我得到Food。这是令人惊讶的(至少对我而言)因为,我使用相同的repository.save方法来填充类中的foodGroup字段。 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;
}

}

二级食品信息实体:

SELECT COUNT(*) no_of_employees
  FROM my_table a 
  JOIN 
     ( SELECT MAX(months*salary) total FROM my_table ) b 
    ON b.total = a.months * a.salary;

2 个答案:

答案 0 :(得分:5)

您需要使用适当的关系@OneToOne / @OneToMany进行注释,否则整个对象将被序列化并作为BLOB保留。如果BLOB大小很大,则会出现Data Truncation错误。

答案 1 :(得分:0)

&#34;解决&#34;通过向food实体中的SecondaryFoodDescription字段添加@OneToOne注释。

但这是一个令人不满意的答案。在Food实体中,实体FoodGroup有一个未注释的字段。该字段不会产生&#34;列为long&#34;错误。