昨天在用hibernate向数据库提交数据时,遇到一个奇怪的问题:数据库中有个表,ID是主键,用sequences生成,还有两个列是date在java中用new Date生成当前日期,并用hibernate插入数据库时,两个日期列没问题,但ID由于值过大,变成了科学计数的方式可是当把new Date生成的日期转成string再转成日期型时,就一切正常了,有没有高手知道是咋回事呀
调试欢乐多
你用long类型看看!!
toad / pl/sql这2个我都用过
5楼提示了,用Timestamp 读取日期和时间! 对应Oracle里面的timestamp
把代码贴出一部分吧,希望能帮找出问题。表的映射文件
<?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 package="ge.threatscan.dataaccess.domainobject">
<class name="Command" table="RMD_COMMAND" >
<id name="id" type="int" column="ID" >
<generator class="sequence">
<param name="sequence" >RMD_COMMAND_SEQ</param>
</generator>
</id>
<property name="userID" type="string" column="USER_ID"></property>
<property name="status" type="string" column="STATUS"></property>
<property name="command" type="string" column="COMMAND"></property>
<many-to-one name="modem"
class="ge.threatscan.dataaccess.domainobject.Modem"
column="MODEM_ID"
lazy="false"
/>
<property name="commandParameters" type="string" column="COMMAND_PARAMS"></property>
<property name="createTime" type="timestamp" column="CREATE_TIME"></property>
<property name="modifiedTime" type="timestamp" column="MODIFIED_TIME"></property> 表字段类
package ge.threatscan.dataaccess.domainobject;import java.io.Serializable;
import java.util.Date;/**
*
* Domain object class is is the foundation of the whole program
* as it is the data structure and is used in many layers. It is
* almostly conresponding to the table in database.
*
* This class is the data structure of Command and contains all of
* useful data as properties.
*
*/
public class Command implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String status;
private String userID;
private String command;
private Modem modem;
private String commandParameters;
private Date createTime = null;
private Date modifiedTime = null;
/**
* construct method
*
*/
public Command() {
super();
}
/**
* @return the status
*/
public String getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(String status) {
this.status = status;
}
/**
* @return the userID
*/
public String getUserID() {
return userID;
}
/**
* @param userID the userID to set
*/
public void setUserID(String userID) {
this.userID = userID;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} /**
* @return the command
*/
public String getCommand() {
return command;
} /**
* @param command the command to set
*/
public void setCommand(String command) {
this.command = command;
} /**
* @return the commandParameters
*/
public String getCommandParameters() {
return commandParameters;
} /**
* @param commandParameters the commandParameters to set
*/
public void setCommandParameters(String commandParameters) {
this.commandParameters = commandParameters;
} /**
* @return the createTime
*/
public Date getCreateTime() {
return createTime;
} /**
* @param createTime the createTime to set
*/
public void setCreateTime(Date createTime) {
this.createTime = createTime;
} /**
* @return the modifiedTime
*/
public Date getModifiedTime() {
return modifiedTime;
} /**
* @param modifiedTime the modifiedTime to set
*/
public void setModifiedTime(Date modifiedTime) {
this.modifiedTime = modifiedTime;
} /**
*
* @return the modem
*/
public Modem getModem() {
return modem;
} /**
*
* @param modem
*/
public void setModem(Modem modem) {
this.modem = modem;
}}
插入数据类
package ge.threatscan.dataaccess.dao;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;import sun.util.calendar.Gregorian;import ge.threatscan.dataaccess.HibernateSessionFactory;
import ge.threatscan.dataaccess.domainobject.Command;
import ge.threatscan.dataaccess.domainobject.Modem;
import ge.threatscan.dataaccess.util.ThreatScanDBException;public class CommandDAO {
/**
* This method is used to save a specified Command according to Command into
* database.
*
* @param command
* @return boolean
* @throws ThreatScanDBException
*/
public boolean saveCommand(Command command) throws ThreatScanDBException {
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(command);
tx.commit();
return true;
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
throw new ThreatScanDBException(e.getMessage());
} finally {
session.close();
}
} public static Date convertDate(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
String temp = sdf.format(date);
date = sdf.parse(temp);
} catch (ParseException e) {
//e.printStackTrace();
System.out.println("error: " + e);
}
return date;
} public static void main(String[] args) {
CommandDAO commandDAO = new CommandDAO();
try {
Command command = new Command();
command.setCommand("9");
Modem modem = new Modem();
modem.setId(1); command.setModem(modem);
command.setStatus("2");
command.setUserID("wd");
Date date = new Date();// date = convertDate(date);//打开这个就没问题 command.setCreateTime(date);
command.setModifiedTime(date);
System.out.println(commandDAO.saveCommand(command));
// System.out.println((new Date().toString()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
</class></hibernate-mapping>