How can I modify Edm types in olingo by mapping in Olingo - jpa

In MySQL the column that are boolean are modeled via bit(1) or byte(1).
When I am using Olingo/Jpa beside MySQL and generate the entities in the Eclipse, it will assign type byte to these columns. It means in the metadata we will have something like this:
<Property Name="Deleted" Type="Edm.Byte" Nullable="false"/>
How can I modify the EDM type like the following?
<Property Name="Deleted" Type="Edm.Boolean" Nullable="false"/>
What I want to do is to do this modification with a mapping file, something like what explained here. (By this tutorial we can only change the names and not the types!)
Please note that I make eclipselink-orm.xml also automatically. I don't want to modify this file but it seems the type can be changed there as it has a line like this:
<basic name="deleted" attribute-type="byte">
However I don't want to modify this file each time while I generate it via Eclipse. Is there anyway that I extend some attributes there and inherent the rests?

It is not possible by olingo mapping file. This file is used only for renaming or exclusing the attributes or sets. We need to generate eclipselink-orm.xml file. You don't need to do the steps by hand. You can easily select the Dynamic Entities from Tables menu from JPA menu like this wizard:
And then define the suitable mapping types for the intended columns as follows in the last step of the wizard.
For example here in the above figure I defined the boolean for column deleted for table conditions or the entity condition!

Related

jhipster - JPA entity with self reference

I am trying out jhipster and I wonder how to define a self reference within an entity.
Something like: a "topic" has a field refering itself as parent or child. Going through the generator I did not see a possibility to do so.
That`s why I generated a topic entity and added a field myself in the entity, howevever, it seems like changes in the entity are not picked up by liquibase.
Readint the docs it seems like the topic.json file from jhipster is parsed for this, but, that does not support the self reference. So I am kind of stuck here.
Any ideas how to achieve what I want?
Thanks,
Sven
liquibase creates the database tables using changelogs.
Look into the resources/config/liquibase/changelog folder
there should be a file named like "201531081212_added_entity_Topic.xml"
when you're adding properties, you have to change the table description of this changelog.
normally, it would be better to create a new, additional changelog when adding/removing columns or tables. This should contain somthing like this:
<addColumn tableName="TOPIC">
<column name="topic_id" type="bigint"/>
</addColumn>
or parent_id or how you named the field in the entity class.
asfair there are also some maven goals as part of the maven liquibase plugin to generate a new changelog.
Changing/Migrating entities is not yet supportet well by jhipster...

ORM to create single entity from more than one database tables

Well tested running system have already defined entity called 'User'.
Now I need to add a new property to User entity (ex: Age)
To do this in the safe way, I do not like to do any changes with the existing data base table, because that is very risky in my case. I need a way to rebuild the User entity with the minimum code changes.
So my proposal is:
Create a new table (user_age), with two columns (user_id, age)
Modify the user entity to add property 'age' and its getter-setters
So my entity (User) properties, will be saved to two different tables (user and user_age)
Loading the user is also similarly.
Is this possible to do with hibernate....??
If not, Any other safer way to do this with Hibernate...?
what are the available ORMs that provide this kind of feature (nhibernate, entityframwork,etc... or any other ORM)...?
Yes, there are various approaches:
[1] See JPA Secondary Tables. This allows you to map an Entity to two or more tables.
Section 2.2.7: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e2235
[2] Create another Entity, say UserInfo, mapped to this new table. Create a one-to-one mapping from User to UserInfo.
Yes. You can do that.
I've used for a similar problem a joined-subclass.
Base:
<class name="User" table="Users">
<id name="Code" type="System.Guid">
<column name="Code" />
<generator class="guid.comb" />
</id>
...
</class>
Subclass:
<joined-subclass name="UserExt" extends=User" table="UsersExt">
<key column="Code" />
<property name="Age">
<column name="Age" not-null="true" />
</property>
</joined-subclass>
A good reference here.
NHibernate's join mapping is for exactly this case.
See Ayende's blog and the documentation for more information. From the documentation:
Using the <join> element, it is possible to map properties of one class to several tables, when there's a 1-to-1 relationship between the tables.
From my searches, it looks like it is also possible to do this with Entity Framework: Simon J Ince - Mapping two Tables to one Entity in the Entity Framework . I think this article is about Entity Framework v1, and things could have changed by now, but it appears that there is an important limitation in Entity Framework's version of this mapping:
... it requires a record in each table to exist as the generated SQL uses an INNER JOIN. It makes sense if you're using a new model, but I guess this is more tricky if you're mapping to an existing schema and data.
With NHibernate, you can set the optional attribute on the join mapping to tell it to use outer joins instead of inner joins.
optional (optional - defaults to false): If enabled, NHibernate will insert a row only if the properties defined by this join are non-null and will always use an outer join to retrieve the properties.

Plural table names with Entity Frameworks Model First

I'm giving EF Model first a go. I'm using EF 4.1
Pretty much followed this article
I've set PluraliseNewObjects to False on the Model and also in Options->Database Tools ->O/R Designer set Pluralization of names to false.
Neither have any effect - when I generate a new schema from the model the table names are always pluralised - is it possible to disable this?
OK - I've found one way to achieve what I want - but it's a pretty daft route.
Generated db with the plural names (interesting that it only pluralised the tables mapping to types - not the auto-generated linking tables for many to many joins).
Manually renamed the tables in the database
Deleted Model from the project and recreated based on existing database schema (the one I've just renamed).
Model is now correctly mapped to singularly names tables.
I'll wait and see if anyone comes up with a more sensible way of achieving this....
The names of the tables in the generated DDL seem to match the "Entity Set Name" values (different than the "Entity Name"). If you singularize the Entity Set Names, the table names in the DDL are singularized as well.
This will have the possibly undesired effect of singularizing the EntitySet property names in your code, though. Instead of:
myDatabase
.Products
.Where...
.Select...
your code will look like:
myDatabase
.Product
.Where...
.Select...
may or may not be an issue

Why can't I use a View containing a Union in Entity Framework 4.0?

I have a View which looks similar to this:
SELECT Id, Name
FROM Users
UNION ALL
SELECT NULL as [Id], NULL as [Name]
When I try to map to this view in Entity Framework, it just fails. I don't get an error, but the view does not exist in my data store. Why is this? Is there a way around it?
I know this is an older question already marked as answered, but I wanted to post an alternative to editing the edmx. I learned this one the hard way, after tons of Google searches and pulling my hair out for hours, trying different options...
For a view, EF attempts to to infer a primary key by identifying columns that are non-nullable
and non-binary (see Create an Entity Key When No Key Is Inferred).
With a view used to flatten related data for lookup purposes, this can result in many columns (or the wrong ones) being inferred as keys.
For a view with a UNION, it can cause the opposite problem, because there may be no true identity column that can be safely included as a key.
To force EF to identify columns as a key, you can use ISNULL() to ensure the value is non-nullable: ISNULL(column_name, replacement_value)
To force EF to not mark a column as a key, you can use NULLIF() to make it nullable: NULLIF(column_name, value_not_equal_to_parameter_1)
If you need to ensure a value is returned, but don't want to have it marked as a key, I believe COALESCE(column_name, replacement_value) will do the job of ISNULL without EF marking the column as a key
If there is truly no viable column available as a primary key (as in a UNION view), you can fake a non-nullable PK through the use of ISNULL() combined with ROW_NUMBER() in your SELECT statement: SELECT ISNULL(ROW_NUMBER() OVER (ORDER BY sort_criteria), 0) as 'ALIASNAME'
As an alternative, the edmx can absolutely be edited directly as Marcos Prieto suggested, but you run the risk of having those changes overwritten the next time you run "Update Model from Database".
Hope this helps anyone who encounters this in the future!
It is because Visual Studio cannot infers the Primary Key of your View.
You can see the error message within edmx file by open it with XML editor and see the SSDL section.
Here is error message that results from my Model(which I created some View like yours within my Database just to emulate) :
Errors Found During Generation:
warning 6013: The table/view 'PhoneBook.dbo.ContactCustomer' does not have
a primary key defined and no valid primary key could be inferred.
This table/view has been excluded. To use the entity,
you will need to review your schema,
add the correct keys, and uncomment it.
It is not true that Union is not supported in EF 4.
But I think the problem is that Visual Studio saw your view as the odd View.
You can doing some experiment by create another View and compares them (using update model from database menu within model designer).
And you can modify the Model by hand (manual typing the edmx file) to define the Primary Key to resolve this.
I had a view that worked perfectly. I modified it (changed the view on a union of 2 tables), updated the model from database and this problem appeared.
I fixed it in 3 steps:
Open the .edmx file with a XML editor.
Uncomment the view's EntityType XML (edmx:StorageModels > Schema) code and add the Key:
<EntityType Name="your_view">
<Key>
<PropertyRef Name="your_id" />
</Key>
<Property Name="your_id" Type="int" Nullable="false" />
<Property Name="other_field" Type="varchar" MaxLength="45" />
</EntityType>
Be sure that EF didn't erased the view in edmx:StorageModels > Schema > EntityContainer (if you have code repository, copy the code from there):
<EntitySet Name="your_view" EntityType="Your_Model.Store.your_view" store:Type="Views" store:Schema="your_schema" store:Name="your_view">
<DefiningQuery>SELECT
`your_view`.`your_id`,
`your_view`.`other_field`,
FROM `your_view` AS `your_view`
</DefiningQuery>
</EntitySet>
I know this is an old question but i faced this issue recently & after trying to do the above mentioned methods i simply created another view that selects from the Union view & i was able to map the new view by updating my entity model.

How to get store type of an entity set

I'm trying to filter entities based on their store types (either table or view).
I have 2 entities in my test project, one's source is a table and the other's source is a view.
<EntitySet Name="Test" EntityType="TestModel.Store.Test" store:Type="Tables" Schema="dbo" />
<EntitySet Name="TestView" EntityType="TestModel.Store.TestView" store:Type="Views" store:Schema="dbo" store:Name="TestView">
The code sample above is taken from model's edmx file's SSDL section.
I think the store:Type information in SSDL is what i need but i couldn't find a way to retrieve that value using entity-framework api.
Any help will be appreciated.
Well you can query the metadata in the SSDL by looking in the SSpace or the StoreItemCollection.
i.e.
var sspaceEntitySets = context.MetadataWorkspace
.GetItems<EntityContainer>(DataSpace.SSpace)
.First().BaseEntitySets.OfType<EntitySet>();
var entitySet = sspaceEntitySets.First();
var tableType = entitySet
.MetadataProperties["http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator:Type"]
.Value.ToString();
Unfortunately this isn't going to help you tie your classes to whether they come from a table or a view. Because the Entities (i.e. the ones you code against in CSpace) rather than the ones that describe the shapes of the table (i.e. SSpace ones) are in CSpace and in order to know whether an Entity comes from a view or table, you would need to be able to get from the CSpace EntitySet to SSpace EntitySet via the Mapping.
Unfortunately the EF doesn't expose public CSSPace (i.e. there is no way to use the API to read the MSL fragment of the EDMX).
So in order to do this you would have to manually reason over the MSL element, probably using LINQ to XML or something.
Hope this helps
Alex