注册 登录
编程论坛 J2EE论坛

hibernate问题

lgdcky 发布于 2007-05-28 10:41, 1036 次点击
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)
不知道是哪里错了!大大给看一下吧!
3 回复
#2
支离破碎2007-05-28 11:19


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

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

这样的写法。/


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

#3
lgdcky2007-05-28 15:55
晕!3.0以下的方法用在了3.1上 难怪有错误!
#4
黄袖标2007-06-04 18:58

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

1