14 Replies - 1315 Views - Last Post: 07 June 2018 - 07:07 AM

#1 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

Update a record in the db via a Java object

Posted 07 June 2018 - 02:34 AM

I am new here and also very new to Java and Java EE

I begin with showing my result and code i have.
Result:
Attached Image

The code from the xhtml page dataTable:

<h:form id="form">
                <h:panelGrid columns="1">

                    <p:dataTable var="day" value="#{insertProjectHoursBean.dayDataList}">
                        <f:facet name="header">
                            <p:commandButton id="submit_insertHours" value="Opslaan" action="#{insertProjectHoursBean.saveHours}" style="width:100px; float:right;" />

                        </f:facet>

                        <p:column headerText="Day of Month" style="width:120px">

                            <h:outputText value="#{day.ramDate}" />




                        </p:column>
                        <p:column headerText="Morning">
                            <p:selectOneMenu  id="selectProjectAm" value="#{day.projectAm}"
                                         converter="#{projectConverter}"    style="width:125px" >
                                <f:selectItem itemLabel="Select One" itemValue=""/>
                               <f:selectItems value="#{insertProjectHoursBean.projects}" var="proj" itemLabel="#{languageBean.language eq 'fr' ? proj.nameFr : proj.nameNl}"
                               itemValue="#{proj.id}"/>

                            </p:selectOneMenu>       <p:commandButton icon="fa fa-angle-down" style="margin-left:40px" /> | <p:commandButton icon="fa fa-angle-double-down" />
                            <p:inputTextarea value="#{day.commentAm}" rows="3" cols="33"/>

                        </p:column>
                        <p:column headerText="Afternoon">
                            <p:selectOneMenu id="selectProjectPm"  value="#{day.projectPm}"
                                             converter="#{projectConverter}"  style="width:125px" >
                                <f:selectItem itemLabel="Select One" itemValue=""/>
                                <f:selectItems value="#{insertProjectHoursBean.projects}" var="proj" itemLabel="#{languageBean.language eq 'nl' ? proj.nameFr : proj.nameNl}"
                                               itemValue="#{proj.id}"/>

                            </p:selectOneMenu>      <p:commandButton icon="fa fa-angle-down" style="margin-left:40px" /> | <p:commandButton icon="fa fa-angle-double-down" />
                            <p:inputTextarea value="#{day.commentPm}" rows="3" cols="33"/>

                        </p:column>
                    </p:dataTable>
                </h:panelGrid>
            </h:form>






And my code in my JavaBean for the saveHours() method:

public void saveHours() {

        for(DayData d: dayDataList)
        {
            if(data.getId() == d.getId()) {
                System.out.println(data.getRamDate());
                data.setName("Jan");
                data.setRamDate(d.getRamDate());
                data.setCommentAm(d.getCommentAm());
                data.setCommentPm(d.getCommentPm());
                data.setProjectAm(d.getProjectAm());
                data.setProjectPm(d.getProjectPm());

                dayDataTaskService.update(data);

            }else{
                DayData data = new DayData();
                data.setName("Jan");
                data.setRamDate(d.getRamDate());
                data.setCommentAm(d.getCommentAm());
                data.setCommentPm(d.getCommentPm());
                data.setProjectAm(d.getProjectAm());
                data.setProjectPm(d.getProjectPm());

                dayDataTaskService.create(data);

            }





        }


    }



So my problem is in the javaBean saveHours() method i think.
I want to check if a row/item of the list from the dataTable already exists as a record in the database table.
If not i have to create a new record.

The last part, creating a new record works already fine.
But it is the check for the update the specific record that doesn't work.

I think my condition in my if statement is maybe completely wrong?

Can Anyone help?

Thank's a lot

Wim

Is This A Good Question/Topic? 0
  • +

Replies To: Update a record in the db via a Java object

#2 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 03:18 AM

Where do you initialize the data variable? It seems that, in the first if condition, data is either a local class variable or not created/initialized at all.
Was This Post Helpful? 0
  • +
  • -

#3 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 03:22 AM

View Postmkkekkonen, on 07 June 2018 - 03:18 AM, said:

Where do you initialize the data variable? It seems that, in the first if condition, data is either a local class variable or not created/initialized at all.


Hi mkkekkonen.

I saw just when i posted my question that i forgot to write it.
I have initialized data in my declaration above in the JavaBean. So i did DayData data = new DayData(); at declaration.
Was This Post Helpful? 0
  • +
  • -

#4 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 04:27 AM

Where do you set the id of the data variable before the getId() comparison? I guess otherwise data.getId() will be null.
Was This Post Helpful? 0
  • +
  • -

#5 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 04:28 AM

View Postmkkekkonen, on 07 June 2018 - 04:27 AM, said:

Where do you set the id of the data variable before the getId() comparison? I guess otherwise data.getId() will be null.


The id in the database is automatically generated when there is a record created.
Was This Post Helpful? 0
  • +
  • -

#6 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 04:31 AM

Does data.getId() return a string? If it does, you should replace the comparison with

data.getId().equals(d.getId())


Was This Post Helpful? 0
  • +
  • -

#7 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 04:34 AM

View Postmkkekkonen, on 07 June 2018 - 04:31 AM, said:

Does data.getId() return a string? If it does, you should replace the comparison with

data.getId().equals(d.getId())


No the id is of a Long type.
Was This Post Helpful? 0
  • +
  • -

#8 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 04:44 AM

Have you debugged the program to see if the comparison returns true?
Was This Post Helpful? 0
  • +
  • -

#9 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 04:52 AM

View Postmkkekkonen, on 07 June 2018 - 04:44 AM, said:

Have you debugged the program to see if the comparison returns true?


Yes it seems that it doesn't even enter the saveHours method.
But when only the create method is in the method it enters very well.
Was This Post Helpful? 0
  • +
  • -

#10 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 05:09 AM

Did you remove the id comparison when having only the create functionality in the method? Have you set a breakpoint at the if() line to see if that is reached?
Was This Post Helpful? 0
  • +
  • -

#11 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 05:11 AM

View Postmkkekkonen, on 07 June 2018 - 04:44 AM, said:

Have you debugged the program to see if the comparison returns true?


I have set a debug point at the for statement and he enters the for loop while it says that data is effectively null.

View Postmkkekkonen, on 07 June 2018 - 05:09 AM, said:

Did you remove the id comparison when having only the create functionality in the method? Have you set a breakpoint at the if() line to see if that is reached?


When i used only the create method i indeed removed the if part.

For my previous reply i changed a value in the dataTable that already exsisted in the database table. Only to see that the data object was null
Was This Post Helpful? 0
  • +
  • -

#12 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 05:23 AM

I'm not entirely sure if it's what you're intending to do, but you could add a null check to the if statement to create a new data object if one does not exist yet.

Like...

if(data == null || data.getId() == d.getId()) {


Was This Post Helpful? 0
  • +
  • -

#13 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 05:46 AM

Of course I meant this, sorry, my bad

if(data != null && data.getId() == d.getId()) {


Was This Post Helpful? 1
  • +
  • -

#14 wim81   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-June 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 06:34 AM

View Postmkkekkonen, on 07 June 2018 - 05:46 AM, said:

Of course I meant this, sorry, my bad

if(data != null && data.getId() == d.getId()) {



Thanks for the answers and information. With your information in mind i have found the solution.

This is the working code:

 public void saveHours() {
        LocalDate date = LocalDate.now().withDayOfMonth(1);
        for(DayData d: dayDataList)
        {

            data = dayDataTaskService.findAllDataForUser("Jan", Date.valueOf(date));
            if(data == null) {

                DayData data = new DayData();
                data.setName("Jan");
                data.setRamDate(d.getRamDate());
                data.setCommentAm(d.getCommentAm());
                data.setCommentPm(d.getCommentPm());
                data.setProjectAm(d.getProjectAm());
                data.setProjectPm(d.getProjectPm());

                dayDataTaskService.create(data);
                date = date.plusDays(1);


            }else if(data.getId() == d.getId()){
                System.out.println(data.getRamDate());
                data.setName("Jan");
                data.setRamDate(d.getRamDate());
                data.setCommentAm(d.getCommentAm());
                data.setCommentPm(d.getCommentPm());
                data.setProjectAm(d.getProjectAm());
                data.setProjectPm(d.getProjectPm());

                dayDataTaskService.update(data);
                date = date.plusDays(1);
            }
            
        }

    }




This line was the crucial part that was missing.

Thanks a lot for your answers.
Was This Post Helpful? 0
  • +
  • -

#15 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: Update a record in the db via a Java object

Posted 07 June 2018 - 07:07 AM

No problem! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1