hibernate问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate问题相关的知识,希望对你有一定的参考价值。

初配hibernate
就是映射一个表Customer,然后读取内容显示.
错误提示:

org.hibernate.exception.JDBCConnectionException: could not load an entity: [Customer#1]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1359)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:116)
......
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.setupImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.<init>(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.<init>(Unknown Source)
... 20 more
是什么问题啊
这个解决了
是读取时必须写成
Iterator it = query.list().iterator();
用Iterator it = query.iterator();就出现上面那个问题了
配置没问题
现在是用个insertdata()出错
提示:org.hibernate.exception.GenericJDBCException: could not insert: [Customer]
插入代码:
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
Customer p1 = new Customer(); p1.setCid(new Integer(3));p1.setPassword("sdfs"); p1.setUsername("dsffsdf");
session.save(p1);
tx.commit();
HibernateSessionFactory.closeSession();
插入的语句有问题吗?
查询都没问题的,配置应该是好着的吧

在配置文件哪块会说明表的读写控制啊,我是用myeclipse下database explorer视图映射的customer,正常建的表也没有设置读写权限什么的.

参考技术A 不插入的语句的问题,你看看配置文件。这个表是不是可写的。 参考技术B 我不知道你的实体映射类是怎么定义的,怎么会是p1.setCid(new Integer(3))呢?为什么要new Integer(3)呢,难道你定义成了封装类类型?那数据库字段又是什么类型呢? 参考技术C 未知源
你配置文件不对吧
参考技术D Session 关闭没? 第5个回答  2007-10-07 没关闭

Hibernate 问题

小弟初学 Hibernate 不会配置了 郁闷 帮忙看一下吧 谢谢了
datasource.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
datasource.url=jdbc:sqlserver://localhost:1433; DatabaseName=newjspdev
datasource.username=javaee
datasource.password=javaee
datasource.maxActive=10
datasource.maxIdle=2
datasource.maxWait=120000
datasource.defaultAutoCommit=true
datasource.whenExhaustedAction=1
datasource.validationQuery=select 1 from dual
datasource.testOnBorrow=true
datasource.testOnReturn=false

hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.jdbc.batch_size=25
hibernate.jdbc.fetch_size=50
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop
小弟不知道这些参数都是干什么的 帮忙解释一下
万分感谢!

datasource.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver ---注册jdbc驱动名称
datasource.url=jdbc:sqlserver://localhost:1433;--注册jdbc url地址 DatabaseName=newjspdev ---数据库名称
datasource.username=javaee --数据库用户名
datasource.password=javaee --数据库密码
datasource.maxActive=10 --表示连接池的最大连接数量10
datasource.maxIdle=2 --表示连接池的空闭数量2
datasource.maxWait=120000 --最长连接等待时间是120000毫秒
datasource.defaultAutoCommit=true --自动提交为true(就是不用自己写commit语句了)
datasource.whenExhaustedAction=1 --指定在池中借出对象的数目已达极限的情况下,调用它的borrowObject方法时的行为datasource.validationQuery=select 1 from dual --验证连接是否成功(sqlserver没有dual这张表吧,应该是oracle数据库才用这个语句验证的吧)
datasource.testOnBorrow=true
datasource.testOnReturn=false

hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.jdbc.batch_size=25
hibernate.jdbc.fetch_size=50
hibernate.show_sql=true --在后台显示执行的sql语句
hibernate.hbm2ddl.auto=create-drop --可以帮助你实现正向工程,即由java代码生成数据库脚本,进而生成具体的表结构.
(
它包含4个属性:

create : 会根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成表,哪怕2次没有任何改变

create-drop : 根据model类生成表,但是sessionFactory一关闭,表就自动删除

update : 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行

validate : 只会和数据库中的表进行比较,不会创建新表,但是会插入新值
)
参考技术A 能发下你的代码么·? 参考技术B TblValidate的主键策略是assigned
,在save之前必须手动给主键字段赋值。
最好能多学点英语就多学点,有好处。

以上是关于hibernate问题的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate 问题

hibernate 一对多双向的问题~~~

将 Hibernate 3 升级到 Hibernate 4 的问题

面试 hibernate第二篇hibernate相关问题

[Hibernate开发之路]Hibernate问题

hibernate自动生成表达问题