我的模特课程:
package com.igate.model;
import java.util.Date;
//import java.sql.Date;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Employee")
public class Employee
{
@Id
@Column(name="empid")
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
@Column(name="empname")
String name;
String job;
Date hiredate;
int sal;
int deptid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getDeptid() {
return deptid;
}
public void setDeptid(int deptid) {
this.deptid = deptid;
}
public void setHiredate(Timestamp hiredate) {
this.hiredate = hiredate;
}
}
我的cfg文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--connection properties -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">MenhiBaba</property>
<!--hibernate properties -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hb2ddl.auto">validate</property>
<property name="show_sql">true</property>
<!-- <property name="use_sql_comments">true</property>
<property name="format_sql">true</property>
-->
<!-- Resource mapping -->
<mapping class="com.igate.model.Employee"/>
</session-factory>
</hibernate-configuration>
我的错误:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into Employee (deptid, hiredate, job, empname, sal) values (?, ?, ?, ?, ?)
problem in creating session object
Exception in thread "main" java.lang.ExceptionInInitializerError
at Test.main(Test.java:37)
Caused by: org.hibernate.exception.GenericJDBCException: could not insert: [com.igate.model.Employee]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at Test.main(Test.java:27)
Caused by: java.sql.SQLException: General error, message from server: "Field 'empid' doesn't have a default value"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1977)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1272)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2236)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1741)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1588)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 16 more
主要方法:
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.igate.model.Employee;
import com.igate.utils.HibernateUtils;
public class Test
{
public static void main(String[] args) {
Session session =null;
Transaction txn = null;
try
{
session = HibernateUtils.getSessionFactory().openSession();
txn = session.beginTransaction();
txn.begin();
Employee emp = new Employee();
emp.setId(101);
emp.setName("Rahim");
emp.setJob("PO");
emp.setHiredate(new Date(24-11-2011));
emp.setSal(5500);
emp.setDeptid(20);
int i=(Integer)session.save(emp);
if(i>=0)
System.out.println("data saved");
else
System.out.println("data not saved");
txn.commit();
session.close();
}catch(Exception e)
{
System.out.println("problem in creating session object");
throw new ExceptionInInitializerError(e);
}
}
}
在此之前我执行了很多程序,但我没有遇到任何问题。但是我不知道我的代码中有什么问题。我在这个简单的程序上花费了2个小时。请有人帮帮我吗?
提前致谢。
答案 0 :(得分:0)
如果要自动分配,请将自动增量字段设置为NULL或0。有关详细信息,请参阅How to insert data to MySQL having auto incremented primary key?