我正在将工作的hibernate项目转换为Spring hibernate Project。在现有的项目会话中,工厂创建已经通过加载hibernate cfg文件来完成。因为即时春天我通过在java中配置通过Localsessionfactory修改了会话创建。我正在获取一个奇怪的例外如下
ORA-00942: table or view does not exist
但在hiberate cfg查找中同样正常工作。
我正在使用两个transcation manager .Below是我的代码 存储库clas
@Repository
public class TestImpl implements
TestRepository {
@Autowired
@Qualifier("fSessionFactory")
SessionFactory sessionFactory;
@Autowired
@Qualifier("dSessionFactory")
SessionFactory sessionFactory2;
@SuppressWarnings("unchecked")
@Transactional("dTransaction")
public Recordse getOrders(String code, int id ) throws PersistenceException {
Session session = null;
try {
session =sessionFactory.getCurrentSession();
List<Order> orderList = session
.createQuery("FROM Orders WHERE CODE = :code AND NUM_CC = :id " )
.setParameter("code", code)
.setParameter("id", id)
.list();
}
catch (Exception e) {
throw new PersistenceException(e);
}
return empSickTime ;
}
@Transactional("fTransaction")
@Override
public SelectedRecords getRecords(String code, String grpId) throws PersistenceException {
Session session = null;
try {
session = sessionFactory2.getCurrentSession();
List<Records> recList = session
.createQuery("FROM Records WHERE CODE = :code AND GRP_CC = :id " )
.setParameter("code", code)
.setParameter("id", id)
.list();
}
}
catch (HibernateException e) {
throw new PersistenceException(e);
}
return qualifiedFleet;
}
}
PersistencConfig1
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence.properties" })
@ComponentScan({ "com.test.test2.*" })
public class PersistenceConfiguration1 {
@Autowired
private Environment env;
@Bean(name=" ")
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(testDataSource());
sessionFactory.setPackagesToScan(new String[] {
" com.test.test2"});
sessionFactory.setHibernateProperties(hibernateProperties1());
return sessionFactory;
}
@Bean
public DataSource testDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}
@Bean(name="dTransaction")
@Autowired
public HibernateTransactionManager transactionManager(
SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties hibernateProperties1() {
return new Properties() {
{
setProperty("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
setProperty("hibernate.dialect",
env.getProperty("hibernate.dialect"));
setProperty("hibernate.globally_quoted_identifiers", "true");
}
};
}
}
PersistenceConfig2
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence.properties" })
@ComponentScan({ "com.test.test3.*" })
public class PersistenceConfigurationDOTC {
@Autowired
private Environment env;
@Bean(name="dSessionFactory")
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(test1DataSource());
sessionFactory.setPackagesToScan(new String[] {
"com.test.test3.entity"});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource test1DataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc1.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc1.url"));
dataSource.setUsername(env.getProperty("jdbc1.user"));
dataSource.setPassword(env.getProperty("jdbc1.pass"));
return dataSource;
}
@Bean(name = "fTransaction")
@Autowired
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactoryFCDS().getObject());
return txManager;
}
Properties hibernateProperties() {
return new Properties() {
{
setProperty("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
setProperty("hibernate.dialect",
env.getProperty("hibernate.dialect"));
setProperty("hibernate.globally_quoted_identifiers", "true");
}
};
}
}
我错过了什么吗?
答案 0 :(得分:0)
@Bean(name=" ")
public LocalSessionFactoryBean sessionFactory() {
看起来您的bean名称不正确?
另一件事是fTransaction
您在宣布dSessionFactory
sessionFactory2
中的会话工厂
@Autowired
@Qualifier("dSessionFactory")
SessionFactory sessionFactory2;
然后在fTransaction
@Transactional("fTransaction")
@Override
public SelectedRecords getRecords(String code, String grpId) throws PersistenceException {
Session session = null;
try {
session = sessionFactory2.getCurrentSession();
我猜你指的是错误的数据库连接。
下次我建议为您的属性提供更有意义的名称,例如paymentSessionFactory
,geoSessionFactory
&amp;等等,这样你就不会混淆var1
vs session2