Liferay 7 entity not accepting date as primary key - date

So i am using liferay 7
and i created many columns in order to get infos from the databse,
i have a primary key which will be composed of three columns ( int , String ,Date)
but the problem is that liferay7 is not accepting DATE as a primary key and not even building service when i
add primary="true" to the column which is a date
<column db-name="A" type="Date" name="date" ></column>
<column db-name="B" type="int" name="id" primary="true"></column>
<column db-name="C" type="String" name="idU" primary="true"></column>

Related

Liquibase: added autoIncrement column into a table with data already present

Hi guys I have some doubt about Liquibase
I created a table (PostgreSQL) with a classical changeset
<changeSet id="create_table">
<createTable tableName="table" schemaName="schema">
<column name="name" type="varchar">
<constraints nullable="false"/>
</column>
<column name="surname" type="varchar">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
I've already added some data to this table.
I want to add a new column 'id' and make it as a primary key as an autoincrement column.
How can I do that?
Because if I try to add a changeset like this:
<changeSet id="added_pk">
<addColumn tableName="table"
schemaName="schema">
<column name="id" type="bigint">
<constraints unique="true" nullable="false" uniqueConstraintName="PK_TABLE_ID"/>
</column>
</addColumn>
<addAutoIncrement columnDataType="bigint"
columnName="id"
tableName="table"
schemaName="schema"
incrementBy="1" startWith="1"/>
</changeSet>
I obtain error when I run the script: "column "id" of relation "table" contains null values".
It depends of the already existing values. How could I manage this situation? (I prefer to avoid to truncate all the data before add the new column)
Thanks a lot!
Regards
Rename the current table in the DB. Delete the liquibase changelogs. Rerun the liquibase file including the new id field (you can combine the two changeSets, this way you also get to decide the position of the id field). After the new table has been created you can transfer the data from the old table into the new table. This way the data from the old table will automatically receive ids. Delete the old table at the end.

Liferay service.xml foreign key many-to-many

Hello Liferay experts,
Please help me to create a service.xml in Liferay for the following scenario:
I have 2 tables
SystemUsed
systemUsedId long
name varchar
User_SystemUsed
userId long --> mapped to userId of User_ table
systemUsedId long --> mapped to systemUsedId of SystemUsed table.<br>
Thanks
You can create tables as follows:
<entity name="SystemUsed" local-service="true" remote-service="false">
<column name="systemUsedId type="Long" />
<column name="name" type="String"/>
</entity>
<entity name="User_SystemUsed" local-service="true" remote-service="false">
<column name="systemUsedId" type="SystemUsed" entity="SystemUsed" mapping-key="systemUsedId" />
<column name="userId" type="User" entity="User_" mapping-key="userid" />
</entity>
I hope the user_ table is mapped correctly like that...

Entity Framework Self Referencing Hierarchical Many To Many

OK this problem has it all.
Conceptually I have a Resource entity which can have many Child Resources and many Parent Resources. The Resource table has two fields, ID and Name with ID being the primary key.
To complete the many to many relationship I created a ResourceHierarchy table which has two fields, (Parent_ID, Child_ID) and two foreign keys with each one referencing the primary key of the Resource table (ID) and the ResourceHierarchy table has a composite primary key of (Parent_ID, Child_ID)
Now we already know that each Resource can act as a Parent or a Child to other resources, however logically not all Resources will have a Parent but that's besides the point. As an example lets say I have the following Resources in my Resource table.
ID Name
10000 Little House
10001 Font Door
10002 Roof
10003 Roof Tile
10004 Tile Monster
And in the ResourceHierarchy table we have the following relationships.
Parent_ID Child_ID
10000 10001
10000 10002
10002 10003
10004 10003
Then Entity Framework generates the Entity, so far so good...
If you were to check the generated code in the edmx file you would see that the ResourceHierarchy table is being treated as a relationship and the ResourceHierarchy table is not accessible via code because it's not being treated as an entity.
If this is all I wanted then it would work out perfectly.
However the problem starts when I want to add a quantity column to the Resource entity hierarchy. For example the little house has just one Front Door and one Roof, but the Roof and Tile Monster Resources can have many Roof Tiles.
So if we add a Quantity column to the Resource table then we get the following.
ID Name Quantity
10000 Little House 1
10001 Font Door 1
10002 Roof 1
10003 Roof Tile 5
10004 Tile Monster 1
This creates the problem that the Roof and Tile Monster must share the 5 Roof Tiles. So naturally I would try to add the quantity column to the ResourceHierarchy table, however as soon as I do this and refresh the generated code is now treating the ResourceHierarchy table as an entity and not a relationship as it was previously. And now in order to get back to the Resource table I have to go through this non conceptual "Entity/Relationship" which isn't very straight forward. It's like I have a Entity in my conceptual model which would only be used to traverse back to the Resource Entity, and I'm not even sure if Resource.Children.Add(r) would create new rows in the ResourceHierarchy table in the db. It's like I would be picking off properties i.e. Quantity, off of an entity that I am only using as a relationship.
Ideally the ResourceHierarchy table would have the Quantity column look like this.
Parent_ID Child_ID Quantity
10000 10001 1
10000 10002 1
10002 10003 8
10004 10003 13
AND the Resource Entity would still have Children, Parents navigation properties and somehow access the Quantity column as a property of the Resource Entity.
I've tried to merge the generated code from having a quantity column and not having a quantity column but an exception is thrown which I interpret as the ResourceHierarchy table can either be a relationship or an entity, but not both.
Please HELP!
The edmx changes drastically with the addition and exclusion of the quantity column on the ResourceHierarchy table in the db.
Here is a sample comparison, the only difference is Resource is ResourceType and ResourceHierarchy is ResourceTypeHierarchy.
The SSDL Storage Model has no changes except one extra Property in the ResourceTypeHierarchy EntityType so I won't include it below.
WITHOUT QUANTITY COLUMN ON RESOURCETYPEHIERARCHY
RESOURCETYPEHIERARCHY IS A RELATIONSHIP
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="MyModel" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="MyEntities">
<EntitySet Name="ResourceTypes" EntityType="MyModel.ResourceType" />
<AssociationSet Name="ResourceTypeHierarchy" Association="MyModel.ResourceTypeHierarchy">
<End Role="ResourceType" EntitySet="ResourceTypes" />
<End Role="ResourceType1" EntitySet="ResourceTypes" /></AssociationSet></EntityContainer>
<EntityType Name="ResourceType">
<Key>
<PropertyRef Name="ID" /></Key>
<Property Type="Int32" Name="ID" Nullable="false" />
<Property Type="String" Name="Type" Nullable="false" MaxLength="25" FixedLength="false" Unicode="false" />
<NavigationProperty Name="Parents" Relationship="MyModel.ResourceTypeHierarchy" FromRole="ResourceType" ToRole="ResourceType1" />
<NavigationProperty Name="Children" Relationship="MyModel.ResourceTypeHierarchy" FromRole="ResourceType1" ToRole="ResourceType" /></EntityType>
<Association Name="ResourceTypeHierarchy">
<End Type="MyModel.ResourceType" Role="ResourceType" Multiplicity="*" />
<End Type="MyModel.ResourceType" Role="ResourceType1" Multiplicity="*" /></Association></Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
<EntityContainerMapping StorageEntityContainer="MyModelStoreContainer" CdmEntityContainer="MyEntities">
<EntitySetMapping Name="ResourceTypes">
<EntityTypeMapping TypeName="IsTypeOf(MyModel.ResourceType)">
<MappingFragment StoreEntitySet="ResourceType">
<ScalarProperty Name="ID" ColumnName="ID" />
<ScalarProperty Name="Type" ColumnName="Type" /></MappingFragment></EntityTypeMapping></EntitySetMapping>
<AssociationSetMapping Name="ResourceTypeHierarchy" TypeName="MyModel.ResourceTypeHierarchy" StoreEntitySet="ResourceTypeHierarchy">
<EndProperty Name="ResourceType1">
<ScalarProperty Name="ID" ColumnName="Parent_ID" /></EndProperty>
<EndProperty Name="ResourceType">
<ScalarProperty Name="ID" ColumnName="Child_ID" /></EndProperty></AssociationSetMapping></EntityContainerMapping>
</Mapping>
</edmx:Mappings>
WITH QUANTITY COLUMN ON RESOURCETYPEHIERARCHY
RESOURCETYPEHIERARCHY IS NOW AN ENTITY
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
<EntityContainerMapping StorageEntityContainer="MyModelStoreContainer" CdmEntityContainer="MyEntities">
<EntitySetMapping Name="ResourceTypes">
<EntityTypeMapping TypeName="IsTypeOf(MyModel.ResourceType)">
<MappingFragment StoreEntitySet="ResourceType">
<ScalarProperty Name="ID" ColumnName="ID" />
<ScalarProperty Name="Type" ColumnName="Type" /></MappingFragment></EntityTypeMapping></EntitySetMapping>
<EntitySetMapping Name="ResourceTypeHierarchies">
<EntityTypeMapping TypeName="IsTypeOf(MyModel.ResourceTypeHierarchy)">
<MappingFragment StoreEntitySet="ResourceTypeHierarchy">
<ScalarProperty Name="Child_ID" ColumnName="Child_ID" />
<ScalarProperty Name="Parent_ID" ColumnName="Parent_ID" />
<ScalarProperty Name="Quantity" ColumnName="Quantity" /></MappingFragment></EntityTypeMapping></EntitySetMapping>
<AssociationSetMapping Name="FK_Child" TypeName="MyModel.FK_Child" StoreEntitySet="ResourceTypeHierarchy">
<EndProperty Name="ResourceTypeHierarchy">
<ScalarProperty Name="Child_ID" ColumnName="Child_ID" />
<ScalarProperty Name="Parent_ID" ColumnName="Parent_ID" /></EndProperty>
<EndProperty Name="ResourceType">
<ScalarProperty Name="ID" ColumnName="Child_ID" /></EndProperty></AssociationSetMapping>
<AssociationSetMapping Name="FK_Parent" TypeName="MyModel.FK_Parent" StoreEntitySet="ResourceTypeHierarchy">
<EndProperty Name="ResourceTypeHierarchy">
<ScalarProperty Name="Child_ID" ColumnName="Child_ID" />
<ScalarProperty Name="Parent_ID" ColumnName="Parent_ID" /></EndProperty>
<EndProperty Name="ResourceType">
<ScalarProperty Name="ID" ColumnName="Parent_ID" /></EndProperty></AssociationSetMapping></EntityContainerMapping>
</Mapping>
</edmx:Mappings>
I believe this is working as espected. What I mean is, if you put an attribute on a table that establishes a relation between two entities, that table MUST (from data representation theory at least) be represented as a proper entity. If you think about this carefully you'll understand why. The quantity you mentioned is an attribute of the relation not of any of the related entities. As such that table must be treated as an entity and not as a relationship.
Now on how to overcome this, one thing that comes to my mind (although I'm not sure if this will completly solve your problem) is to treat the relationship as you originally thought (without the quantity) and have another table (that will be mapped to an Entity in you model) that stores the quantity of a certain relation. I think that this table can even have a foreign key constraint on you db to the original relationship table, although this foreign key can't be mapped to a relation on your model (because you have no entity for the endpoint), but this still allows you to mantain data integrity on you storage.
Hope this helps,
VĂ­tor

In Entity Framework, how can I create a referrential constraint using a subset of the primary key?

My data model contains two tables with composite primary keys and an associative table. Part of the composite primary key is common between the tables.
SitePrivilege
-------------
SiteId
PrivilegeId
UserSite
--------
SiteId
UserId
UserSitePrivilege
-----------------
UserId
SiteId
PrivilegeId
I have created a SitePrivilege entity and a UserSite entity. I have mapped a many-to-many association between them to UserSitePrivilege.
<Association Name="UserSiteSitePrivilege">
<End Type="PrivilegeModel.UserSite" Multiplicity="*" Role="UserSite" />
<End Type="PrivilegeModel.SitePrivilege" Multiplicity="*" Role="SitePrivilege" />
</Association>
...
<AssociationSetMapping Name="UserSiteSitePrivilege" TypeName="PrivilegeModel.UserSiteSitePrivilege" StoreEntitySet="UserSitePrivilege">
<EndProperty Name="SitePrivilege">
<ScalarProperty Name="PrivilegeId" ColumnName="PrivilegeId" />
<ScalarProperty Name="SiteId" ColumnName="SiteId" />
</EndProperty>
<EndProperty Name="UserSite">
<ScalarProperty Name="SiteId" ColumnName="SiteId" />
<ScalarProperty Name="UserId" ColumnName="UserId" />
</EndProperty>
</AssociationSetMapping>
The above code produces this error:
Each of the following columns in table
UserSitePrivilege is mapped to
multiple conceptual side properties:
UserSitePrivilege.SiteId is mapped to
UserSiteSitePrivilegeSitePrivilege.SiteId,
UserSiteSitePrivilege.UserSite.SiteId
So I added a referential constraint.
<Association Name="UserSiteSitePrivilege">
<End Type="PrivilegeModel.UserSite" Multiplicity="*" Role="UserSite" />
<End Type="PrivilegeModel.SitePrivilege" Multiplicity="*" Role="SitePrivilege" />
<ReferentialConstraint>
<Principal Role="UserSite">
<PropertyRef Name="SiteId"/>
</Principal>
<Dependent Role="SitePrivilege">
<PropertyRef Name="SiteId"/>
</Dependent>
</ReferentialConstraint>
</Association>
...
<AssociationSetMapping Name="UserSiteSitePrivilege" TypeName="PrivilegeModel.UserSiteSitePrivilege" StoreEntitySet="UserSitePrivilege">
<EndProperty Name="SitePrivilege">
<ScalarProperty Name="PrivilegeId" ColumnName="PrivilegeId" />
<ScalarProperty Name="SiteId" ColumnName="SiteId" />
</EndProperty>
<EndProperty Name="UserSite">
<ScalarProperty Name="SiteId" ColumnName="SiteId" />
<ScalarProperty Name="UserId" ColumnName="UserId" />
</EndProperty>
</AssociationSetMapping>
Now it produces this error:
Properties referred by the Principal
Role UserSite must be exactly
identical to the key of the EntityType
PrivilegeModel.UserSite referred to by
the Principal Role in the relationship
constraint for Relationship
PrivilegeModel.UserSiteSitePrivilege.
Make sure all the key properties are
specified in the Principal Role.
How do I correctly model this relationship?
Overlapping FKs like this are not supported in 3.5 SP1.
i.e.
UserSitePrivilege
----------
UserId
SiteId
PrivilegeId
PK => UserId, SitedId, PrivilegeId
FK1 => UserId, SiteId
FK2 => SiteId, PrivilegeId
FK1 overlaps with FK2. This is going to be supported as of Beta2 of EF 4. This is because FK Associations (which is available in Beta2) are much more flexible than Independent Associations (what you have in 3.5 SP1 and 4.0 Beta 1).
See this post for more on FK Associations
In the meantime your only option is probably to hide all of this behind DefiningQueries and CUD procedures etc.
Alex
If your primary key is a compound key, all your foreign key relationships must also be using the entire compound key (all key columns) for reference - I don't see any way to get around this, this is a basic tenet of relational database design, really.
This is definitely one of the major reasons I would probably choose to use a substitute column on the main table for the primary key, instead of a compound key made up from actual data columns.
UPDATE: yes, based on your comment, you're absolutely right - the DB design is solid. Not quite sure why EF can't deal with this....
Marc

Many-to-many relationship with surrogate key in Entity Framework

Entity Framework magically interprets the following table structure as a many-to-many relationship.
table foo (int id)
table foo_bar (int foo_id, int bar_id)
table bar (int id)
But if the join table has any additional fields it will instead be interpreted as two one-to-many relationships.
I am using a database in which the join table has a surrogate key as primary key. Because of this EF interprets it as two one-to-many relationships.
table foo (int id)
table foo_bar (int surrogate_pk, int foo_id, int bar_id)
table bar (int id)
Is it possible to modify EF:s interpretation to make it an actual many-to-many relationship in the model? Can it be done using the designer?
it's possible, but it requires quite a bit of manual work in the EDMX file, and I haven't been able to make EF use the surrogate key as actual primary key on the link table. You have to make EF use a combination key of both foo_id and bar_id as primary key.
in your storage model you have to change the EntityType of the link table from
<EntityType Name="foo_bar">
<Key>
<PropertyRef Name="surrogate_pk" />
</Key>
<Property Name="surrogate_pk" Type="bigint" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="foo_id" Type="int" Nullable="false" StoreGeneratedPattern="None" />
<Property Name="bar_id" Type="int" Nullable="false" StoreGeneratedPattern="None" />
</EntityType>
to:
<EntityType Name="foo_bar">
<Key>
<PropertyRef Name="foo_id" />
<PropertyRef Name="bar_id" />
</Key>
<Property Name="foo_id" Type="int" Nullable="false" StoreGeneratedPattern="None" />
<Property Name="bar_id" Type="int" Nullable="false" StoreGeneratedPattern="None" />
</EntityType>
So you make the surrogate key invisible to EF, and tell it to use the combination of the two foreign keys as primary key.
In your conceptual model, you need to have the many-many association defined:
<Association Name="foo_bar_association">
<End Role="foo" Type="foo" Multiplicity="*" />
<End Role="bar" Type="bar" Multiplicity="*" />
</Association>
and in your mappings, an AssociationSetMapping:
<AssociationSetMapping Name="foo_bar_association" TypeName="foo_bar_association" StoreEntitySet="foo_bar">
<EndProperty Name="foo">
<ScalarProperty Name="id" ColumnName="foo_id" />
</EndProperty>
<EndProperty Name="bar">
<ScalarProperty Name="id" ColumnName="bar_id" />
</EndProperty>
</AssociationSetMapping>
By far the easiest way to get this right, is to remove the surrogate key from the db, generate the EDMX, and then put this model on your original DB. The result will be the same. EF doesn't really need the surrogate key for anything, the table is invisible in a many-many association
I am positive that this cannot be done using the designer. I don't know if there is a way to do it in EDMX manually, but I have never seen an example of it. One possible workaround might be to not map the surrogate key at all. If you can generate that on the database, you might not need it in your model.