First, my class:
public class Person {
#OneToMany
private List<Address> address;
...
}
My problem is that I want to change from where I am getting the data, first I was getting from database and now I need to fetch from a service, but this probably will break my entity, because I will not have #OneToMany List but a List holding all the address's id.
There is a easier way to change JPA relationship to JAX-RS service? How I will fetch the data now? Maybe a AddressService where I pass all ids and the service return a List, can anyone here help me with a less painful solution?
Related
I'm currently in a situation where I need to create a Repository class which would contain multiple financial statistic queries. The queries are not exactly tied up with one Entity but rather with multiple Entities and will select specific data from the database, based on various some conditions.
Having said that, I'm looking for a way to create a Repository class (i.e. StatisticsRepository) which is not associated with an Entity at all, so I could store the queries there. Simply creating that repository doesn't seem to be working. I'm guessing I probably need to create a service of some kind that loads this repo class? Is this correct, and if so is there an example I'm missing in the Symfony/Doctrine docs?
You can just create a class like StatisticsService/StatisticsFinder (naming convention is for you).
That service should have an entity manager injected, so define it in your config.
Create a query builder inside that service, then simply get and return results.
Let's say I have two users, A and B, with IDs 1 and 2 (respectively). Further, let's assume I have two datasources configured: X and Y.
How could I isolate ALL queries issued by user A to datasource X, and all by B to Y for some given remote method? For example, say that A wants to run 'find' for some model via the API - how could I make sure that the only results A will get are those which are accessible through datasource X?
I'm not sure I entirely understand why you would decide a datasource based on the current user, but in any case, I'm not sure you can do that with LoopBack ... at least, not easily. LoopBack is a model-driven framework - everything derives from the model. As such, all API endpoints go through a model (although you can set up custom routes). And each model is connected to a single datasource.
So, if I hit /api/Widget/13 there is no way to make that findById() call switch between two datasources, it will always hit whatever datasource the model is connected to.
Okay, that all said, the solutions I see are to:
Create a "dispatcher" and have that model do the appropriate thing.
Create a custom remote method on your existing model and do the decision making there and the find, etc on the correct datasource.
In either case, it's not straightforward, and not built-in. FYI, if you need to get the datasource you can access it from the LoopBack application object: MyModel.app.datasources.ds1
I have the entity of some BusinessParticipant, it just simple flat entity. And these Participants may be organized in groups, by user's wish. The group has no data, just id. So creating entity and table seems overkill... I'll wish to have 2 db tables, one for participants and one to link the participant to it's group.
The problem is how should I generate the group id? All the GenerateValue & co. work with #id annotation only.
Are there any way to mark the field that is not PK, to be automatically generated?
I have created a proposal for JPA to support #GeneratedValue on non-id fields.
Please vote here for it to be included in a future release
Depends on your persistence provider. For example, ObjectDB supports #GeneratedValue on normal fields, but this is non-standard functionality. If you're using Hibernate, there's no clean way to do this, see this stackoverflow question: Hibernate JPA Sequence (non-Id)
I have 2 tables in database one of them is employee and the other one is city. I created ado.net entity data model from database. I took a employe and i can access city of these. I am using web service and have a webmethod which return List. I selected employees and called ToList() method. I can not access city name in presentation layer which feeds by web service (asmx not wcf), only access in ID of city. how can i solve this problem.
You can use .Include when retrieving your entity from EF to load navigation properties for your entity.
http://msdn.microsoft.com/en-us/library/bb896272.aspx heres the docs on .Include, if you have EF4.1 or above there is also a lambda version in an extension method which gives you type safety (Reference System.Data.Entity)
Is it possible to access metadata about a POCO entity using EF4.1 that states what the database field names are? Most importantly on foreign key values.
I would like to access this for the purpose of unit testing whilst trying to apply EF4.1 to an existing database schema. I cannot change the existing column names.
Currently the tests create the database using SQL Server Compact 4.0 so I can see the names of the fields, but it would be better if this was automated.
Pseudo code for a test being
Create entity
Save entity
Check that the field name is as expected
That is not unit testing. That is integration testing. Moreover such tests are not too much usefull. Don't let EF create database and instead use copy of the existing database. Simply save entity and load entity in a new context. If data are ok you are happy with it. You can make each test transactional so the transaction is rolled back after each test.
EF 4.1 with code-first approach doesn't offer access to metadata workspace. You can try to convert DbContext back to ObjectContext and try to access MetadataWorkspace:
ObjectContext ctx = ((IObjectContextAdapter) context).ObjectContext;
MetadataWorkspace workspace = ctx.MetadataWorkspace;
Searching anything in MetadataWorkspace is terrible experience. That class is definitely not designed to be used in custom code.