跳至主要内容

博文

目前显示的是标签为“Hibernate”的博文

Dev's Ranting - Hibernate batch processing

Recently I have been working on a performance issue of a small application. It utilizes Hibernate to delete a lot of entities from database. Unfortunately the application died throwing a `OutOfMemoryError`. After discussing with my colleagues and reading some posts about how Hibernate works in batch mode, it appears to me that Hibernate's batch mode (`order_inserts` and/or `order_updates`) may played some role in this issue. Ref: * [Why is Hibernate batching / order_inserts / order_updates disabled by default](https://stackoverflow.com/questions/27755461/why-is-hibernate-batching-order-inserts-order-updates-disabled-by-default) * [Hibernate Batch Processing - Why you may not be using it. (Even if you think you are](https://abramsm.wordpress.com/2008/04/23/hibernate-batch-processing-why-you-may-not-be-using-it-even-if-you-think-you-are/)

Dev's Ranting - Hibernate Mapping Collections of Subtypes

Hibernate 作为ORM框架的中流砥柱被广泛地应用于企业应用程序开发中。从早期的XML配置到现在的Java Configuration,开发者早已习惯了使用各种 annotation 来配置 entity mapping。不过基础概念都是一样的。 为了展示用于一对多关系的多表映射,DZone上有一个[挺简单的例子](https://dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-one-to-many-using-annotations-1.html)。 @Entity @Table(name = "STUDENT") public class Student { private long studentId; private String studentName; private Set studentPhoneNumbers = new HashSet (0); @OneToMany(cascade = CascadeType.ALL) @JoinTable(name = "STUDENT_PHONE", joinColumns = { @JoinColumn(name = "STUDENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "PHONE_ID") }) public Set getStudentPhoneNumbers() { return this.studentPhoneNumbers; } } 这两天遇到了一个定义映射关系的问题,与上面这个映射的区别在于,除了 Entity A 和 B 之间有 join table之外,Entity B 实际上是共享一个table的sub type,使用了 `@DiscriminatorColumn`...

Hibernate Escape SQL Keywords

最近在使用Hibernate的时候遇到了一个诡异的bug,花了将近一个小时的debug时间,终于发现根源是table的column name刚好是SQL keyword (例如id, mode之类的名字)。Hibernate在自动生成的SQL语句中是不会自动加上标准的单引号(`)的,这导致我的数据库MySQL引擎在解释SQL语句的时候抛出了异常…… 经过一番google,最终采用了一个解决方案——在hbm.xml映射文件中为该字段指定使用quote,例如:      <property name="mode" column="`mod`"> 这样就等于强制使用了单引号的语法。 有意思的是,发现NHibernate的bug list中就有 关于是否要自动使用quote的相当激烈的讨论 。个人认为如果在hibernate configuration中可以通过某个property指定所有的SQL语句都要使用quote就很方便了。