habuma / spring-in-action-5-samples

Home for example code from Spring in Action 5.
Apache License 2.0
1.21k stars 1.04k forks source link

Chapter 3 JDBC problems #25

Open Celebes opened 6 years ago

Celebes commented 6 years ago

1) In chapter 3 there isn't a mention about adding "IngredientByIdConverter" - without it the app crashes after saving taco design.

2) The code in JdbcTacoRepository.saveTacoInfo() often results in NullPointerException. The solution is to set flag "setReturnGeneratedKeys(true)" on PreparedStatementCreatorFactory object:

PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(QUERY_SAVE_TACO, Types.VARCHAR, Types.TIMESTAMP);
pscf.setReturnGeneratedKeys(true);
PreparedStatementCreator psc = pscf.newPreparedStatementCreator(Arrays.asList(taco.getName(), new Timestamp(taco.getCreatedAt().getTime())));
habuma commented 5 years ago

First, good point on item #1. In fact, that wasn't originally required (earlier versions of the code worked fine without it, but something changed...I don't recall what...and now that is necessary. I added it post-production on the book and thus mention of it didn't make it into the book. I'll see to it that the errata list is updated to mention it, though.

On point #2, I just ran through the code for ch03/tacos-jdbc, as it appears in the master branch of this repository. The only change I made was to add some logging to the saveTacoInfo() method to log the ID. Other than that, I was able to create multiple tacos with no issue (and without having to call setReturnGeneratedKeys(true)).

I'm not saying that calling setReturnGeneratedKeys() isn't a good idea. Maybe it is. But I'm curious as to why it works fine for me without it, but not so much for you until you add it. I wonder what's different.

thiagotgo90 commented 5 years ago

Hi, can you please let us know when the errata is updated? I'm curious to find out what changed. Thanks very much.

TomDu commented 5 years ago
  1. In chapter 3 there isn't a mention about adding "IngredientByIdConverter" - without it the app crashes after saving taco design.
  2. The code in JdbcTacoRepository.saveTacoInfo() often results in NullPointerException. The solution is to set flag "setReturnGeneratedKeys(true)" on PreparedStatementCreatorFactory object:
PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(QUERY_SAVE_TACO, Types.VARCHAR, Types.TIMESTAMP);
pscf.setReturnGeneratedKeys(true);
PreparedStatementCreator psc = pscf.newPreparedStatementCreator(Arrays.asList(taco.getName(), new Timestamp(taco.getCreatedAt().getTime())));

Reproed No.2 and fixed by calling the same method.

FallingW commented 5 years ago

Thanks for your solution! @Celebes

EMEDRANO86 commented 5 years ago

OMG Thank you so much @Celebes !!! It was driving me crazy!

mghildiy commented 5 years ago

I am also facign #1 issue. So where and do I need to add IngredientByIdConverter?

mghildiy commented 5 years ago

Got the answer.

But now I face another issue. Whenever I save Taco, the Order object is always a new one with previous tacos not part of it.

EMEDRANO86 commented 5 years ago

You should add the IngredientByIdConverter inside the "Tacos" package. Look for it in the GitHub project. Once you do that the conversion cab be made and everything works!

On Sun, Jun 9, 2019, 06:07 mghildiy notifications@github.com wrote:

Got the answer.

But now I face another issue. Whenever I save Taco, the Order object is always a new one with previous tacos not part of it.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/habuma/spring-in-action-5-samples/issues/25?email_source=notifications&email_token=AGBBPJYE54BQ7OK7C7SKRMTPZR6XHA5CNFSM4GBTIA6KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXIDCZI#issuecomment-500183397, or mute the thread https://github.com/notifications/unsubscribe-auth/AGBBPJ66B77HA7V4BBAGOS3PZR6XHANCNFSM4GBTIA6A .

nanbojini commented 4 years ago

I just ran into this issue, thanks very much!

abstract-thinking commented 4 years ago

Even I wonder about this issue. I cloned the project from Craig's repository and let the test cases run with mvn and Intellij community IDE. The result was perfect - all test cases are passing. So I copied the folder src and the pom.xml into my projects but the result depends where the code is running. If running with maven all test cases passes. Running inside the IDE all test cases of DesignAndOrderTacosBrowserTest are failing with the NPE. :-(

Alexandr-Gubkin commented 4 years ago

Can someone explain to me why we have created the class Integrientbyidconverter, but do not use its method anywhere?

seguri commented 4 years ago

@Alexandr-Gubkin You can try that: delete the @Component annotation from IngredientByIdConverter and run DesignTacoControllerTest. There's a validation error (it cannot convert a String to an Ingredient):

2020-10-08 09:02:27.794 ERROR 16396 --- [           main] tacos.web.DesignTacoController           : Validation error: Field error in object 'taco' on field 'ingredients': rejected value [FLTO,GRBF,CHED]; codes [typeMismatch.taco.ingredients,typeMismatch.ingredients,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [taco.ingredients,ingredients]; arguments []; default message [ingredients]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'ingredients'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'tacos.Ingredient' for property 'ingredients[0]': no matching editors or conversion strategy found]
Alexandr-Gubkin commented 4 years ago

@Alexandr-Gubkin You can try that: delete the @Component annotation from IngredientByIdConverter and run DesignTacoControllerTest. There's a validation error (it cannot convert a String to an Ingredient):

2020-10-08 09:02:27.794 ERROR 16396 --- [           main] tacos.web.DesignTacoController           : Validation error: Field error in object 'taco' on field 'ingredients': rejected value [FLTO,GRBF,CHED]; codes [typeMismatch.taco.ingredients,typeMismatch.ingredients,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [taco.ingredients,ingredients]; arguments []; default message [ingredients]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'ingredients'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'tacos.Ingredient' for property 'ingredients[0]': no matching editors or conversion strategy found]

okay, it doesnt work without IngredientByIdConverter =) But I wanted to understand where in General it is used in this application? Which entity uses it ? I can't see the convert method being applied...

medkhabt commented 3 years ago

@Alexandr-Gubkin You can try that: delete the @Component annotation from IngredientByIdConverter and run DesignTacoControllerTest. There's a validation error (it cannot convert a String to an Ingredient):

2020-10-08 09:02:27.794 ERROR 16396 --- [           main] tacos.web.DesignTacoController           : Validation error: Field error in object 'taco' on field 'ingredients': rejected value [FLTO,GRBF,CHED]; codes [typeMismatch.taco.ingredients,typeMismatch.ingredients,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [taco.ingredients,ingredients]; arguments []; default message [ingredients]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'ingredients'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'tacos.Ingredient' for property 'ingredients[0]': no matching editors or conversion strategy found]

okay, it doesnt work without IngredientByIdConverter =) But I wanted to understand where in General it is used in this application? Which entity uses it ? I can't see the convert method being applied...

Did you find an answer ? i have the same question, it's maybe a thing with thymleaf and spring, but i don't know what it is.

hasanforaty commented 3 years ago

@Alexandr-Gubkin You can try that: delete the @Component annotation from IngredientByIdConverter and run DesignTacoControllerTest. There's a validation error (it cannot convert a String to an Ingredient):

2020-10-08 09:02:27.794 ERROR 16396 --- [           main] tacos.web.DesignTacoController           : Validation error: Field error in object 'taco' on field 'ingredients': rejected value [FLTO,GRBF,CHED]; codes [typeMismatch.taco.ingredients,typeMismatch.ingredients,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [taco.ingredients,ingredients]; arguments []; default message [ingredients]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'ingredients'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'tacos.Ingredient' for property 'ingredients[0]': no matching editors or conversion strategy found]

okay, it doesnt work without IngredientByIdConverter =) But I wanted to understand where in General it is used in this application? Which entity uses it ? I can't see the convert method being applied...

Did you find an answer ? i have the same question, it's maybe a thing with thymleaf and spring, but i don't know what it is.

i think we need it to tell spring that how convert String to ingredient why ? cuz we will get String ( ingredient id) from design.html and Spring don't know how convert it to Ingredient and create out taco object