表结构mysql
============================================================================
CREATE TABLE `t_emp_ip` (
`EMP_ID` bigint(20) NOT NULL default '0',
`EMP_NO` varchar(32) default NULL,
`IP_NAME` varchar(32) default NULL,
`IPP_ID` bigint(20) NOT NULL,
`NETADDR_NO` varchar(32) default NULL,
`IS_DOWN` varchar(32) default NULL,
PRIMARY KEY (`EMP_ID`,`IPP_ID`),
KEY `EMP_ID` (`EMP_ID`),
KEY `IP_ID` (`IPP_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AVG_ROW_LENGTH=1260 COMMENT='InnoDB free: 3072 kB';
============================================================================
EmpIpp类
============================================================================
@Table(name = "t_emp_ip")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class EmpIpp implements java.io.Serializable {
private EmpIppId id;
private String empNo;
private String ipName;
private String netaddrNo;
private String isDown;
public EmpIpp() {
}
public EmpIpp(EmpIppId id) {
this.id =id;
}
public EmpIpp(EmpIppId id, String empNo, String ipName,
String netAddrNo, String isDown) {
this.id = id;
this.empNo = empNo;
this.ipName = ipName;
this.netaddrNo = netaddrNo;
this.isDown = isDown;
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "ippId", column = @Column(name = "IPP_ID", nullable = false)),
@AttributeOverride(name = "empId", column = @Column(name = "EMP_ID", nullable = false)) })
public EmpIppId getId() {
return this.id;
}
public void setId(EmpIppId id) {
this.id = id;
}
public String getEmpNo() {
return empNo;
}
public void setEmpNo(String empNo) {
this.empNo = empNo;
}
//省略了get和set方法
}
==================================================================
EmpIppId类
=================================================================
@Embeddable
public class EmpIppId implements java.io.Serializable {
private Integer empId;
private Integer ippId;
public EmpIppId() {
}
public EmpIppId(Integer ippId, Integer empId) {
this.empId = empId;
this.ippId = ippId;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof EmpIppId))
return false;
EmpIppId castOther = (EmpIppId) other;
return ((this.getIppId() == castOther.getIppId()) || (this
.getIppId() != null
&& castOther.getIppId() != null && this.getIppId()
.equals(castOther.getIppId())))
&& ((this.getEmpId() == castOther.getEmpId()) || (this
.getEmpId() != null
&& castOther.getEmpId() != null && this
.getEmpId().equals(castOther.getEmpId())));
}
public int hashCode() {
int result = 17;
result = 37* result+ (getIppId() == null ? 0 : this.getIppId()
.hashCode());
result = 37* result+ (getEmpId() == null ? 0 : this.getEmpId()
.hashCode());
return result;
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
empId = empId;
}
public Integer getIppId() {
return ippId;
}
public void setIppId(Integer ippId) {
ippId = ippId;
}
}
======================================================================
hbm.xml文件
======================================================================
========================================================================
运行程序代码
========================================================================
public static void main(String[] args) {
Session session = HibernateStatic.getSession();
Transaction t =session.beginTransaction();
int rowNum=0;
try {
EmpIpp ei =null;
EmpIppId eii = null;
ei = new EmpIpp();
eii = new EmpIppId();
eii.setEmpId(new Integer(1));//目前表中无数据
eii.setIppId(new Integer(1));
ei.setId(eii);
ei.setIsDown("1");
session.save(ei);
//Query query =session.createQuery("select count(*) from EmpIpp ");
//rowNum = Integer.parseInt(query.uniqueResult().toString());
t.commit();
} catch (HibernateException e) {
e.printStackTrace();
}
System.out.println("data ---------: "+rowNum);
}
*******************************************************************************
*******************************************************************************
*******************************************************************************
以上是程序的源代码:
运行之后再eclipse的控制台报如下错误:(查询这个表是正常的,就是save出错)
DEBUG org.hibernate.pretty.Printer - com.flyoa.entity.empipp.EmpIpp{netaddrNo=null, empNo=null, ipName=null, isDown=1, id=component[empId,ippId]{empId=null, ippId=null}}
DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG org.hibernate.SQL - insert into t_emp_ip (EMP_NO, IP_NAME, NETADDR_NO, IS_DOWN, EMP_ID, IPP_ID) values (?, ?, ?, ?, ?, ?)
DEBUG org.hibernate.jdbc.AbstractBatcher - Executing batch size: 1
DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG o.h.util.JDBCExceptionReporter - Could not execute JDBC batch update [insert into t_emp_ip (EMP_NO, IP_NAME, NETADDR_NO, IS_DOWN, EMP_ID, IPP_ID) values (?, ?, ?, ?, ?, ?)]
java.sql.BatchUpdateException: Column 'EMP_ID' cannot be null
WARN o.h.util.JDBCExceptionReporter - SQL Error: 1048, SQLState: 23000
ERROR o.h.util.JDBCExceptionReporter - Column 'EMP_ID' cannot be null
ERROR o.h.e.d.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
Caused by: java.sql.BatchUpdateException: Column 'EMP_ID' cannot be null
... 8 common frames omitted
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at com.qianwei.timer.TimerTest.main(TimerTest.java:83)
Caused by: java.sql.BatchUpdateException: Column 'EMP_ID' cannot be null
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665)
======================================================================================
2010年6月22日 20:07