I just started using OrmLite so I am playing around with it. I have trouble understanding a few concepts, esp. dealing with many to many relationships.
These are my main tables:
Pc
Name
Domain
Ethernet
IP
Gateway
Subnet
MAC
And here the ones ensuring many-to-many-relationship just as in the online-example:
PcEth
I populate the Database via:
PC somePc = new PC("randomPc", "someDomain");
pcDao.create(somePc);
Ethernet eth1 = new Ethernet("127.255.0.1", "255.0.0.0", "192.168.1.1", "macadress");
Ethernet eth2 = new Ethernet("192.168.1.1", "255.0.0.0", "192.168.1.1", "macadress");
ethernetDao.create(eth1);
ethernetDao.create(eth2);
pcEthernetDao.create(new PcEthernet(somePc, eth1));
But I am quite unsure how I so I get all the data that belongs to a Pc back. I was kind of expecting the
PC pc = pcDao.queryForId(1);
to automagically retrieve the data of the other tables. Isn’t that what an ORM is supposed to do? Having an object so I do not need to care about the underlying database? Yet all I got is indeed just the Pc.object which only has the PC-defined attributes. (On the other hand, it is not that surprising, as I am only dealing on the pcDao.)
Yet how am I supposed to build a query so that I get a “meta-object” that contains all the data belonging to a PC? An object that includes the data of the PC, and a list the ethernetDevices belonging to the Pcs (that include their own list of Dns-adresses as well), as well as a list of Software and Operating systems.
Am I now supposed to manually solve the dependencies? Query a Pc, ask the PcEthernet-table to get matching Ids, retrieve the EthernetDevice, and so on?
Or is there a trick I am not quite grasping yet?
@Emmanuel’s answer is good but I thought I’d add some context. He is correct that you will need to manage at least some of this relationship yourself.
Yes, although ORMLite is constantly fighting the “size versus feature-set” war. It was designed and built to be a “Lite” ORM and to this end it will never support a full complement of ORM features like Hibernate or iBatis does.
What you can do with ORMLite and your dataset is to have, as @Emmanuel mentioned, a
@ForeignCollectionFieldin both yourPcandEthernetobjects.When you retrieve a
Pc, a separate query will fill in thepcEthernets. But you will then need to do the queries to get the associatedEthernetobjects yourself.There have been requests before to automatically generate the join tables and automatically do the IN query. If you want to outline how it would work or assist in the development, be sure to join the ORMLite developers mailing list.
Lastly, in this case, do you really need a many-to-many relationship? Doesn’t the
Ethernetobject only have onePcobject?