Java 使用spring在一个应用程序中维护两个数据库
我有一个用例,我必须在远程数据库中保留一些数据,但是我会在本地Java 使用spring在一个应用程序中维护两个数据库,java,spring,hibernate,spring-mvc,spring-data,Java,Spring,Hibernate,Spring Mvc,Spring Data,我有一个用例,我必须在远程数据库中保留一些数据,但是我会在本地entercode heredb中引用数据,插入在远程数据库中正确发生,但是当我试图从远程数据库访问数据时,我无法获取数据,在这里我指定了我的几个代码片段 persistence.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="ht
entercode here
db中引用数据,插入在远程数据库中正确发生,但是当我试图从远程数据库访问数据时,我无法获取数据,在这里我指定了我的几个代码片段
persistence.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<!-- Added this additional line to share the connection Pool -->
<!-- <property name="initialSize" value="1000" /> <property name="maxActive"
value="1000" /> <property name="maxIdle" value="10" /> -->
</bean>
<!-- This is the new Properties for the new DB of aadhaar vault -->
<bean id="myDataSource1"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${db1.url}" />
<property name="username" value="${db1.username}" />
<property name="password" value="${db1.password}" />
<!-- Added this additional line to share the connection Pool -->
<!-- <property name="initialSize" value="1000" /> <property name="maxActive"
value="1000" /> <property name="maxIdle" value="10" /> -->
</bean>
<!-- This is the new DB table, which is being initialized for the aadhaar
vault -->
<bean id="mySessionFactory1"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource1" />
<property name="configLocation" value="classpath:hibernateNew.cfg.xml" />
<property name="namingStrategy" ref="namingStrategy" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- Specify session context -->
<!-- <prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</prop> -->
</props>
</property>
</bean>
<!-- This is the new DB table, which is being initialized for the
remote , ends here -->
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="namingStrategy" ref="namingStrategy" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- Specify session context -->
<!-- <prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</prop> -->
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<bean id="transactionManagerNew"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory1" />
</bean>
<bean id="namingStrategy" class="org.hibernate.cfg.ImprovedNamingStrategy" />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="com.abc.def.model.User"></mapping>
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="com.abc.def.model.User1"></mapping>
</session-factory>
</hibernate-configuration>
ABCService.Java
@Service
public class ABCServiceImpl implements ABCService {
@Autowired
ABCDao abcDao;
@Override
@Transactional("transactionManagerNew")
public ABC getByForeignKey(Long refid,Long customerEntityId) {
return abcDao.getByForeignKey(refid,customerEntityId);
}
@Repository
public class ABCDaoImpl implements ABCDao {
@Autowired
@Qualifier("mySessionFactory1")
protected SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
@Override
public ABC getByForeignKey(Long refId,Long customerEntityId) {
Query query = sessionFactory.getCurrentSession().createQuery(" from ABC where RefId = " + refId + " and customerEntityId = "+customerEntityId);
List<ABC> abcs = query.list();
if(abcs.size()>0) {
return abcs.get(0);
}
return null;
}
}
ABCDaoImpl.java
@Service
public class ABCServiceImpl implements ABCService {
@Autowired
ABCDao abcDao;
@Override
@Transactional("transactionManagerNew")
public ABC getByForeignKey(Long refid,Long customerEntityId) {
return abcDao.getByForeignKey(refid,customerEntityId);
}
@Repository
public class ABCDaoImpl implements ABCDao {
@Autowired
@Qualifier("mySessionFactory1")
protected SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
@Override
public ABC getByForeignKey(Long refId,Long customerEntityId) {
Query query = sessionFactory.getCurrentSession().createQuery(" from ABC where RefId = " + refId + " and customerEntityId = "+customerEntityId);
List<ABC> abcs = query.list();
if(abcs.size()>0) {
return abcs.get(0);
}
return null;
}
}
@存储库
公共类ABCDaoImpl实现ABCDao{
@自动连线
@限定词(“mySessionFactory1”)
受保护的SessionFactory SessionFactory;
@抑制警告(“未选中”)
@凌驾
公共ABC getByForeignKey(长refId、长customerEntityId){
Query Query=sessionFactory.getCurrentSession().createQuery(“来自ABC,其中RefId=“+RefId+”和customerEntityId=“+customerEntityId”);
List abcs=query.List();
如果(abcs.size()>0){
返回abcs.get(0);
}
返回null;
}
}
这里我尝试从新的_db访问值,它总是返回空值,
任何帮助都将不胜感激。我正在处理这从过去2天还没有找到任何解决方案,请让我知道,如果一些更多的片段是必要的 select在哪里?@YCF\L我正在使用hibernate,所以它本身管理您正在使用的rdbms?您是否在rdbms中测试了查询?@YCF_L我正在使用MySql,但是我的查询在我的db环境中正常运行。您确定
refId
和customerEntityId
不为空吗?