当前位置:编程学习 > JAVA >>

hibernate见表,插入不进去数据

// User.java
package com.weixin.entity;

import java.util.Date;



public class User {
private int id;

private String email;

private String nickname;

private String password;

// email是否已经验证 0-未验证 1-已经验证
private int emailVerify;

// email验证码
private String verifyCode;

private Date lastLogin;

private String lastIP;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getEmailVerify() {
return emailVerify;
}

public void setEmailVerify(int emailVerify) {
this.emailVerify = emailVerify;
}

public String getVerifyCode() {
return verifyCode;
}

public void setVerifyCode(String verifyCode) {
this.verifyCode = verifyCode;
}

public Date getLastLogin() {
return lastLogin;
}

public void setLastLogin(Date lastLogin) {
this.lastLogin = lastLogin;
}

public String getLastIP() {
return lastIP;
}

public void setLastIP(String lastIP) {
this.lastIP = lastIP;
}


}
//User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.weixin.entity.User" table="d_d_user">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="email" column="email" />
<property name="nickname" column="nickname" />
<property name="password" column="password" />
<property name="emailVerify" column="is_email_verify" />
<property name="verifyCode" column="email_verify_code" />
<property name="lastLogin" column="last_login_time"  type="date"/>
<property name="lastIP" column="last_login_ip" />
</class>
</hibernate-mapping>
//IUserDao.java
package com.weixin.dao;

import com.weixin.entity.User;

public inte易做图ce IUserDao {
/**
 * 添加用户
 */
int saveUser (User user);
}
//UserDaoImpl
package com.weixin.dao.impl;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.weixin.dao.IUserDao;
import com.weixin.entity.User;

public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {

public int saveUser(User user) {
return (Integer) this.getHibernateTemplate().save(user);
}

}
//IUserServ
package com.weixin.biz;

import com.weixin.entity.User;

public inte易做图ce IUserServ {
int saveUser(User user);
}
//UserServImpl
package com.weixin.biz.impl;

import com.weixin.biz.IUserServ;
import com.weixin.dao.IUserDao;
import com.weixin.entity.User;

public class UserServImpl implements IUserServ{
private IUserDao iuserDao;
public int saveUser(User user) {
return iuserDao.saveUser(user);
}
public IUserDao getIuserDao() {
return iuserDao;
}
public void setIuserDao(IUserDao iuserDao) {
this.iuserDao = iuserDao;
}

}

//UserAction
package com.weixin.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.weixin.biz.IUserServ;
import com.weixin.entity.User;

public class UserAction extends ActionSupport {
private IUserServ iuserServ;

private User user;

public String regUser() {
int userId = iuserServ.saveUser(user);
if (userId > 0) {
return "ok";
}
return "error";
}

public void setIuserServ(IUserServ iuserServ) {
this.iuserServ = iuserServ;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

}
//struts-user.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="ssh-user" extends="ssh-default" namespace="/user">
<action name="userAction" class="userAction">
<result name="ok">
/WEB-INF/jsp/ok.jsp
</result>
<result name="error">
/WEB-INF/jsp/error.jsp
</result>
</action>
</package>
</struts>
//user_save.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'user_save.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<s:form action="userAction!regUser" namespace="/user">
<table>
<tr>
<td>
账号:
</td>
<td>
<s:textfield name="user.username" />
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<s:textfield name="user.password" />
</td>
</tr>
<tr>
  <td>
    <input type="submit" name="regButton" value="注册">
  </td>
</tr>
</table>
</s:form>
</body>
</html>


提交之后浏览器返回的结果是
Struts Problem Report

Struts has detected an unhandled exception:
Messages: 

    Column 'email' cannot be null
    could not insert: [com.weixin.entity.User]
    could not insert: [com.weixin.entity.User]; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [com.weixin.entity.User]

File:  org/hibernate/exception/SQLStateConverter.java

控制台查询表结构是:
mysql> desc d_D_user;
+-------------------+-------------+------+-----+---------+----------------+
| Field             | Type        | Null | Key | Default | Extra          |
+-------------------+-------------+------+-----+---------+----------------+
| id                | int(12)     | NO   | PRI | NULL    | auto_increment |
| email             | varchar(50) | NO   | UNI | NULL    |                |
| nickname          | varchar(50) | YES  | UNI | NULL    |                |
| password          | varchar(50) | NO   |     | NULL    |                |
| is_email_verify   | int(1)      | YES  |     | 0       |                |
| email_verify_code | varchar(50) | YES  |     | NULL    |                |
| last_login_time   | bigint(20)  | YES  |     | NULL    |                |
| last_login_ip     | varchar(15) | YES  |     | NULL    |                |
+-------------------+-------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

但我明明没有设置email的列啊,这是怎么回事???





--------------------编程问答-------------------- 你库表里边设定email不能为空,看你desc出来的结果
+-------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+----------------+
| id | int(12) | NO | PRI | NULL | auto_increment |
| email | varchar(50) | NO | UNI | NULL | |
| nickname | varchar(50) | YES | UNI | NULL | |
| password | varchar(50) | NO | | NULL | |
| is_email_verify | int(1) | YES | | 0 | |
| email_verify_code | varchar(50) | YES | | NULL | |
| last_login_time | bigint(20) | YES | | NULL | |
| last_login_ip | varchar(15) | YES | | NULL | |
+-------------------+-------------+------+-----+---------+----------------+|
你在进行插入的操作时,email字段没给值,所以才报错 --------------------编程问答-------------------- 你在Hibernate中配置了email去掉看看会不会报错。也可以设置你的email可以为空就OK了,你配置了email就会自动易做图去数据,没有就是null --------------------编程问答-------------------- email自动生成的是默认不为空吗 --------------------编程问答-------------------- 觉得1楼说得对,email数据库中设置不能为空,看看你给email赋值没 --------------------编程问答-------------------- Column 'email' cannot be null
--------------------编程问答-------------------- Column 'email' cannot be null   
给出的错误已经说明了问题了。
并且你的页面也可以看出根本没有email的值传到后台,只有账号和密码。所以会报这个错误
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,