Entity, using multiple datasources
anacarda Mar 20, 2008 9:08 PMHi,
I am still newish to using Seam, and I found an issue that I can't seem to fix, so I thought I might as well post a big post, with all the things that I have done, just to see if I have done it correctly.
The basic problem I am having, is that I have two datasources defined within my Project-ds.xml:
<local-tx-datasource> <jndi-name>MySQLDatasource</jndi-name> <connection-url>jdbc:mysql://localhost:3306/mydb</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>myuser</user-name> <password>mypass</password> </local-tx-datasource> <local-tx-datasource> <jndi-name>PgSQLDatasource</jndi-name> <connection-url>jdbc:postgresql://localhost/pgdb</connection-url> <driver-class>org.postgresql.Driver</driver-class> <user-name>pguser</user-name> <password>pgpass</password> </local-tx-datasource>
And I am trying to use the the PostgreSQL datasource within an xhtml file, using the Entity functionality ie:
<rich:dataTable id="settingGroupView" var="settingGroup" value="#{settingGroupAction.resultList}"> <rich:column> <f:facet name="header"><h:outputText styleClass="headerText" value="Input Id"/></f:facet> <h:outputText value="#{settingGroupAction.setting_group_id}"/> </rich:column> <rich:column> <f:facet name="header"><h:outputText styleClass="headerText" value="Setting Group Id"/></f:facet> <h:outputText value="#{settingGroupAction.group_name}"/> </rich:column> </rich:dataTable>
However, the error I am getting shown is:
javax.el.ELException: /page.xhtml @29,103 value="#{settingGroupAction.resultList}": Error reading 'resultList' on type nz.co.company.core.db.SettingGroupAction_$$_javassist_1
And more interestingly, in the stack trace:
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: SELECT command denied to user 'myuser'@'localhost' for table 'setting_group' at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
Im not sure why it is trying to use the MySQL datasource... (setting_group is located within the PostgreSQL datasource).
I am thinking that I have done something wrong, so here is the code snippets from my project:
persistence.xml:
<persistence-unit name="MyDatabase"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/MySQLDatasource</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagerFactories/myData"/> </properties> </persistence-unit> <persistence-unit name="PgDatabase"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/PgSQLDatasource</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagerFactories/pgData"/> </properties> </persistence-unit>
components.xml:
<persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/EntityManagerFactories/myData"/> <persistence:managed-persistence-context name="pgDatabase" auto-create="true" persistence-unit-jndi-name="java:/EntityManagerFactories/pgData" />
SettingGroup.java:
@Entity @Table(name = "setting_group", catalog = "pgdb") public class SettingGroup implements Serializable { @In EntityManager pgDatabase; private Integer setting_group_id; private String group_name; @Id @Column(name = "setting_group_id") public Integer getsetting_group_id() { return setting_group_id; } public void setsetting_group_id(Integer setting_group_id) { this.setting_group_id = setting_group_id; } @Column(name = "group_name") public String getgroup_name() { return group_name; } public void setgroup_name(String group_name) { this.group_name = group_name; } }
SettingGroupAction.java:
@Name("settingGroupAction") public class SettingGroupAction extends EntityQuery { static final long serialVersionUID = 0; private SettingGroup setting_group = new SettingGroup(); @Override public String getEjbql() { return "select setting_group from SettingGroup setting_group"; } @Override public Integer getMaxResults() { return 25; } public SettingGroup getSettingGroup() { return setting_group; } }
Can anyone see what I have done wrong? is it possible to use multiple datasources within the same Seam project?
Also, I found that at least one of the managed-persistence-context names had to be called entityManager
, or else things started to break.
Is this the reason why it is using the MySQL datasource, is because Seam only uses entityManager
?? (even though I have specified the @In pgDatabase within SettingGroup.java?
Thanks