简体   繁体   中英

Issue with Integer vs. int attached to a database in JPA Repository

I am using Visual Paradigm to generate a database and associated code, and have put that into a Maven, Spring JPA Repository framework. In one class, I happened to define a class member as an Integer. When VP generated the code, it generated two setters:

public void setCapacityDown(Integer value) {
    this.capacityDown = value;
}

public void setCapacityDown(int value) {
    this.capacityDown = value;
}

This code runs, but when a user sends a save request to the server, it fails because the framework doesn't know which setter to call.

I changed from Integer to int in VP, regenerated the code, and got the single setter. Now the class will save as expected. However, when the server runs queries on the database, I now get a getOutputStream() already called for this response (because it has already started to output JSON when this new error occurs). I can compare the Hibernate outputs on one query, for each case (one setter vs. two setters). For the two-setter case (where the query runs), I get 11 Hibernate statements for ServiceLevel (where the setters are):

Hibernate: select servicelev0_.ServiceProfileID as Service12_32_0_, servicelev0_.ID as ID1_31_0_, servicelev0_.ID as ID1_31_1_, servicelev0_.CapacityDown as Capacity2_31_1_, servicelev0_.CapacityUp as Capacity3_31_1_, servicelev0_.CreationDate as Creation4_31_1_, servicelev0_.Deleted as Deleted5_31_1_, servicelev0_.Description as Descript6_31_1_, servicelev0_.UnitsDownID as UnitsDo11_31_1_, servicelev0_.LastModified as LastModi7_31_1_, servicelev0_.Name as Name8_31_1_, servicelev0_.ProvisioningPackage as Provisio9_31_1_, servicelev0_.SalesDescription as SalesDe10_31_1_, servicelev0_.ServiceProfileID as Service12_31_1_, servicelev0_.UnitsUpID as UnitsUp13_31_1_ from ServiceLevel servicelev0_ where servicelev0_.ServiceProfileID=?

For the single-setter case, I get 6 of those identical statements, followed by:

java.lang.IllegalStateException: getOutputStream() has already been called for this response at org.apache.catalina.connector.Response.getWriter(Response.java:578)

The stack trace only contains data of the Exception, and not of the error which I am trying to track down.

Finally figured it out. The ServiceLevel table had several rows where the column of interest was null. Once the server code changed from Integer to int for that column/member, the code failed trying to insert a into an int.

It would be nice if the stack trace gave a clue about this, instead of the generic getOutputStream() error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM