我尝试用java把object通过http post 方式发送到play framework连接的mysql中,我不太清楚play framework中的 id怎么处理,返回的错误如下
---------------------------------------------------
java送信端Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:9000/Application/show
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sqlPost.SqlPost.httpPost(SqlPost.java:37)
at sqlPost.SqlPost.main(SqlPost.java:47)
---------------------------------------------------
play framework端Internal Server Error (500) for request POST /Application/showExecution exception (In /app/controllers/Application.java around line 27)
IllegalArgumentException occured : id to load is required for loadingplay.exceptions.JavaExecutionException: id to load is required for loading
        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:231)
        at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalArgumentException: id to load is required for loadin
g
        at org.hibernate.event.LoadEvent.<init>(LoadEvent.java:89)
        at org.hibernate.event.LoadEvent.<init>(LoadEvent.java:61)
        at org.hibernate.impl.SessionImpl.get(SessionImpl.java:1002)
        at org.hibernate.impl.SessionImpl.get(SessionImpl.java:998)
        at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManage
rImpl.java:614)
        at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManage
rImpl.java:589)
        at play.db.jpa.JPQL.findById(JPQL.java:36)
        at models.AuctionItem.findById(AuctionItem.java)
        at controllers.Application.show(Application.java:27)
        at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:548)        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:502)
        at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:478)        at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:473)        at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
        ... 1 more
---------------------------------------------------
java发送端代码
package sqlPost;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;public class SqlPost {
public void httpPost() throws IOException {
// Create Post String
// String data = URLEncoder.encode("myName", "UTF-8") + "="+
// URLEncoder.encode("ttgg", "UTF-8"); AuctionItem item = new AuctionItem();
item.title = "Football";
item.days = 11;
item.startBid = (float) 1;
item.buyNowEnabled = true;
item.buyNowPrice = (float) 20;
item.deliveryCost = (float) 5;
item.description = "this football is a new one"; // Send Data To Page
URL url = new URL("http://localhost:9000/Application/show");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
// OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(conn.getOutputStream());
oos.writeObject(item);
oos.flush(); // Get The Response
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
// you Can Break The String Down Here
}
} public static void main(String args[]) throws Exception {
SqlPost formpost = new SqlPost();
formpost.httpPost();
}
}
---------------
package sqlPost;import java.io.Serializable;
import java.util.*;public class AuctionItem implements Serializable {
public String title;
public Date startDate;
public Date endDate;
public Float deliveryCost;
public Float startBid;
public Float buyNowPrice;
public Boolean buyNowEnabled;
public String description;
public Integer viewCount = 0; public Float getCurrentTopBid() {
return startBid;
} public Integer days; public void setDays(Integer days) {
this.days = days;
if (days != null) {
startDate = new Date();
endDate = new Date(System.currentTimeMillis()
+ (days * 24 * 60 * 60 * 1000));
}
}
}---------------------------------------------------
play framework端代码
package controllers;import java.util.Date;
import java.util.List;import models.AuctionItem;
import play.mvc.*;public class Application extends Controller {
public static void createAuctionItem() {
render();
} public static void index() {
List<AuctionItem> mostPopular = AuctionItem.getMostPopular(5);
List<AuctionItem> endingSoon = AuctionItem.getEndingSoon(5);
render(mostPopular, endingSoon);
} public static void doCreateItem(AuctionItem item) {
item.save();
show(item.id);
System.out.println(item.id);
} public static void show(Long id) {
AuctionItem item = AuctionItem.findById(id);
item.viewCount++;
item.save();
render(item);
}
}
------------------
show.html
#{extends 'main.html' /}
#{set title:'ePlay Auctions - Create a new Item Listing' /}
#{set 'moreStyles'}
<link rel="stylesheet" href="@{'/public/stylesheets/style.css'}">
#{/set}<div id="content">
  <h1>${item.title}</h1>
  <p class="field">
<label>Start Bid:</label>${item.startBid}
  </p>
  #{if item.buyNowEnabled}
  <p class="field">
<label>Buy Now Price:</label>${item.buyNowPrice}
  </p>
  #{/if}
  <p class="field">
<label>Auction Ends:</label>${item.endDate}
  </p>
  <p class="field">
<label>Delivery Charges:</label>${item.deliveryCost}
  </p>
  <p class="field">
<label>Item Description:</label>&nbsp;
  </p>
  <p class="field">
${item.description}
  </p>
</div>
---------------------------------------------------