There is a way to save an Araaylist<Food> in the MYSQL database

Asked

Viewed 32 times

0

Here’s my situation: I’m putting together an app that indicates meals .

So I have a table called food, which has as fields: idAlimento, name, measure

For example: 1 , rice, 100 grams 2, feijao, 50 grams 3, beef steak, 200 grams.

I want to insert these foods into the meal table, which would have as fields: idRocation, name, food, typeRef

For example: 1, second lunch, (rice, beans, farofa and picanha steak), lunch

My doubt: I in case, would only insert the food in the meal table But they will be different food, which will be in the same line.

What would be the best way to do that ?

I thought about saving the food in an arraylist and then go through and save them in a single field of the refection table, but I don’t know if this is the best way.

I wanted to stay in a relational database, today I’m using mysql. But if the best way is a nonrelational, as I would ?

Thanks for your help

  • 2

    What is happening is that you have mapped your relationship from 1 to 1, that is, each Meal has one and only one Food, and now you need a Meal to have several foods. at this point you have a 1-to-N relationship, one for many, so you can program this relationship using mysql and java, it is no limitation for the relational database, including why it is called relational, hope to have helped, if you post your code we can even give other tips and even fix something you missed.

  • https://en.m.wikipedia.org/wiki/Normaliza%C3%A7%C3%A3o_de_dados / Normalize structure, otherwise you will have sqls complicsdos unnecessarily.

  • @Isaíasdelimacoelho thank you so much for the tip actually, this exclusive class, I didn’t even think about how to do but really, I had forgotten this kind of relationship.

  • @Isaíasdelimacoelho I will start with the DBO with the variables and the builder .. and I post the code here so far, thanks I am currently searching on the subject

  • I appreciate the help of all I am saving the arraylist in a json file and then adding in the database Thanks !

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.