java - Spring jpa - can't use more than one EntityManagerFactory -
i'm working on java spring mvc application uses hibernate , jpa persistence layer. have 2 databases , want map entity tables in database1 , in database2. have defined 2 configuration classes this:
first:
@configuration @enabletransactionmanagement @enablejparepositories(value = "com.mycompany.project.persistence.orm.module.common",entitymanagerfactoryref = "commonentitymanagerfactory",transactionmanagerref = "commontransactionmanager") public class persistenceconfigcommon { public persistenceconfigcommon() { } properties additionalproperties() { return new properties() { { setproperty("hibernate.dialect", "org.hibernate.dialect.mysqldialect"); setproperty("hibernate.hbm2ddl.auto", "update"); setproperty("hibernate.show_sql", "true"); setproperty("hibernate.enable_lazy_load_no_trans", "true"); setproperty("hibernate.connection.charset", "utf8"); setproperty("hibernate.connection.characterencoding", "utf8"); setproperty("hibernate.connection.useunicode", "true"); } }; } @primary @bean(name = "commonentitymanagerfactory") public entitymanagerfactory commonentitymanagerfactory() {entitymanagerfactorybean"); localcontainerentitymanagerfactorybean factory = new localcontainerentitymanagerfactorybean(); factory.setpersistenceunitname("common"); factory.setdatasource(commondatasource()); factory.setpackagestoscan("com.mycompany.project.persistence.orm.module"); hibernatejpavendoradapter vendoradapter = new hibernatejpavendoradapter(); vendoradapter.setgenerateddl(boolean.true); vendoradapter.setshowsql(boolean.true); factory.setjpavendoradapter(vendoradapter); factory.setjpaproperties(additionalproperties()); factory.afterpropertiesset(); return factory.getobject(); } @primary @bean(name = "commondatasource") public datasource commondatasource() { drivermanagerdatasource datasource = new drivermanagerdatasource(); datasource.setdriverclassname("com.mysql.jdbc.driver"); datasource.seturl("jdbc:mysql://10.10.1.11:3306/database1?characterencoding=utf-8"); datasource.setusername("fpuser"); datasource.setpassword("fpdb$123456"); return datasource; } @primary @bean(name = "commontransactionmanager") public platformtransactionmanager commontransactionmanager() { jpatransactionmanager transactionmanager = new jpatransactionmanager(); transactionmanager.setentitymanagerfactory(commonentitymanagerfactory()); return transactionmanager; } @bean public persistenceexceptiontranslationpostprocessor exceptiontranslation() { return new persistenceexceptiontranslationpostprocessor(); } }
second:
@configuration @enabletransactionmanagement @enablejparepositories(value = "com.mycompany.project.persistence.orm.module.profile",entitymanagerfactoryref = "profileentitymanagerfactory",transactionmanagerref = "profiletransactionmanager") public class persistenceconfigprofile { public persistenceconfigprofile() { } properties additionalproperties() { return new properties() { { setproperty("hibernate.dialect", "org.hibernate.dialect.mysqldialect"); setproperty("hibernate.hbm2ddl.auto", "update"); setproperty("hibernate.show_sql", "true"); setproperty("hibernate.enable_lazy_load_no_trans", "true"); setproperty("hibernate.connection.charset", "utf8"); setproperty("hibernate.connection.characterencoding", "utf8"); setproperty("hibernate.connection.useunicode", "true"); } }; } @primary @bean(name = "profileentitymanagerfactory") public entitymanagerfactory profileentitymanagerfactory() {entitymanagerfactorybean"); localcontainerentitymanagerfactorybean factory = new localcontainerentitymanagerfactorybean(); factory.setpersistenceunitname("profile"); factory.setdatasource(profiledatasource()); factory.setpackagestoscan("com.mycompany.project.persistence.orm.module"); hibernatejpavendoradapter vendoradapter = new hibernatejpavendoradapter(); vendoradapter.setgenerateddl(boolean.true); vendoradapter.setshowsql(boolean.true); factory.setjpavendoradapter(vendoradapter); factory.setjpaproperties(additionalproperties()); factory.afterpropertiesset(); return factory.getobject(); } @primary @bean(name = "profiledatasource") public datasource profiledatasource() { drivermanagerdatasource datasource = new drivermanagerdatasource(); datasource.setdriverclassname("com.mysql.jdbc.driver"); datasource.seturl("jdbc:mysql://10.10.1.63:3306/database2?characterencoding=utf-8"); datasource.setusername("fpuser"); datasource.setpassword("fpdb$123456"); return datasource; } @primary @bean(name = "profiletransactionmanager") public platformtransactionmanager commontransactionmanager() { jpatransactionmanager transactionmanager = new jpatransactionmanager(); transactionmanager.setentitymanagerfactory(commonentitymanagerfactory()); return transactionmanager; } @bean public persistenceexceptiontranslationpostprocessor exceptiontranslation() { return new persistenceexceptiontranslationpostprocessor(); } }
note similar in these 2 files, common
has been converted profile
in second file , database name , ip has been changed. in daos have put this:
@persistencecontext(unitname = "common") //or "profile" protected entitymanager entitymanager; @override public entitymanager getentitymanager() { return entitymanager; }
now, when run project, following exception:
org.springframework.beans.factory.nouniquebeandefinitionexception: no qualifying bean of type [javax.persistence.entitymanagerfactory] defined: expected single matching bean found 2: commonentitymanagerfactory,profileentitymanagerfactory
i have bee stuck in exception 2 days. me solve problem. time.
you have not defined "common"
unitname , have 2 @primary
annotations. try configuring: @bean(name = "common")
instead @bean(name = "commonentitymanagerfactory")
Comments
Post a Comment