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)
不知道是哪里错了!大大给看一下吧!