获取org.hibernate.MappingException:未知实体。使用Spring框架

时间:2017-04-03 19:12:16

标签: java spring hibernate spring-mvc hibernate-mapping

我刚刚开始使用 java spring framework 。我只是尝试用列id和名称填充一个简单的表。但是我得到了:

  

未知实体:org.hibernate.MappingException

我知道它是常见的异常。但我无法解决这个问题。你可以在下面找到实体,dao和hibernate配置。

HibernateConfig.java

 @Getter @Setter 
@Configuration@ConfigurationProperties(prefix = "databaseConfiguration") 
public class HibernateConfig {
    @Value("${driverClass}")
    private String driverClass;

    @Value("${url}")
    private String url;

    @Value("username")
    private String username;

    @Value("password")
    private String password;

    @Value("${hibernateDialect}")
    private String hibernateDialect;

    @Value("${hbm2ddlAuto}")
    private String hbm2ddlAuto;

    private Integer minSize;

    private Integer maxSize;


    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
       return dataSource;
    }

    @Bean
    public Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.hbm2ddl.auto", hbm2ddlAuto);
        properties.put("hibernate.dialect", hibernateDialect);
        properties.put("hibernate.c3p0.min_size", minSize);
        properties.put("hibernate.c3p0.max_size", maxSize);
        return properties;
   }

    @Bean
    public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setHibernateProperties(hibernateProperties());
       return sessionFactory;
}


    @Bean
    public ITestDao testDao() {
        ITestDao testDao = new TestDao();
       return testDao;
    }
} 

所有属性都来自.yml文件。 ITestDao是其中带有抽象add()方法的接口。

实体类

@Getter
@Setter
@Entity
@Table(name = "test")
public class Test {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, unique = true)
    private Long id;

    @Column(name = "dump", nullable = false)
    private String dump;
}

Dao班

@Repository
@Transactional
@Getter
@Setter
public class TestDao implements ITestDao {

    @Autowired
    private LocalSessionFactoryBean sessionFactoryBean;


    public Test add(Test test) {
        try {
                sessionFactoryBean.getObject().getCurrentSession().getTransaction().begin();
            sessionFactoryBean.getObject().getCurrentSession().persist(test);

    } finally {
            sessionFactoryBean.getObject().getCurrentSession().getTransaction().commit();
    }
    return test;
    }

}

服务方法将调用此dao,并在其上方添加@Transactional注释。但是在调用此add() dao方法时,我正在获取未知实体:

  

org.hibernate.MappingException

2 个答案:

答案 0 :(得分:1)

注释下方可能会丢失。

@EntityScan("some.known.persistence")

@EntityScan仅标识特定持久性上下文应使用的类。

答案 1 :(得分:1)

尝试这种方式:

    @Bean
    public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
         sessionFactory.setPackagesToScan(new String[] { "my.package.model" });// You need to provide to adapt : my.package.model
        sessionFactory.setHibernateProperties(hibernateProperties());
       return sessionFactory;
}
祝你好运