1

我收到以下异常

org.hibernate.MappingException:未知实体:com.sample.Student

我在 Stackoverflow 上看到了很多相同问题的答案,但他们都建议使用 javax.persistence 中的 @Entity 注释而不是 hibernate,在我的情况下,我只使用 javax.persistence 中的它,但仍然得到这个异常。

我的 POJO 课

package com.sample;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
public class Student {

    @Id
    int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    String firstName;
    String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

和我的经理班

   package com.sample;

import java.util.Properties;

import org.hibernate.SessionFactory;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;

public class ORMManager {

    /**
     * @param args
     */
    public static void main(String[] args) {
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setUrl("jdbc:oracle:thin:@localhost:1521:XE");
    ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
    ds.setUsername("system");
    ds.setPassword("tiger");

    LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
    lsfb.setDataSource(ds);
    HibernateTemplate template = new HibernateTemplate();
    Properties prop = new Properties();
    prop.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
    prop.put("hibernate.show_sql", "true");
    prop.put("hbm2ddl.auto", "create");

    lsfb.setHibernateProperties(prop);
    try {
        lsfb.afterPropertiesSet();
    } catch (Exception e) {

        e.printStackTrace();
    }
    template.setSessionFactory((SessionFactory)lsfb.getObject());
    template.afterPropertiesSet();

    Student s = new Student();
    s.setFirstName("pallavi");
    s.setLastName("sing");
    template.save(s);
    System.out.println("done");


    }

}

请帮我解决这个问题

4

2 回答 2

2

你试图混合两件事

注释

LocalSessionFactoryBean

使用 LocalSessionFactoryBean 的子类,即

AnnotationSessionFactoryBean

替换这个

LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();

  AnnotationSessionFactoryBean lsfb = new AnnotationSessionFactoryBean();
  Class [] annotatedClasses =  {Student.class};
  lsfb.setAnnotatedClasses(annotatedClasses);

或者

  AnnotationSessionFactoryBean lsfb = new AnnotationSessionFactoryBean();    
  annotationSessionFactoryBean.setPackagesToScan(new String[]{"com.sample"});

看到这个答案

于 2013-09-05T07:06:40.653 回答
1

替换LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();为以下

     AnnotationSessionFactoryBean annotationSessionFactoryBean = new AnnotationSessionFactoryBean();
     annotationSessionFactoryBean.setPackagesToScan(new String[]{"com.sample"});
于 2013-09-05T07:12:28.150 回答