]]>hibernate和jdbc事务l一控制http://www.aygfsteel.com/leekiang/archive/2009/12/11/305492.htmlleekiangleekiangThu, 10 Dec 2009 16:14:00 GMThttp://www.aygfsteel.com/leekiang/archive/2009/12/11/305492.htmlhttp://www.aygfsteel.com/leekiang/comments/305492.htmlhttp://www.aygfsteel.com/leekiang/archive/2009/12/11/305492.html#Feedback0http://www.aygfsteel.com/leekiang/comments/commentRss/305492.htmlhttp://www.aygfsteel.com/leekiang/services/trackbacks/305492.html
原理是保证?connection 的唯一性?br />
jdbc我是调spring的jdbcTemplate来操作,
l过试。在同一个数据源的情况下直接使用Hibernate的TxManager可以同步事务Q问题解冟?br />
?br />
Rod Johnson的话Q?
引用
It is possible--and sometimes useful--to have coordinated transactions
for both. Your JDBC transactions will be managed by the
HibernateTransactionManager if you work with the same JDBC DataSource
in the same transaction. That is, create the SessionFactory using
Spring's SessionFactoryBean using the same DataSource that your
JdbcTemplates use.
The only issue to watch, of course, is that you may be invalidating
your Hibernate cache by JDBC changes. Generally I find it best to use
JDBC to update only tables that don't have Hibernate mappings.
Juergen Hoeller的话Q?
引用
As Rod said, simply keep using HibernateTransactionManager, which
auto-detects the DataSource used by Hibernate and seamlessly exposes
Hibernate transactions as JDBC transactions for that DataSource. JDBC
code that accesses the same DataSource via Spring will automatically
participate in such transactions.
Note that you must specify the DataSource for Hibernate via
LocalSessionFactoryBean's "dataSource" property to allow
HibernateTransactionManager to auto-detect it. Alternatively, you can
explicitly pass the DataSource to HibernateTransactionManager's
"dataSource" property. http://www.fireflow.org/redirect.php?tid=498
Rod Johnson在spring 论坛中有一句话很好的ȝ?jin)如何在试中处理hibernate~存: Remember that you can clear the Hibernate session, removing objects already associated with it. This is often necessary before requerying in tests, and solves most (if not all) problems. I typically use JDBC for verification. The pattern is - do Hibernate operation - flush Hibernate session - issue JDBC query to verify results That way I'm verifying what Hibernate did to the database in the same transaction.
]]>hibernate异常"Found shared references to a collection"http://www.aygfsteel.com/leekiang/archive/2008/10/31/237908.htmlleekiangleekiangFri, 31 Oct 2008 14:11:00 GMThttp://www.aygfsteel.com/leekiang/archive/2008/10/31/237908.htmlhttp://www.aygfsteel.com/leekiang/comments/237908.htmlhttp://www.aygfsteel.com/leekiang/archive/2008/10/31/237908.html#Feedback2http://www.aygfsteel.com/leekiang/comments/commentRss/237908.htmlhttp://www.aygfsteel.com/leekiang/services/trackbacks/237908.html假定,ParentcL一个Set属性,里面攄是Son。如果查?from Parent",某个Parent哪怕一个Son都没有,那个Set属性不?x)?f)nullQ而是一个空集合?br />q时候如果你Parent newP=new Parent();然后BeanUtils.copyPropertis(newP,origP);最后就?x)报hibernate异常"Found shared references to a collection"?br />?hibernate在什么时机发?两个对象׃n一个集?的情늚Q我q边的例子是在下一ơ查询时发现的?br /> 原因: BeanUtils.copyPropertis是浅拯Q导致这两个对象引用的Set是同一个Set,q在hibernate中是不允许的Q参见Hibernate referenceW?章的"Two entities may not share a reference to the same collection
instance"?br />q种问题常见于复制对象时?br />如何解决:newP.setSonSet(null); q有原因可能是ƈ发操?http://www.aygfsteel.com/fastzch/archive/2006/12/22/89520.html
]]>hibernate杂记http://www.aygfsteel.com/leekiang/archive/2008/02/16/180148.htmlleekiangleekiangFri, 15 Feb 2008 21:04:00 GMThttp://www.aygfsteel.com/leekiang/archive/2008/02/16/180148.htmlhttp://www.aygfsteel.com/leekiang/comments/180148.htmlhttp://www.aygfsteel.com/leekiang/archive/2008/02/16/180148.html#Feedback0http://www.aygfsteel.com/leekiang/comments/commentRss/180148.htmlhttp://www.aygfsteel.com/leekiang/services/trackbacks/180148.htmlselect new Map(a.id as id,a.name as name,b.type as type) from A a,B b where a.id=b.aId; ... List mapList=query.list(); ....
select new MyObj(id,name,deptid,deptName) from tb_usr,tb_dept where ..... q种方式要求MyObjcL对应的构造方?br /> 2.HQL supports subqueries in the where clause. We can’t think of many good uses for subqueries in the from clause, although select clause subqueries might be a nice future extension. 不支持from后的子查询,支持where子查?br />http://blog.zol.com.cn/655/article_654256.html
3Q?String sql="select {fi.*} from FuncInfo fi " + (tng) (tng) "left join RoleSubFunc rsf on fi.FuncId=rsf.FuncId "+ (tng) (tng) (tng) (tng) (tng) (tng) "left join RoleInfo ri on rsf.RoleId=ri.RoleId "+ (tng) (tng) (tng) (tng) (tng) (tng) "left join UserRole ur on ri.RoleId=ur.RoleId "+ (tng) (tng) (tng) (tng) (tng) (tng) "where ur.UserId='"+userId+"'";//可以无限向上扄U?br /> (tng)List list =session.createSQLQuery(sql).addEntity("fi", FuncInfo.class).list();
4,Z么Hibernate 3中的HQL无法查询汉字 使用同样的代码和配置文gQ在Hibernate 2上完全没有问题,在Hibernate 3中,使用如下HQL查询Q无法得到正的l果集:(x) String hql = "from story where title like '%汉字%'"; Query q = session.createQuery(hql); 但用下面的HQL查询Q却可以得到正确l果集:(x) String hql = " from story where title like '%english%'"; Query q = session.createQuery(hql); {:(x)如果采用的是拼接HQL的方式,从Hibernate 2升到Hibernate 3实?x)出现汉字ؕ码问题。在控制C可以看到QSQL的汉字部分变成了(jin)qQ?br />[DEBUG] 2005-08-14 14:33:58 org.hibernate.SQL - "select story0_.content from story as story0_ where story0_.title like '%&–°é?amp;&Št&€? 在Hibernate中,查询时应量使用占位W的写法Q如下)(j)Q这h可以避免q问题Q又可以避免潜在的SQL注入dQ?br />getHibernate().find("from story where title like ? ", "%汉字%") ??update TbTest set name='张三' "也会(x)有类似的问题,要改为占位符的写?br />http://xqfy1983.blog.sohu.com/61054398.html
5,使用Hibernate.DATEӞ得到的时间不带分U?br /> (tng) 可以考虑使用Hibernate.CALENDAR,实际cd是java.util.GregorianCalendar,然后再getTime() 6,报表查询 (tng) select new Table(t1.a,t2.b ...) (tng) from ..... (tng) select new Object(vo.id,vo.name) from VO vo (tng) q种写法需事先写好构造方?br />7,List cats = sess.createSQLQuery("select {cat.*} from cats cat") (tng) (tng) (tng) (tng) .addEntity("cat", Cat.class); (tng) (tng) (tng) (tng) .setMaxResults(50); (tng) (tng) (tng) (tng) .list(); 8,貌似hibernated或修改对?如果字符串类型的属性的gؓ(f)I字W串(大小?),则自动当null处理 9,如果在hbm中某属性设为not nullQ然后如果po中的该属性ؓ(f)nullQ则hibernate?x)抛异?br /> (tng) 但如果在hbm中设|属性的长度Q而实际的长度过?jin),hibernate不会(x)抛异?br />10,如果要生成得sql不是每次都包括所有的列,可配|参数实?
6QsetResultTransformer与addEntity的一个区别是前者支持查L的列Q后者必ȝselect * from users的Ş式或select {a.*},{b.*} from a,b where ....?br />
7,Map vs. Object[]
Since you can also use a transformer that return a Map from alias to
value/entity (e.g. Transformers.ALIAS_TO_MAP), you are no longer
required to mess with index based Object arrays when working with a
result.
List iter = s.createQuery( "select e.student.name as studentName," + " e.course.description as courseDescription" + "from Enrolment as e") .setResultTransformer( Transformers.ALIAS_TO_MAP ) .iterate();
String name = (Map)(iter.next()).get("studentName");
Again, this works equally well for Criteria, HQL and native SQL.
]]>hibernate问题http://www.aygfsteel.com/leekiang/archive/2007/09/28/148919.htmlleekiangleekiangThu, 27 Sep 2007 19:51:00 GMThttp://www.aygfsteel.com/leekiang/archive/2007/09/28/148919.htmlhttp://www.aygfsteel.com/leekiang/comments/148919.htmlhttp://www.aygfsteel.com/leekiang/archive/2007/09/28/148919.html#Feedback0http://www.aygfsteel.com/leekiang/comments/commentRss/148919.htmlhttp://www.aygfsteel.com/leekiang/services/trackbacks/148919.html 2,执行以下语句时hibernate3.0.5?x)报??.2不会(x) sql="select 1+1 from dual"; session.createSQLQuery(sql).uniqueResult() 报错:addScalar() or addEntity() (tng)must be called on a sql query before executing the query.
3,<many-to-one> 的lazy讄为trueӞget子对象不?x)把该父对象抓过?但可以手动写代码抓取父对?br />?Son son =(Son)this.getHibernateTemplate().get(Son.class, id); (tng) (tng) (tng) (tng) (tng) 然后执行 son.getParent().getName(); (tng) (tng) q样不仅仅会(x)抓取到name,其他所有的属性如age,sex{都?x)取刎ͼ即用p.getParent().getAge()辑ֈ?jin)同L(fng)效果Q后台都执行?jin)select * from parent where id=? (tng) (tng) 注意用p.getParent()仅能得到parent的id. (tng) (tng) q时debug查看parent对象的内存快?看到的是一个用cglib实现的代理对? Hibernate通过使用CGLIB,来实现动态构造一个目标对象的代理cd象,q且在代理类对象中包含目标对象的所有属性和Ҏ(gu)Q而且所有属性均被赋gؓ(f)null。通过调试器显C的内存快照Q我们可以看出此时真正的User对象Q是包含在代理对象的CGLIB$CALBACK_0.target属性中Q当调用son.getName()Ҏ(gu)Q这旉过CGLIB赋予的回调机Ӟ实际上调用CGLIB$CALBACK_0.getName()Ҏ(gu)Q当调用该方法时QHibernate?x)首先检查CGLIB$CALBACK_0.target属性是否ؓ(f)nullQ如果不为空Q则调用目标对象的getNameҎ(gu)Q如果ؓ(f)I,则会(x)发v数据库查询,生成cMq样的SQL语句Qselect * from parent where id=??来查询数据,q构造目标对象,q且它赋值到CGLIB$CALBACK_0.target属性中?br />q样Q通过一个中间代理对象,Hibernate实现?jin)实体的延迟加蝲Q只有当用户真正发v获得实体对象属性的动作Ӟ才真正会(x)发v数据库查询操作?br /> (tng) (tng) (tng) <many-to-one> 的lazy讄为false?抓取父对象没有采用代理机制?br /> 4,用session.close()Q执行多ơ查询后报session is closed的错?而hibernateSessionFactory.closeSession()没有q个问题Q这是什么原?