初涉 hibernate
Forest 有兴趣在下个项目中用hibernate,于是看看停停的试了试, 由于用了个最新版本的 hibernate 而走了很多弯路,网上的教程都是hibernate2 的,幸亏还有 英文版的 hibernate3 的 reference,终于把环境搭好了,tomcat5.0.27+hibernate3.1+MySql5.0。却碰到一个 sql 的错调不好了,郁闷中……来这逛逛
高人有空的话帮忙看看,我记得冰子好像是个 hibernate 高手:)指点菜鸟一下。
我的Cat.hbm.xml
<?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="net.hibernate.examples.quickstart.Cat" table="CAT">
<id name="id" type="string" unsaved-value="null" >
<column name="CAT_ID" sql-type="varchar(20)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name">
<column name="NAME" length="20" not-null="true"/>
</property>
<property name="sex"/>
<property name="weight"/>
</class>
</hibernate-mapping>
我的数据库脚本:
CREATE DATABASE quickstart;
USE quickstart;
CREATE TABLE cat (
cat_id varchar(20) NOT NULL,
name varchar(20) NOT NULL,
sex char(1),
weight float,
PRIMARY KEY (cat_id)
);
我的测试 jsp 页面:
<%@ page language="java" pageEncoding="GB2312" %>
<%@ page import="org.hibernate.Transaction"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="org.hibernate.cfg.*"%>
<%@ page import="org.hibernate.Query"%>
<%@ page import="net.hibernate.examples.quickstart.HibernateUtil"%>
<%@ page import="net.hibernate.examples.quickstart.Cat"%>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head><title>Lomboz JSP</title></head>
<body bgcolor="#FFFFFF">
<%
System.out.print("here!1");
try
{
//添加一只Cat
Session ses = HibernateUtil.currentSession();
Transaction tx= ses.beginTransaction();
///*
Cat princess = new Cat();
princess.setName("Jenny");
princess.setSex('F');
princess.setWeight(7.4f);
ses.save(princess);
tx.commit();
System.out.print("here!7");
HibernateUtil.closeSession();
// */
//读取库里所有Cat
///*
ses = HibernateUtil.currentSession();
tx= ses.beginTransaction();
Query query = ses.createQuery("select c from Cat as c where c.sex = :sex");
query.setCharacter("sex", 'F');
for (Iterator it = query.iterate();it.hasNext(); ){
Cat cat = (Cat) it.next();
out.println("Female Cat: " + cat.getName() +"<br>");
}
tx.commit();
HibernateUtil.closeSession();
//*/
}
catch(Exception excep){
excep.printStackTrace();
throw excep;
}
%>
</body>
</html>
只执行读取查询没有问题,能正常查出每只 cat,添加一只 cat 的时候报错:
警告: SQL Error: 1406, SQLState: 22001
2005-12-1 15:58:33 org.hibernate.util.JDBCExceptionReporter logExceptions
严重: Data too long for column 'cat_id' at row 1
2005-12-1 15:58:33 org.hibernate.event.def.AbstractFlushingEventListener perform
Executions
严重: Could not synchronize database state with session
org.hibernate.exception.DataException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:75)
…………
at java.lang.Thread.run(Thread.java:534)
Caused by: java.sql.BatchUpdateException: Data too long for column 'cat_id' at row 1
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1540)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
…………
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
Caused by: java.sql.BatchUpdateException: Data too long for column 'cat_id' at row 1
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1540)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 38 more
看起来就是一个 cat_id 数据过长,我最开始的时候没注意,把Cat.hbm.xml中写成了<column name="CAT_ID" sql-type="char(32)" not-null="true"/>超过了数据库定义的长度20,但是后来改成20了,还是不行......对 MySql 和 hibernate 都不熟
逛完继续调去,哪位大侠知道的话望赐教。
改20应该就行了,tomcat重新启动一下
我已经改成20,并且重启过了,网上有人说是字符集的问题,先执行sql:SET NAMES GBK(或其他任意字符集),但是我不知道在hibernate里面怎么写让他先执行这个sql。
uuid is 128bit long, if hex, it's 32. cat_id is not long enough.
是说这个?
MYSQL4。1 以上版本
建库要用CREATE DATABASE `bbscs6` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;这个语句。
还有:datasource.url=jdbc:mysql://192.168.0.1:3306/bbscs6?useUnicode=true&characterEncoding=utf8
非常感谢楼上各位!
根据匿名高人的提示,重建了数据库,把 id 字段改成32位,结果好了,可以正常运行了。