我已尝试过所有可能的网站,但无法找到解决方案。这里的不同情况是我的主键是字符串类型,我甚至不能使用序列自动生成。当我尝试更新表中的数据时,会发生此问题。 这是我的Model类:
package com.pack1.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table (name="automation_test")
public class Config {
@Id
@Column(name="DATA_ID")
private String dataa_id;
private String r12_user_name ;
private String r12_app_url;
private String r12_user_password;
private String p6_url;
private String p6_username;
private String p6_password;
private String dcp_url;
private String dcp_username;
private String dcp_password;
private String so_appurl;
private String so_appusername;
private String so_apppassword;
private String odsrefresh_url;
private String odsrefresh_username;
private String odsrefresh_password;
public String getDataa_id() {
return dataa_id;
}
public void setDataa_id(String dataa_id) {
this.dataa_id = dataa_id;
}
public String getR12_user_name() {
return r12_user_name;
}
public void setR12_user_name(String r12_user_name) {
this.r12_user_name = r12_user_name;
}
public String getR12_app_url() {
return r12_app_url;
}
public void setR12_app_url(String r12_app_url) {
this.r12_app_url = r12_app_url;
}
public String getR12_user_password() {
return r12_user_password;
}
public void setR12_user_password(String r12_user_password) {
this.r12_user_password = r12_user_password;
}
public String getP6_url() {
return p6_url;
}
public void setP6_url(String p6_url) {
this.p6_url = p6_url;
}
public String getP6_username() {
return p6_username;
}
public void setP6_username(String p6_username) {
this.p6_username = p6_username;
}
public String getP6_password() {
return p6_password;
}
public void setP6_password(String p6_password) {
this.p6_password = p6_password;
}
public String getDcp_url() {
return dcp_url;
}
public void setDcp_url(String dcp_url) {
this.dcp_url = dcp_url;
}
public String getDcp_username() {
return dcp_username;
}
public void setDcp_username(String dcp_username) {
this.dcp_username = dcp_username;
}
public String getDcp_password() {
return dcp_password;
}
public void setDcp_password(String dcp_password) {
this.dcp_password = dcp_password;
}
public String getSo_appurl() {
return so_appurl;
}
public void setSo_appurl(String so_appurl) {
this.so_appurl = so_appurl;
}
public String getSo_appusername() {
return so_appusername;
}
public void setSo_appusername(String so_appusername) {
this.so_appusername = so_appusername;
}
public String getSo_apppassword() {
return so_apppassword;
}
public void setSo_apppassword(String so_apppassword) {
this.so_apppassword = so_apppassword;
}
public String getOdsrefresh_url() {
return odsrefresh_url;
}
public void setOdsrefresh_url(String odsrefresh_url) {
this.odsrefresh_url = odsrefresh_url;
}
public String getOdsrefresh_username() {
return odsrefresh_username;
}
public void setOdsrefresh_username(String odsrefresh_username) {
this.odsrefresh_username = odsrefresh_username;
}
public String getOdsrefresh_password() {
return odsrefresh_password;
}
public void setOdsrefresh_password(String odsrefresh_password) {
this.odsrefresh_password = odsrefresh_password;
}
}
这是我的DAO:
package com.pack1.dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.pack1.model.Test;
import com.pack1.model.Config;
@Repository("testiDao")
public class TestDAOImpl implements TestDAO{
@Autowired
private SessionFactory sessionFactory;
@Override
public List getAllDetails()
{
SQLQuery query=sessionFactory.getCurrentSession().createSQLQuery("select * from automation_test");
query.addEntity("automation_test",Test.class);
List list=query.list();
return list;
}
@Override
public void insert(Config cg)
{
System.out.println("Request");
sessionFactory.getCurrentSession().update(cg);
System.out.println("Response");
}
}
这是我的控制器:
package com.pack1.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.pack1.model.Test;
import com.pack1.model.Config;
import com.pack1.service.TestService;
@Controller
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value="/start",method=RequestMethod.GET)
public String setupForm(@ModelAttribute Config con1,Map<String,Object> map)
{
Config con=new Config();
map.put("con", con);
return "config";
}
@RequestMapping(value="/doCon",method=RequestMethod.GET)
public String doInsert(@ModelAttribute Config con1, BindingResult result,@RequestParam String action,Map<String,Object> map)
{
Config cg=new Config();
if(action.toLowerCase().equals("submit"))
{
testService.insert(cg);
}
map.put("con",cg);
return "config";
}
@RequestMapping(value="/config",method=RequestMethod.GET)
public String doback(Map<String,Object> map)
{
Config con=new Config();
map.put("con", con);
return "config";
}
}
这是我得到的错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.TransientObjectException: The given object has a null identifier: com.pack1.model.Config
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
请帮助,因为我很长时间以来一直坚持这一点。
答案 0 :(得分:0)
@RequestMapping(value="/doCon",method=RequestMethod.GET)
public String doInsert(@ModelAttribute Config con1, BindingResult result,@RequestParam String action,Map<String,Object> map)
{
Config cg=new Config();
if(action.toLowerCase().equals("submit"))
{
testService.insert(cg);
}
map.put("con",cg);
return "config";
}
cg有一个null
id,这就是你在执行
testService.insert(cg);
也许你的意思是:
testService.insert(con1);
取而代之的是确保con1的dataa_id
不是null