我剛剛開始在java spring framework中工作。我只是試圖用列ID和名稱填充一個簡單的表。但我得到:獲取org.hibernate.MappingException:未知實體。使用Spring框架
未知實體:org.hibernate.MappingException
我得到的,它是經常遇到的異常。但我無法解決這個問題。你可以找到下面使用的實體,dao和hibernate配置。
HibernateConfig.java
@Getter @Setter
@[email protected](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
後準確完整的堆棧跟蹤。 –
看到這個答案:http://stackoverflow.com/a/23215605/3959856 –