Spring-boot和Java FindById返回null而不是数字

时间:2019-03-13 16:34:25

标签: java hibernate spring-boot

有人可以告诉我为什么我的代码找不到每个人的ID吗?每当我更新已经输入的人员信息时,我的代码都会在SQL中创建新行,因为它找不到ID。 我正在使用旧版本的Spring-Boot,并且我知道在新版本中,findById已变为可选...这是否在我的旧版本中引起问题?

这是我的存储库代码:

@Transactional
public interface BrokerRepository extends CrudRepository<Broker,Long>, JpaSpecificationExecutor {

    Broker save(Broker entity);

    Broker findById(Long id);

    void delete(Broker entity);

    List<Broker> findAll();

    List<Broker> findByBrokerDetailContaining(String brokerDetail);
    List<Broker> findByAccountNameContaining(String accountName);
    List<Broker> findByEpicBrokerCodeContaining(String epicBrokerCode);

    List<Broker> findByStatus(String prospect);

//    List<Broker> findByExpiration(LocalDate licenseExpiration);
}

这是我的控制人

    @RequestMapping(value="/edit/{id}")
    public String editbroker(Model model, @PathVariable("id") Long id){
        Broker existing= brokerRepository.findById(id);
        model.addAttribute("broker",existing);
        return "brokerProfile";
    }



    @RequestMapping(value="/saveBroker")
    @ResponseBody
    public JSONObject saveBroker(Model model, @ModelAttribute(value="broker") Broker broker)
    {
        Boolean saved=false;
        JSONObject response=new JSONObject();
        Broker brokerBeforeUpdate=brokerRepository.findById(broker.getId());
        if (brokerBeforeUpdate!=null && !brokerBeforeUpdate.getStatus().equals("active") && broker.getStatus().equals("active"))
            broker.setOnboardedDate(LocalDate.now());
        else if (!broker.getStatus().equals("active"))
            broker.setOnboardedDate(null);
        try{
            brokerBeforeUpdate=brokerRepository.save(broker);
            saved=true;
            response.put("id",broker.getId());

        }catch (DataAccessException e) {
            e.printStackTrace();
            response.put("error",e.getLocalizedMessage());
            response.put("cause",e.getLocalizedMessage());
        }
        response.put("success",saved);
        return response;
    }

这是我的XML:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
  	<groupId>com.example</groupId>
	<artifactId>Portal</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>Portal</name>
	<description>Portal</description>
  <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-ldap</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.dblock.waffle</groupId>
			<artifactId>waffle-spring-security3</artifactId>
			<version>1.8.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.security.kerberos</groupId>
			<artifactId>spring-security-kerberos-web</artifactId>
			<version>1.0.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-java8time</artifactId>
			<version>2.1.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<version>1.4.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>com.microsoft.sqlserver</groupId>
			<artifactId>mssql-jdbc</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.jayway.jsonpath</groupId>
			<artifactId>json-path</artifactId>
		</dependency>
		<!-- flying saucer pdf -->
		<dependency>
			<groupId>org.xhtmlrenderer</groupId>
			<artifactId>flying-saucer-pdf</artifactId>
			<version>9.1.4</version>
		</dependency>

		<!-- jxls simple excel -->
		<dependency>
			<groupId>org.jxls</groupId>
			<artifactId>jxls</artifactId>
			<version>2.4.3</version>
		</dependency>

		<dependency>
			<groupId>org.jxls</groupId>
			<artifactId>jxls-poi</artifactId>
			<version>1.0.14</version>
		</dependency>

		<dependency>
			<groupId>org.jxls</groupId>
			<artifactId>jxls-jexcel</artifactId>
			<version>1.0.6</version>
		</dependency>

		<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-springsecurity4</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>3.2.8.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.metawidget.modules</groupId>
			<artifactId>metawidget-all</artifactId>
			<version>2.1</version>
			<scope>compile</scope>
		</dependency>

		<!--<dependency>-->
		<!--<groupId>org.springframework.boot</groupId>-->
		<!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
		<!--<scope>provided</scope>-->
		<!--</dependency>-->
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<executable>true</executable>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

HTML表单:

    <div id="rootwizard" class="container" th:with="brokerEndpoint = ${broker.id == null ? '/addBroker' : '/saveBroker'}">
        <form enctype="multipart/form-data" th:action="${brokerEndpoint}" method="post" th:object="${broker}" class="tab-content">
            <h4>Broker Onboarding Info</h4>
            <br/>
                <div class="col-sm-6">
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Broker Detail</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="brokerDetail" th:field="*{brokerDetail}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Account Name</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="accountName" th:field="*{accountName}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Parent Account</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="parentAccount" th:field="*{parentAccount}" class="form-control"/>
                        </div>
                    </div>
                    <br/>

                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Physical Zip</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="physicalZip" th:field="*{physicalZip}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Date Appointed</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="dateAppt" th:field="*{dateAppt}" class="form-control date"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Phone</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="phone" th:field="*{phone}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Website</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="website" th:field="*{website}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Main Agency Contact</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="mainAgencyContact" th:field="*{mainAgencyContact}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">FEIN #</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="feinNo" th:field="*{feinNo}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <div class="col-sm-6 col-xs-8" style="margin-left:5.27rem;">
                            <label>Appointment Paperwork Sent
                                <input type="checkbox" id="apptPaperworkSent" th:field="*{apptPaperworkSent}" class="flexFit"/>
                            </label>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <div class="col-sm-6 col-xs-8" style="margin-left:13rem;">
                            <label>License Received
                                <input type="checkbox" id="licenseRecd" th:field="*{licenseRecd}" class="flexFit"/>
                            </label>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">License Effective</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="licenseEffective" th:field="*{licenseEffective}" class="form-control date"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">License Expiration</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="licenseExpiration" th:field="*{licenseExpiration}" class="form-control date"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <div class="col-sm-6 col-xs-8" style="margin-left:8rem;">
                            <label>E&amp;O Dec. Page Received
                                <input type="checkbox" id="eoDecPageRecd" th:field="*{eoDecPageRecd}" class="flexFit"/>
                            </label>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">E&amp;O Effective Date</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="eoEffectiveDate" th:field="*{eoEffectiveDate}" class="form-control date"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">E&amp;O Expiration Date</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="eoExpirationDate" th:field="*{eoExpirationDate}" class="form-control date"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <div class="col-sm-6 col-xs-8" style="margin-left:4.5rem;">
                            <label class="flexFit">W9 Received
                                <input type="checkbox" id="w9Recd" th:field="*{w9Recd}" class="flexFit"/>
                            </label>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">W9 Date</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="w9Date" th:field="*{w9Date}" class="form-control date"/>
                        </div>
                    </div>
            </div>
                <div class="col-sm-6">
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Status</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="status" th:field="*{status}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Onboarded Date</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="onboardedDate" th:field="*{onboardedDate}" class="form-control date"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <div class="col-sm-6 col-xs-8 check-div" style="margin-left:6.85rem;">
                            <label>Agent Agreement Received
                                <input type="checkbox" id="agentAgreementRecd" th:field="*{agentAgreementRecd}" class="flexFit"/>
                            </label>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <div class="col-sm-6 col-xs-8 check-div" style="margin-left:6.85rem;">
                            <label>1099 Required
                                <input type="checkbox" id="x1099required" th:field="*{x1099required}" class="flexFit"/>
                            </label>
                        </div>
                    </div>
                    <br/>

                    <div class="row">
                        <div class="col-sm-6 col-xs-8 check-div" style="margin-left:6rem;">
                            <label style="margin-left: 30px;" >Contact Sheet Received
                                <input type="checkbox" id="contactSheetRecd" th:field="*{contactSheetRecd}" class="flexFit"/>
                            </label>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Contact Sheet Date</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="contactSheetDate" th:field="*{contactSheetDate}" class="form-control date"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Description</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="description" th:field="*{description}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Office Paid</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="officePaid" th:field="*{officePaid}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">formerBrokerName</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="formerBrokerName" th:field="*{formerBrokerName}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">E&amp;O Renewal Follow-up 1</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="eoRenewalFollowUp1" th:field="*{eoRenewalFollowUp1}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <div class="col-sm-6 col-xs-8 check-div" style="margin-left:7rem;">
                            <label >E&amp;O Follow-up 1 Complete
                                <input type="checkbox" id="eoFollowUp1Complete" th:field="*{eoFollowUp1Complete}" class="flexFit"/>
                            </label>
                        </div>
                    </div>
                    <br/>
                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">E&amp;O Renewal Follow-up 2</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="eoRenewalFollowUp2" th:field="*{eoRenewalFollowUp2}" class="form-control"/>
                        </div>
                    </div>
                    <br/>

                    <div class="row">
                        <label class="col-sm-6 col-xs-4 text-right">Billing Zip Code</label>
                        <div class="col-sm-6 col-xs-8">
                            <input type="text" id="billingZipCode" th:field="*{billingZipCode}" class="form-control"/>
                        </div>
                    </div>
                    <br/>
                </div> <!--end of right column-->
                <div class="row">
                    <div class="text-right" sec:authorize="hasRole('ROLE_ADMIN')">
                        <input id="submitButton"  type="submit" value="Save" name="save" class="btn tangramColor txt finish" data-loading-text="Saved!" disabled="true"/><br/>
                    </div>
                </div>
        </form>
    </div>

0 个答案:

没有答案