我的applicationContext.xml中有两个实体管理器,它们对应两个不同的数据库.我可以使用entityManager1轻松查询
database1,但是当我尝试使用entityManager2访问database2时,我没有得到任何结果.我正在使用
Spring Hibernate JPA.
这是我的ApplicationContext.xml
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
destroy-method="close">
value="jdbc:db2://HOST_NAME:PORT_NO/DB_NAME:INFORMIXSERVER=SERVER_NAME;DELIMIDENT=y;" />
destroy-method="close">
value="jdbc:db2://HOST_NAME:PORT_NO/DB_NAME2:INFORMIXSERVER=SERVER_NAME;DELIMIDENT=y;" />
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
lazy-init="false">
characterEncoding
UTF-8
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
lazy-init="false">
characterEncoding
UTF-8
scope="prototype">
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
classpath*:META-INF/persistence.xml
classpath*:META-INF/persistence2.xml
这是我的服务层代码,可以与enityManager1一起使用:
@Transactional
public class StatesDAO implements IStatesDAO {
private EntityManager em;
@PersistenceContext(unitName = "PU1")
public void setEntityManager(EntityManager em) {
this.em = em;
}
private EntityManager getEntityManager() {
return em;
}
@SuppressWarnings("unchecked")
public List findAll() {
logger.info("finding all States instances");
try {
final String queryString = "select model from States model";
Query query = getEntityManager().createQuery(queryString);
return query.getResultList();
} catch (RuntimeException re) {
throw re;
}
}
}
我的两个persitence.xml文件如下所示:
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
org.hibernate.ejb.HibernatePersistence
com.jpa.entity.States
和
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
org.hibernate.ejb.HibernatePersistence
com.jpa.other.entity.States
如果我更改了我的服务层(如下所示),我没有得到任何结果.基本上列表的大小为零:
@Transactional
public class StatesDAO implements IStatesDAO {
private EntityManager em;
@PersistenceContext(unitName = "PU2")
public void setEntityManager(EntityManager em) {
this.em = em;
}
private EntityManager getEntityManager() {
return em;
}
@SuppressWarnings("unchecked")
public List findAll() {
logger.info("finding all States instances");
try {
final String queryString = "select model from States model";
Query query = getEntityManager().createQuery(queryString);
return query.getResultList();
} catch (RuntimeException re) {
throw re;
}
}
}
所以基本上你可以看到我有两个具有完全相同结构的实体(状态),为了区分彼此,我将它们放入单独的包中
根据我的知识,我在这里没有做任何疯狂的事情,但它似乎仍然无法正常工作.这个问题是怎么造成的,怎么解决这个问题?
后续行动:我忘记提到的一件事是即使有两个不同的数据库,但数据库服务器名称相同.我不知道这是否是一个有用的信息.所以想到分享它.
这是我现在得到的例外:
16:24:44,732 INFO [STDOUT] Hibernate: select state0_.state as col_0_0_ from states state0_
16:24:44,753 WARN [JDBCExceptionReporter] SQL Warning: 36106, SQLState: 01I01
16:24:44,753 WARN [JDBCExceptionReporter] IDS SQL Warning: SQLCODE=36106, SQLSTATE=01I01, SQLERRMC=0;819;informix;;IDS/NT32;1;1;0;819;0;, DRIVER=4.7.85