| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1019 人关注过本帖
标题:hibernate问题
只看楼主 加入收藏
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
结帖率:33.33%
收藏
 问题点数:0 回复次数:3 
hibernate问题
public class HelloApp {
public static SessionFactory sessionFactory;
static {
try {
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}

public void getAllPersoninfo(OutputStream out) throws Exception {

Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
String hql = "from Personinfo as c order by c.name asc";
Query q = session.createQuery(hql);
List personinfos = q.list();
for (Iterator it = personinfos.iterator(); it.hasNext();) {
printPersoninfo((PrintStream) out, (Personinfo) it.next());
}
tx.commit();

} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}

public void savePersoninfo(Personinfo personinfo) throws Exception {

Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(personinfo);
tx.commit();

} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {

session.close();
}
}

public void loadAndUpdatePersoninfo(Long personinfo_id, String address)
throws Exception {

Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Personinfo c = (Personinfo) session.load(Personinfo.class,
personinfo_id);
c.setAddress(address);
tx.commit();

} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}

public void deleteAllPersoninfo() throws Exception {

Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.delete("from Personinfo as c");
tx.commit();

} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {

session.close();
}
}

private void printPersoninfo(PrintStream out, Personinfo personinfo)
throws Exception {

out.println("Name: " + personinfo.getName());
out.println("E-Mail: " + personinfo.getEmail());
out.println("电话: " + personinfo.getPhone());
out.println("地址: " + personinfo.getAddress());
out.println("自我介绍: " + personinfo.getDescription());

}

public void test(ServletContext context, OutputStream out) throws Exception {

Personinfo personinfo = new Personinfo();
personinfo.setName("chen");
personinfo.setEmail("chen@126.com");
personinfo.setPhone(111111);
personinfo.setAddress("Beijing");
personinfo.setDescription("I am very honest.");

savePersoninfo(personinfo);

getAllPersoninfo(out);
loadAndUpdatePersoninfo(personinfo.getId(), "Beijing");
getAllPersoninfo(out);

deleteAllPersoninfo();
}

public static void main(String args[]) throws Exception {
HelloApp example = new HelloApp();
example.test(null, System.out);
sessionFactory.close();
}

}
他抛出了以下异常
Exception in thread "main" org.hibernate.MappingException: Unknown entity: java.lang.String
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1302)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:59)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:761)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:739)
at mypack.HelloApp.deleteAllPersoninfo(HelloApp.java:99)
at mypack.HelloApp.test(HelloApp.java:139)
at mypack.HelloApp.main(HelloApp.java:144)
不知道是哪里错了!大大给看一下吧!
搜索更多相关主题的帖子: hibernate public Exception config static 
2007-05-28 10:41
支离破碎
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:737
专家分:0
注 册:2007-1-4
收藏
得分:0 


如果用hiberntea3.0就用下面的方法来删除。
session.delete("from Personinfo as c");

换成session.createQuery("delete from Personinfo")

这样的写法。/


--------------------------------------
hibernate2.0的批量删除好像比较复杂。忘怎么搞的了。。反正是要一个一个删。要拿到每个的实体ID才能删(好像是的)


人生漂泊無依,有如浮萍菱花,隨水飄流,你会在我这里停留吗?
[url=http://51mynet.]http://51mynet.[/url]
2007-05-28 11:19
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
晕!3.0以下的方法用在了3.1上 难怪有错误!

2007-05-28 15:55
黄袖标
Rank: 4
等 级:贵宾
威 望:13
帖 子:676
专家分:0
注 册:2007-3-22
收藏
得分:0 

有些方法真的在3.x的版本中废除了...我证明。


我胡汉三又回来啦!物是人非啊,只有静夜思大大还在。
2007-06-04 18:58
快速回复:hibernate问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014899 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved