我试图用hibernate xml映射连接到mysql数据库,但我收到此错误

时间:2016-01-22 02:52:16

标签: java mysql xml hibernate

这是我的输出,它显示我无法从org.hibernate.persister.entity.SingleTableEntityPersister获取构造函数。据我所知,它已设法访问登录到数据库。错误在此之后很快就会开始。

            Jan 21, 2016 9:19:44 PM org.hibernate.Version logVersion
            INFO: HHH000412: Hibernate Core {5.0.6.Final}
            Jan 21, 2016 9:19:44 PM org.hibernate.cfg.Environment <clinit>
            INFO: HHH000206: hibernate.properties not found
            Jan 21, 2016 9:19:44 PM org.hibernate.cfg.Environment          buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jan 21, 2016 9:19:45 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Jan 21, 2016 9:19:45 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
Jan 21, 2016 9:19:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Jan 21, 2016 9:19:47 PM `enter code here`org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/rhs]
Jan 21, 2016 9:19:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Jan 21, 2016 9:19:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jan 21, 2016 9:19:47 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Jan 21, 2016 9:19:48 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Exception in thread "main" org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:123)
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:346)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at JavaUtil.main(JavaUtil.java:20)
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:91)
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:116)
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:388)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:509)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:124)
    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.createEntityPersister(PersisterFactoryImpl.java:96)
    ... 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:88)
    ... 15 more
Caused by: org.hibernate.PropertyNotFoundException: Could not locate setter method for property [Profession#SALARY]
    at org.hibernate.internal.util.ReflectHelper.findSetterMethod(ReflectHelper.java:532)
    at org.hibernate.property.access.internal.PropertyAccessBasicImpl.<init>(PropertyAccessBasicImpl.java:44)
    at org.hibernate.property.access.internal.PropertyAccessStrategyBasicImpl.buildPropertyAccess(PropertyAccessStrategyBasicImpl.java:27)
    at org.hibernate.mapping.Property.getGetter(Property.java:299)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:270)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:145)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:63)
    ... 20 more

这是一个简单的POJO类

import javax.persistence.*;

public class Profession {
    private int EDUCATION;
    private int HOURS;
    private String JOB_OUTLOOK;
    private String JOBTITLE;
    private double SALARY;
    private String UPWARD_MOBILITY;

    public Profession() {
    }

    public int getEDUCATION() {
        return EDUCATION;
    }

    public void setEDUCATION(int eDUCATION) {
        EDUCATION = eDUCATION;
    }

    public int getHOURS() {
        return HOURS;
    }

    public void setHOURS(int hOURS) {
        HOURS = hOURS;
    }

    public String getJOB_OUTLOOK() {
        return JOB_OUTLOOK;
    }

    public void setJOB_OUTLOOK(String jOB_OUTLOOK) {
        JOB_OUTLOOK = jOB_OUTLOOK;
    }

    public String getJOBTITLE() {
        return JOBTITLE;
    }

    public void setJOBTITLE(String jOBTITLE) {
        JOBTITLE = jOBTITLE;
    }

    public double getSALARY() {
        return SALARY;
    }

    public void setSALRAY(double sALARY) {
        SALARY = sALARY;
    }

    public String getUPWARD_MOBILITY() {
        return UPWARD_MOBILITY;
    }

    public void setUPWARD_MOBILITY(String uPWARD_MOBILITY) {
        UPWARD_MOBILITY = uPWARD_MOBILITY;
    }
}

这是我的主要课程

import org.hibernate.*;
import org.hibernate.cfg.*;

import java.util.Scanner;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.*;
import javax.swing.*;

public class JavaUtil {
    public JavaUtil() {
    }

    public static void main(String[] args) throws IOException {
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");

        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();
        Profession p = new Profession();


        // closing session
        Transaction tx = session.beginTransaction();
        session.save(p);
        System.out.println("Object saved successfully.....!!");

        tx.commit();
        session.close();
        factory.close();
    }

}

这是我的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>

<!-- Related to the connection START -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/rhs </property>
<property name="connection.user">root</property>
<property name="connection.password">root</property>
<!-- Related to the connection END -->

<!-- Related to hibernate properties START -->
<property name="show_sql">true </property>
<property name="dialet">org.hibernate.dialect.MySqlDialect </property>
<property name="hbm2ddl.auto">update </property>
<!-- Related to hibernate properties END -->

<!-- Related to mapping START -->
<mapping resource="profession.hbm.xml" />
<!-- Related to the mapping END -->

</session-factory>
</hibernate-configuration> 

这是xml映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Profession" table="profession">

<id name="JOBTITLE" column="JOBTITLE"  >
<generator class="assigned" />
</id>

<property name="EDUCATION"/>
<property name="HOURS"/>
<property name="JOB_OUTLOOK"/>
<property name="SALARY"/>
<property name="UPWARD_MOBILITY"/>
</class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:1)

请更改您的媒体资源名称以遵循POJO规则

public class Profession {
   private int education;
   private int hours;
   private String jobOutlook;       
   private String jobTitle;
   private double salary;
   private String upwardMobility;

   .....

}