i have 3 tables in database.food_group, food_description, secondary_food_description,
. application.java
parses data files in correct order on table can useful next. issue when comes setting databankid field in secondaryfooddescription
class, data truncation: data long column 'food' @ row 1
. surprising (at least me) because, use same repository.save method populate foodgroup field in class. food
.
edit: i'm aware normally, error data truncation: data long column 'foodid' @ row 1
mean i'm trying insert big field. however, i'm trying insert entity. how entity big?
can spot i'm wrong?
@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(); (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; (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); } }; } }
repository interfaces:
@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> { }
food group entity:
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; } }
food entity:
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; } }
secondary food info entity:
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; } }
you need annotate appropriate relations @onetoone
/@onetomany
, otherwise complete object serialized , persisted blob. if blob size large data truncation
error.
Comments
Post a Comment