1
考虑代码:休眠时间映射在TIMESTAMP WITH时间00:00:00
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.ServletException;
import model.UserModel;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
/**
* This class adds initial values to the DB
* @author X2
*
*/
public class InitialRecords {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static final String HIBERNATE_USERS = "usersModel.hbm.xml";
/**
*
* @param myDabatase
* @throws ServletException
*/
public void insertInitialRecords(ChatDatabase myDabatase) throws ServletException
{
Session session = null;
java.util.Date date= new java.util.Date();
Timestamp stamp = new Timestamp(date.getTime()); // get time & date
Timestamp stamp2 = new Timestamp(date.getTime()); // get time & date
UserModel user = new UserModel("jackson" , stamp , "Some cool message");
UserModel user2 = new UserModel("yohan" , stamp2 , "Another cool message");
// Setting up HIBERNATE
// now use Hibernate to put it in the DB
try
{
Configuration cfg = new Configuration().addResource(HIBERNATE_USERS).configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
// now insert the record of the person using Hibernate
session = sessionFactory.openSession();
System.out.println("Inserting Person records");
Transaction tx = session.beginTransaction();
// save the persons before hibernating
session.save(user);
session.save(user2);
// execute
tx.commit();
System.out.println("Done");
session.close();
sessionFactory.close();
}
catch (Exception exp)
{
System.out.println("Error , here is the description :");
System.out.println(exp.toString());
System.exit(0); // abort the program
}
}
}
的的usermodel类:
package model;
import java.sql.Timestamp;
public class UserModel
{
static private int ctr;
private int id;
private String username;
private Timestamp date;
private String message;
/**
* Ctor
* @param username
* @param stamp
* @param msg
*/
public UserModel(String username , Timestamp stamp , String msg)
{
id = ctr + 1;
ctr++;
this.username = username;
this.date = stamp;
this.message = msg;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Timestamp getDate() {
return date;
}
public void setDate(Timestamp date) {
this.date = date;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
当我映射记录用户和user2时,Hibernate映射随着时间的00:00:00
如何ç记录我解决了它?
非常感谢
更新:
这里是我创建表:
public void createHibernateTableMessagesUsers() throws SQLException, Exception
{
// this code assumes that the database "DATABASE_NAME" already exists
// so the user must create that database before running the below code !!!
try
{
Class.forName(FORNAME_URL);
m_connectionHibernate = DriverManager.getConnection(URL , USERNAME , PASSWORD);
m_statementHibernate = m_connectionHibernate.createStatement();
m_statementHibernate.executeQuery("USE " + DATABASE_NAME); // the name of the DATABASE
m_statementHibernate.executeUpdate (
"CREATE TABLE IF NOT EXISTS "+ MESSAGES_TABLE +" ("
+ "Username CHAR(40) , Message TEXT , Date TIMESTAMP ,"
+ " id INT AUTO_INCREMENT primary key NOT NULL" + ")");
}
'UserModel'的代码是什么? –
也许你的数据库的列类型只是DATE,而不是没有TIMEZONE的TIMESTAMP。 –
@ Alexandre Lavoie:我更新了帖子......请看看。 – ron