<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping>
  <class name="hibernate.Students" table="students">
     
  
    <id name="id" column="ID" type="long">
      <generator class="increment"/>
    </id>
    <property name="name"  column="NAME"  type="string" not-null="true" />  
    <property name="timestamp"  column="time"  type="timestamp"  />  </class></hibernate-mapping>

解决方案 »

  1.   

    我可以 插入数据 ,并且能从 id和 name 查找出记录,但是唯独不能从时间查!
    老是抱错,稀奇了!
      

  2.   

    XX time=XX;(String time="2006-06-03";)
    Query q = session.createQuery("from Students s where s.time=?");
    q.setEntity(0,time);
      

  3.   

    麻烦 大哥再写清楚一点
    XX time=XX;(String time="2006-06-03";)
    是什么意思 啊?
    不明白啊
      

  4.   

    public List find(Timestamp time) throws Exception {
    List list = null;
    try {
    session = HibernateUtil.openSession();
    tx = session.beginTransaction();
    Query q = session.createQuery("from Students s where s.time=:time");
    q.setEntity("time",time);
    list=q.list();
    tx.commit();
    } catch (HibernateException e) {
    e.printStackTrace();
    } finally {
    if (session != null)
    HibernateUtil.closeSession(session);
    }
    return list;
    }最好在这里改改!感谢
      

  5.   

    public List find(Timestamp time) throws Exception {
     
    try {
    session = HibernateUtil.openSession();
    tx = session.beginTransaction();
    Query q = session.createQuery("from Students s where s.time=?");
    q.setEntity(0,time);
    List  list=q.list();
    tx.commit();
    } catch (HibernateException e) {
    e.printStackTrace();
    } finally {
    if (session != null)
    HibernateUtil.closeSession(session);
    }
    return list;
    }
      

  6.   

    疯掉了!~~~
    我这样写把 String 转变 timestamp, 不对吗?List list =s.find(Timestamp.valueOf("2006-04-05 14:08:14"));真的好复杂啊 ~ 
    天啊◎ 真搞不懂了!could not resolve property: time of: hibernate.Students [from hibernate.Students s where s.time=?]
      

  7.   

    经过feixue6511(飞)的提醒。。总于做出来了 时间查询
    其实 在mysql里面 时间是不用转变的 就用String就可以了
    但是我发现 Students.hbm.xml 配置很重要!
      <property name="rgtimestamp"  column="rgtimestamp"  type="timestamp"  />
    就是 name 和 column 最好是统一个!省的以后麻烦!
      

  8.   

    我把 代码都贴出来!
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping>
      <class name="hibernate.Students" table="students">
         
      
        <id name="id" column="ID" type="long">
          <generator class="increment"/>
        </id>
        <property name="name"  column="NAME"  type="string" not-null="true" />  
        <property name="rgtimestamp"  column="rgtimestamp"  type="timestamp"  />  </class></hibernate-mapping>package hibernate;import java.io.Serializable;
    import java.sql.Timestamp;public class Students implements Serializable {
    private long id; private String name;

    private Timestamp rgtimestamp; public Students() {
    } public long getId() {
    return id;
    } public void setId(long id) {
    this.id = id;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    }
    public Timestamp getRgtimestamp() {
    return rgtimestamp;
    } public void setRgtimestamp(Timestamp rgtimestamp) {
    this.rgtimestamp = rgtimestamp;
    }
    }
      

  9.   

    package hibernate;import net.sf.hibernate.*;
    import net.sf.hibernate.cfg.*;public class HibernateUtil {
    public static SessionFactory sessionFactory;
    static {
    try {
    Configuration config = new Configuration();
    config.addClass(Students.class);
    sessionFactory = config.buildSessionFactory();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public static Session openSession() throws HibernateException {
            Session session = sessionFactory.openSession();
    return session;
    } public static void closeSession(Session session) throws HibernateException {
    session.close(); }
    }
      

  10.   

    package hibernate;import java.util.List;
    import java.sql.Timestamp;
    import java.sql.Date;
    import net.sf.hibernate.*;
    import net.sf.hibernate.cfg.Configuration;public class StudentsDAO {
    Session session = null; Transaction tx = null; public void AddMessage(String str) throws Exception {
    try {
    session = HibernateUtil.openSession();
    tx = session.beginTransaction();
    Students sds = new Students();
    sds.setName(str);
    session.save(sds);
    tx.commit();
    } catch (HibernateException e) {
    e.printStackTrace();
    tx.rollback();
    } finally {
    if (session != null)
    HibernateUtil.closeSession(session);
    } } public void EditMessage() throws Exception {
    try {
    session = HibernateUtil.openSession();
    tx = session.beginTransaction();
    Students sds = new Students();
    sds.setId(2);
    sds.setName("my first job");
    session.update(sds);
    tx.commit();
    } catch (HibernateException e) {
    e.printStackTrace();
    tx.rollback();
    } finally {
    if (session != null)
    HibernateUtil.closeSession(session);
    } } public void DelMessage(long id) throws Exception {
    try {
    session = HibernateUtil.openSession();
    tx = session.beginTransaction();
    Object obj = session.get(Students.class, new Long(id));
    session.delete(obj);
    tx.commit();
    } catch (HibernateException e) {
    e.printStackTrace();
    tx.rollback();
    } finally {
    if (session != null)
    HibernateUtil.closeSession(session);
    } } public List getAll() throws Exception {
    List list = null;
    try {
    session = HibernateUtil.openSession();
    tx = session.beginTransaction();
    list = session.createQuery("from Students s ").list();
    tx.commit();
    } catch (HibernateException e) {
    e.printStackTrace();
    } finally {
    if (session != null)
    HibernateUtil.closeSession(session);
    }
    return list;
    }

    public List find(String rgtimestamp) throws Exception {
    List list = null;
    try {
    session = HibernateUtil.openSession();
    tx = session.beginTransaction();
    Query q = session.createQuery("from Students s where s.rgtimestamp=?");
    q.setString(0,rgtimestamp);
    list=q.list();
    tx.commit();
    } catch (HibernateException e) {
    e.printStackTrace();
    } finally {
    if (session != null)
    HibernateUtil.closeSession(session);
    }
    return list;
    }

    public static void main(String[] args) throws Exception {
    StudentsDAO s = new StudentsDAO();
    List list =s.find("2006-04-05 18:02:07");
    for(int i=0;i<list.size();i++){
    Students sds=new Students();
    sds=(Students)list.get(i);
    System.out.print(sds.getId()+" ");
    System.out.print(sds.getName()+" ");
    System.out.println(sds.getRgtimestamp());
    }
    }
    }