我正在尝试在HSQLDB中的表的列中插入Date类型条目。问题是我正在使用Hibernate这样做,有人可以帮我解决这个问题吗?
该表格如下:
CREATE TABLE TEST(
WORKINGDATE DATE NOT NULL
)
Bean文件如下:
import java.util.Date;
public class Test {
private Date workingDate;
public Date getWorkingDate() {
return workingDate;
}
public void setWorkingDate(Date workingDate) {
this.workingDate = workingDate;
}
@Override
public String toString() {
return "Test [workingDate=" + workingDate + "]";
}
}
XML Mapping如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="bean.hibpackage.Test" table="TEST">
<property name="workingDate" column="WORKINGDATE" type="date" />
</class>
</hibernate-mapping>
现在测试场景是:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import bean.hibpackage.Test;
public class TestHibernate2 {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test = new Test();
String date_s = "2015-02-22";
{
//How to format the above given string to a date type so that the
//program will work
}
test.setWorkingDate(?);
Session ses = null;
try {
SessionFactory sessions = null;
sessions = new Configuration().configure().buildSessionFactory();
ses = sessions.openSession();
Transaction tran = ses.beginTransaction();
ses.save(test);
tran.commit();
} finally {
ses.close();
}
}
}
答案 0 :(得分:0)
您只需要解析Date
字符串表示形式:
String date_s = "2015-02-22";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
test.setWorkingDate(sdf.parse(date_s));