How do I create a read-only entity? - jpa

I have a web application using J2EE + Spring and a MySQL database. I need one entity which will be read-only. I have one main table with products, and they are only to read. There should be no insertion of new records and no updates currently.
The entity class should only read data and pass the entities forward (other entities are tables like order, shipments etc.).
Is there any solution for this? Does anyone have the same issue? Thanks for the help.

If you don't change an object, it will never be updated.
If you are using EclipseLink you can use the #ReadOnly annotation to mark something as read-only.

Related

How do I tell EF to ignore columns from my database?

I'm building my EF (v4.0.30319) data model from my SQL Server database. Each table has Created and Updated fields populated via database triggers.
This database is the backend of a ASP.NET Web API application and I recently discovered a problem. The problem is, since the Created and Updated fields are not populated in the model passed into the api endpoint, they are written to the database as NULL. This overwrites any values already in the database for those properties.
I discovered I can edit the EF data model and just delete those two columns from the entity. It works and the datetimes are not overwritten with NULL. But this leads to another, less serious but more annoying, problem... my data model has a bunch of tables that contain these properties and all the tables need to be updated by removing these two columns.
Is there a way to tell EF to ignore certain columns in entities across the entire data model without manually deleting them?
As far as I know, no. Generating the model from the database is going to always create all of the fields from the database table. Well, as long as it has a primary key it will.
It is possible to only update the fields that you want i.e. don't include the "Created" and "Updated" fields in your create and update methods. I'd have to say though, that I'd think it'd be better if those fields didn't even exist on the model at that point. You may at one point see those fields on the model and not remember that they won't get persisted to the DB.
You may want to look into just inserting the datetimes into those fields when you call your create() and update() methods. Then you could just ditch the triggers. You'd obviously want to use a class library for all of your database operations so this functionality would be in one place. To keep it nice and neat, you know?

add changes via EF to DB possible?

i've got a question about the tables and stored procedures which are created by aspnet_regsql.exe.
scenario:
new empty database: foodb
execute the script aspnet_regsql.exe for this Database.
now:
can i add a entity model framework for this Database, and add aditional Entities and associations to the existing Entities (e.g. ASPNET_Users) ?
best regards
So, yes you can bring in any database objects to an entity framework model. I'm not sue why you would want to, because these things aren't really made to be directly accessed (there are API's to get to this data).
To your second question of adding new associations, that part I don't think you can do. As far as memory serves, the EF designer does not allow you to just wire up non-existant association. So you would need to actually make the associations within SQL server, and then import them into the model.
I wouldn't recommend doing this if you are still using any of the API's as it might have unintended consequences.

Datanucleus JPA Update & Delete operation

I am using Datanucleus as the JPA engine to perform CRUD on an entity in Force.com DB. Insert and Select are working fine, but while updating a new row is getting created and delete does not remove the record at all. I am using following for transaction enforcement
Is there kind of an issue with the proxy object to actual object synchronization after the object has been fetched, modified and then subject to updating.
It seems that as the ORM layer (datanucleus+force sdk) is unable to match between the altered object and the original one, it is landing up creating new row.
Any help is highly appreciated.
Thanks
It would help if you can post your code. But I am guessing you might be hitting a known difference in behavior between DataNucleus and other ORMs like Hibernate.
Are you doing something like this?
MyEntity ent = new MyEntity();
ent.setId(idFromWebRequest);
ent.setXXX(valueFromWebRequest);
ent = entityManager.merge(ent);
(where the instantiation and setters might be carried out by a data binding mechanism such as Spring MVC). If you do it like this, it will not work with DataNucleus but it will work with Hibernate. For DataNucleus you must instead do:
MyEntity ent = entityManager.find(MyEntity.class, idFromWebRequest);
ent.setXXX(valueFromWebRequest);
ent = entityManager.merge(ent);
I would prefer it worked like Hibernate, but the DataNucleus team believes this is the correct behavior. Maybe they can chime in. I believe it's a matter of when you consider an entity a new entity vs. a detached entity. If your entity instance is detached, then calling merge on it should reattach it and your database row will be updated at transaction commit / flush. If it's a new instance, then the entity manager will always create a new record.
As for your delete issue, I don't know what it could be. Perhaps you can post a code sample? You can find a complete CRUD sample app using the JPA provider here:
https://github.com/forcedotcom/javasample-musiclib

Entity Framework: Modeling against an existing database scheme

I've been scratching my head over this for over a week now and haven't gotten anywhere :( We have an existing legacy DB that I'm trying to model my entities against. The tables are extremely bloated and we do not have enough bandwidth to create new, optimized tables. So I'm having to work with what we already have. However, I do not want to use all the redundant columns that are exposed by the DB. My initial plan was to use Views in my Model but that is looking to be equally hairy with very little documentation around.
Now, what would be the best way to go about creating a Model with just a select few columns? All I need is a bunch of read-only entities; so if there is a way to ignore non-nullable columns from the schema, I'd be all set. I was planning on making use of POCOs else I'd have to create my own mappings I reckon.
UPDATE: By POCOs, I mean I'd like to use the ADO.NET POCO Entity Generator.
What about creating views in the DB, and only importing the views into the model?
Well, if you need only a bunch of entities and if they won't change a lot during time, than I would just pick the tables you need, generate the entities with the normal wizard and all collumns, and than delete all not needed collumns manually in the model designer.
add the table to your EF, and just delete the properties you don't want. it just won't map those DB fields.

Entity Framework and soft deleted records

I'm developing WCF service which is using entity framework as data source. Almost all is ok except problem with deleted records. In our database we're using soft delete (mark record attribute IsDeleted = true). My question how to exclude soft deleted records from entity set?
For example, entity "A" has entity set "Bs" (FK to table "B").
How to make that "Bs" entity set only contains from records which is not deleted?
Thank you
I have written a post about this topic, hope it helps.
http://blog.jorgef.net/2010/12/ef-soft-delete.html
One way would be to use a defining query. But we typically handle this in the Repository, since we actually do want to materialize "soft deleted" entities in rare cases.
You could map you EF entities to views instead of tables
CREATE VIEW vw_Currency AS
SELECT
*
FROM
Currency c
WHERE
c.IsAKDeleted=0
I have worked on a system which used this approach but it was not based on EF. I have not tried it with EF