<?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>
<!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>
解决方案 »
- 关于《重构 改善既有代码的设计》第一章的例子
- 一道算法题,求高手帮忙解答,谢谢了
- 字符串的截取
- tamcat给出的信息,请高后赐教是什么原因
- 大家来讨论讨论,struts怎么不做一些和asp.net中的广告,分页控件相似的控件呢?
- 除了hibernate外, 还有什么O/R mapping??
- struts 中的多模块配置问题。。
- 请问大家这是什么错误:[SQLServer JDBC Driver]Connection reset by peer: socket write error?
- 高分求jboss文档
- asix2 请求超时。求解决思路啊。在线等了
- JBOSS 数据源绑定问题,请高手指教
- struts分页问题
老是抱错,稀奇了!
Query q = session.createQuery("from Students s where s.time=?");
q.setEntity(0,time);
XX time=XX;(String time="2006-06-03";)
是什么意思 啊?
不明白啊
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;
}最好在这里改改!感谢
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;
}
我这样写把 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=?]
其实 在mysql里面 时间是不用转变的 就用String就可以了
但是我发现 Students.hbm.xml 配置很重要!
<property name="rgtimestamp" column="rgtimestamp" type="timestamp" />
就是 name 和 column 最好是统一个!省的以后麻烦!
<?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;
}
}
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(); }
}
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());
}
}
}