使用hibernate4+oracle11g学习hibernate一对多关联关系,程序运行出现异常。不知道怎么解决,请大家帮忙?
下面是hibernate配置文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">admin</property>
<property name="connection.password">admin</property>
<property name="show_sql">true</property>
<mapping resource="com/pb/hibernate/po/Grade.hbm.xml" />
<mapping resource="com/pb/hibernate/po/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
下面是hibernate工具类:
package com.pb.hibernate.util;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static SessionFactory congigureSessionFactory() throws HibernateException {
Configuration configuration = new Configuration().configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
}
下面是Grade持久化类:
package com.pb.hibernate.po;
import java.util.HashSet;
import java.util.Set;
public class Grade {
private int gid;
private String gname;
private String gdesc;
private Set students = new HashSet();
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public String getGdesc() {
return gdesc;
}
public void setGdesc(String gdesc) {
this.gdesc = gdesc;
}
public Set getStudents() {
return students;
}
public void setStudents(Set students) {
this.students = students;
}
}
下面是Grade映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.pb.hibernate.po.Grade" table="GRADE" lazy="false">
<id name="gid" type="java.lang.Integer">
<column name="GID" />
<generator class="assigned" />
</id>
<property name="gname" type="java.lang.String">
<column name="GNAME" length="10" not-null="true" />
</property>
<property name="gdesc" type="java.lang.String">
<column name="GDESC" length="50" />
</property>
<set name="students">
<key column="GID" />
<one-to-many class="com.pb.hibernate.po.Student" />
</set>
</class>
</hibernate-mapping>
下面是Student持久化类:
package com.pb.hibernate.po;
public class Student {
private int sid;
private String sanme;
private String sex;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSanme() {
return sanme;
}
public void setSanme(String sanme) {
this.sanme = sanme;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
下面是Student映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.pb.hibernate.po.Student" table="STUDENT">
<id name="sid" type="java.lang.Integer">
<column name="SID" />
<generator class="assigned" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" length="10" not-null="true" />
</property>
<property name="sex" type="java.lang.String">
<column name="SEX" length="20" />
</property>
</class>
</hibernate-mapping>
测试类:
package com.pb.hibernate.po;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.pb.hibernate.util.HibernateUtil;
public class Test {
public static void main(String[] args) {
save();
}
public static void save() {
Grade g = new Grade();
g.setGid(110701);
g.setGname("计应一班");
g.setGdesc("计算机专业应用一班");
Student stu1 = new Student();
stu1.setSid(11070101);
stu1.setSanme("张华");
stu1.setSex("男");
Student stu2 = new Student();
stu2.setSid(11070125);
stu2.setSanme("刘萍");
stu2.setSex("女");
g.getStudents().add(stu1);
g.getStudents().add(stu1);
Session session = HibernateUtil.congigureSessionFactory().openSession();
Transaction tr = session.beginTransaction();
session.save(g);
session.save(stu1);
session.save(stu2);
tr.commit();
session.close();
}
}
运行异常情况:
2014-12-7 12:46:48 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
2014-12-7 12:46:48 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
2014-12-7 12:46:48 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" 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:1769)
at com.pb.hibernate.util.HibernateUtil.congigureSessionFactory(HibernateUtil.java:15)
at com.pb.hibernate.po.Test.save(Test.java:33)
at com.pb.hibernate.po.Test.main(Test.java:11)
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(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
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(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:135)
... 15 more
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for sname in class com.pb.hibernate.po.Student
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:436)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:200)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:82)
... 20 more
大家帮忙看看是哪出错了?
------解决思路----------------------
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for sname in class com.pb.hibernate.po.Student
Student类中找不到sname的get方法。
配置文件中配置的是sname,但是Student类中定义的属性是sanme