1、用MyEclipse通過連接到數據庫,可以生成Hibernate需要的POJO和對應的映射文件。但是我生成的hbm.xml有問題,會報錯"could not load an entity"。后來找到了元兇
<class name="hbm.pojo.Misuser" table="misuser" schema="informix" catalog="zzymis">
把catalog="zzymis"去掉就OK了。
2、Hibrenate保存數據失敗
如果忘記提交事務會導致數據保存或者更新失敗,正確代碼如下:
HibernateSessionFactory. getSession().beginTransaction();
new hbm.pojo.StudySubjectDAO().merge(subject);
HibernateSessionFactory. getSession().flush();
HibernateSessionFactory. getSession().getTransaction().commit();
3、Hibernate一對多的配置
最好使用雙向關聯(假設Main->Detail)
1)Main.java中加入:
private Set< Detail > detail=new HashSet();
然后加入get()和set()方法;
2)Detail.java中加入:
private Main main;
然后加入get()和set()方法;另外別忘了重寫equals()方法

public boolean equals(Object object)
{

if (object == null || !this.getClass().equals(object.getClass()))
{
return false;
}
Detail other = (Detail)object;
if(other.id==null||this.id==null)
return false;
if (this.id != other.id && (this.id == null || !this.id.equals(other.id)))
return false;
return true;
}
3)Main.hbm.xml中加入:
<set name="detail" inverse="true" cascade="all">
<key column="mainId"></key>
<one-to-many class=" Detail"/>
</set>
4)Detail.hbm.xml中加入:
<many-to-one name=" main " class=" Main" column=" mainId">
</many-to-one>
同時把mainId對應的<property>……</property>刪掉
5)操作:
雙向設置:(調用setMain()setDetail())
4、EJB中的getSingleResult()方法
查找返回一個結果,是唯一的一個結果,當getSingleResult()個方法被調用時執行查詢。如果沒有結果返回,這個方法將會拋出javax.persistence.EntityNotFoundException運行異常.如果找到多于一個結果, javax.persistence.NonUniqueResultException異常將會在運行期間拋出.因為這兩個異常都是RuntimeException,例子中的代碼不需要完整的try/catch塊。
5、Hibernate3的解決中文查詢問題
如果直接把查詢的參數放到sql語句中是查不出來的,比如:
Query query=em.createQuery("select u from User u where u.name like '%"+myName+"%'");
可以使用占位符或者設置參數的方法來查詢,例如:
1)Query query=em.createQuery("select u from User u where u.name like ? ");
query.setString(0,"%"+myName+"%");
2)Query query=em.createQuery("select u from User u where u.name like :name");
query.setString("name","%"+myName+"%");
6、Like的其他用法(正則)
like '_heryl' :搜索以字母 heryl 結尾的所有六個字母的名稱(如 Cheryl、Sheryl)。
like '[CK]ars[eo]n' :搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。
like '[M-Z]inger' :搜索以字符串 inger 結尾、以從M到Z的任何單個字母開頭的所有名稱如 。
like 'M[^c]%' :搜索以字母 M 開頭,并且第二個字母不是 c 的所有名稱(如 MacFeather)。
7、一對多(one-to-many)刪除不掉
比如Main-to-Detail是one-to-many關系,在新增的時候可以通過persistMain()同時把多個Detail插入數據庫,但是如果想刪除某個Main中的某幾個Detail時,好像通過mergeMain()是無法做到的,通過mergeMain()可以更新Main的屬性,但是沒辦法刪除相應的Detail,至少我是沒辦法做到。這時,我一半都是寫個方法單獨來刪除Detail,例如deleteDetailById()來一個個刪除。
8、查詢返回多個實體Bean
1)查詢(省略后的代碼)
@PersistenceContext(unitName="crm")
private EntityManager em;
public List getUserinfoTrace(){
return em.createQuery("select new List(u,t) from Userinfo u,Trace t where u.id=t.id").getResultList();
}
2)讀取(省略后的代碼)
List<List> result=(List<List>)remote.getUserinfoTrace();
for(List obj:result){
Userinfo userinfo=(Userinfo)result.get(0);
Trace trace=(Trace)result.get(1);
……
}
9、設置ID自增
在用netbean生成的實體Bean后,需要手工加上自增注釋(@GeneratedValue(strategy = GenerationType.AUTO))
例如:
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
10、failed to lazily initialize a collection of role
因為延遲加載導致的,但是我在EntityBean中使用fetch=FetchType.EAGER和FetchType.LAZY都無效,我的EntityBean是@OneToMany,最后只有在SessionBean的讀取EntityBean的方法中加入:
if(main.getDetails()!=null)
main.getDetails().size();
return main;
這樣總算解決問題。
posted on 2007-06-01 18:35
破繭而出 閱讀(1115)
評論(0) 編輯 收藏 所屬分類:
Hibernate_EJB