标题:hibernate问题
取消只看楼主
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
结帖率:33.33%
 问题点数:0 回复次数:1 
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
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
得分:0 
晕!3.0以下的方法用在了3.1上 难怪有错误!

2007-05-28 15:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-142915-1-1.html




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

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