public class HibernateUtil {
private static SessionFactory sessionFactory;
private static Session session;
static {
//创建configuration对象,读取hibernate.cfg.xml文件,完成初始化
Configuration config = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry); //16
}
//获取sessionFactory
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
//获取session对象
public static Session getSession() {
session = sessionFactory.openSession();
return session;
}
//关闭会话
public static void closeSession(Session session) {
if(session!=null) {
session.close();
}
}
}
public class Test {
public static void main(String[] args) {
add(); //14
}
//将学生添加到班级
public static void add()
{
Grade g = new Grade("java一班", "java软件开发一班");
Student s1 = new Student("张三", "男");
Student s2 = new Student("慕女神","女");
g.getStudents().add(s1);
g.getStudents().add(s2);
//保存班级、学生信息
Session session = HibernateUtil.getSession(); //29
Transaction transaction = session.beginTransaction();
session.save(g);
session.save(s1);
session.save(s2);
transaction.commit();
HibernateUtil.closeSession(session);
}
}
错误信息:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.imooc.entity.Test.add(Test.java:29)
at com.imooc.entity.Test.main(Test.java:14)
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:185)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:385)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1790)
at com.imooc.util.HibernateUtil.<clinit>(HibernateUtil.java:16)
... 2 more
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:138)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:188)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:341)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:507)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:146)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163)
... 6 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:135)
... 15 more
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for student in class com.imooc.entity.Grade
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:316)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:310)
at org.hibernate.mapping.Property.getGetter(Property.java:321)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:444)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:200)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:82)
... 20 more看不出哪出问题了,求解啊!!!
添加回答
举报
0/150
提交
取消