16

I have 2 databases that I need to connect to. I can easily connect to them in the application.conf file like so:

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost/db1"
db.default.user=postgres
db.default.password="password"

db.secondary.driver=org.postgresql.Driver
db.secondary.url="jdbc:postgresql://localhost/db2"
db.secondary.user=postgres
db.secondary.password="password"

ebean.default="models.db1.*"
ebean.secondary="models.db2.*"

I have my model classes in those packages, and it DDL generates the tables properly.

The problem lies in actually working with these entities. Anything not in the "default" package throws this error (using the Users table in the secondary database as an example)

If I try to query all the rows of the table:

List<Users> users = Users.find.all();

It throws this error:

[PersistenceException: models.db2.Users is NOT an Entity Bean registered with this server?]

Even though I am 100% sure that the Users table is there in the backend, it is a registered table the DDL works and makes this table properly, and I am importing the proper classes.

Is there a certain way I need to query model classes that aren't in the default package?

EDIT: I realize that the stack trace shows that it's trying to use the DefaultServer. How can I make it use the secondary server?

    at com.avaje.ebeaninternal.server.core.DefaultServer.createQuery(DefaultServer.java:989) ~[avaje-ebeanorm-server.jar:na]
    at com.avaje.ebeaninternal.server.core.DefaultServer.createQuery(DefaultServer.java:946) ~[avaje-ebeanorm-server.jar:na]
    at com.avaje.ebeaninternal.server.core.DefaultServer.find(DefaultServer.java:982) ~[avaje-ebeanorm-server.jar:na]
    at play.db.ebean.Model$Finder.all(Model.java:254) ~[play-java-ebean_2.10.jar:2.1.3]
Bert B.
  • 579
  • 2
  • 11
  • 26
  • No one's answered your question yet, so I think rather than append your 'EDIT:' to your question, you should just really edit it to ask what you really need to. – Greg Kopff Aug 19 '13 at 04:30
  • Instead of using models.db2.* in applications.conf, try to put models.db2.Users. – Franz Aug 19 '13 at 05:25
  • @GregKopff, I only said EDIT because I wasn't sure if someone was answering it while I edited the questions. – Bert B. Aug 19 '13 at 15:09
  • @Franz, I've tried that, but I get a `Error with [models.db2.Users] It has not been enhanced but it's superClass [class play.db.ebean.Model] is? (You are not allowed to mix enhancement in a single inheritance hierarchy) marker[play.db.ebean.Model]` error. – Bert B. Aug 19 '13 at 15:10

3 Answers3

7

Ok, your application.conf seems to be correct.

You may use the secondary server like this:

EbeanServer secondary = Ebean.getServer("secondary");
secondary.find(User.class).findList();

Once you've got your secondary server, you may treat it just as you treat the Ebean singleton.

serejja
  • 19,968
  • 6
  • 54
  • 69
  • Yeah I've tried this approach, but then the problem comes in doing anything besides actually just SELECTing for data. For example, with this method, I can get all the Users, but the Users have a OneToMany relationship with another class, "Groups." Groups is also in the secondary database. In the view, I want to map over all of the groups for each of the users, so I have `@user.groups.map { group => group.name }`, yet that throws this error: `[PersistenceException: Query threw SQLException:ERROR: column t1.users_id does not exist ...` even though that column DOES exist. – Bert B. Aug 19 '13 at 15:08
7

I had the same problem and I fixed it by specifying the server name at the Model.Finder level.

So in your case, in your User class, you should have something like :

public static Model.Finder<Long, User> find = new Finder<Long, User>("secondary", Long.class, User.class);
DrMabulle
  • 91
  • 4
  • Wow, this is exactly what I'm looking for, unfortunately this only works for anything involving querying this model, but any time I use a save command for this approach, I get the error `The type ____ is not a registered entity? If you don't explicitly list the entity classes to use Ebean will search for them in the classpath.` Do you have any ideas on how to fix that? (Yes, I know my models are created properly already). – Bert B. Sep 09 '13 at 01:10
  • Sorry, I don't get it. How do you try to save your objects ? I do `User user = User.find.byId(aId); user.update(aId); user.delete();` It works fine, usualy... – DrMabulle Oct 30 '13 at 23:23
  • 1
    Be aware that you also need to tell the Ebean save method which server! `public void save(String server)`. Don't use the default method `save()`. – myborobudur Feb 05 '14 at 16:28
  • Bert B. - Either u missing model package specification in config or @Entity in Model class – Rajat Nov 18 '19 at 13:00
4

You can use both:

public static Model.Finder<Long, User> find = new Finder<Long, User>("secondary", Long.class, User.class);

and

Ebean.getServer("secondary");

But you have to use the save method with the server declaration on your entity

public void save(String server)

Without save() ebean uses the default server, even on your user-entity!

myborobudur
  • 3,965
  • 6
  • 36
  • 57