Hibernate exception javassist - eclipse

I need some help as I'm stumped on an Hibernate 4/Struts 2 project. This is my first Hibernate 4 (4.3.11) project, as I worked for years with Hibernate 3. The database is MySQL 5.
All mapping classes were produced with Hibernate Tools provided by JBoss Tools 4.3.5, on Eclipse Mars 2. No problem was encountered, it worked fine.
But when testing, I faced this exception :
2017-02-16 17:23:45 ERROR Dispatcher:38 - Exception occurred during processing request: metier.Ville_$$_javassist_2 cannot be cast to javassist.util.proxy.Proxy
java.lang.ClassCastException: metier.Ville_$$_javassist_2 cannot be cast to javassist.util.proxy.Proxy
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:147)
at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:75)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:771)
I read that post but I don't understand what's happening.
Two classes are involved : Salle and Ville. Here's the Hibernate XML files : Salle.hbm.xml & Ville.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2 f?vr. 2017 11:28:16 by Hibernate Tools 4.3.5.Final -->
<hibernate-mapping>
<class name="metier.Salle" table="salle" catalog="aevbadherents" optimistic-lock="version">
<id name="idSalle" type="int">
<column name="idSALLE" />
<generator class="assigned" />
</id>
<many-to-one name="ville" class="metier.Ville" fetch="select">
<column name="idVilleSalle" />
</many-to-one>
<property name="adresse1" type="string">
<column name="Adresse1" length="80" />
</property>
<property name="adresse2" type="string">
<column name="Adresse2" length="80" />
</property>
<property name="description" type="string">
<column name="Description" length="500" />
</property>
<set name="courses" table="cours" inverse="true" lazy="true" fetch="select">
<key>
<column name="IdSalle" not-null="true" />
</key>
<one-to-many class="metier.Cours" />
</set>
<set name="passagegrades" table="passagegrade" inverse="true" lazy="true" fetch="select">
<key>
<column name="IdSalle" />
</key>
<one-to-many class="metier.Passagegrade" />
</set>
</class>
</hibernate-mapping>
The second file :
<hibernate-mapping>
<class name="metier.Ville" table="ville" catalog="aevbadherents" optimistic-lock="version">
<id name="idVille" type="int">
<column name="idVILLE" />
<generator class="assigned" />
</id>
<many-to-one name="departement" class="metier.Departement" fetch="select">
<column name="idDepartement" not-null="true" />
</many-to-one>
<property name="nom" type="string">
<column name="Nom" length="60" not-null="true" />
</property>
<property name="codepostal" type="string">
<column name="codepostal" length="6" not-null="true" />
</property>
<property name="gpslat" type="string">
<column name="GPSLat" length="12" not-null="true" />
</property>
<property name="gpslon" type="string">
<column name="GPSLon" length="12" not-null="true" />
</property>
<set name="adherentsForIdVilleResid" table="adherent" inverse="true" lazy="true" fetch="select">
<key>
<column name="IdVilleResid" />
</key>
<one-to-many class="metier.Adherent" />
</set>
<set name="clubs" table="club" inverse="true" lazy="true" fetch="select">
<key>
<column name="idville" not-null="true" />
</key>
<one-to-many class="metier.Club" />
</set>
<set name="salles" table="salle" inverse="true" lazy="true" fetch="select">
<key>
<column name="idVilleSalle" />
</key>
<one-to-many class="metier.Salle" />
</set>
<set name="adherentsForIdVilleNais" table="adherent" inverse="true" lazy="true" fetch="select">
<key>
<column name="IdVilleNais" />
</key>
<one-to-many class="metier.Adherent" />
</set>
</class>
</hibernate-mapping>
I read that this exception may be caused by a JAR conflict, but I did not find anything.I don't use Maven (maybe I should ?), so let me show you the JARs included :
antlr-2.7.7.jar
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.2-javadoc.jar
commons-logging-1.2.jar
dom4j-1.6.1.jar
freemarker-2.3.19.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.11.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
log4j-1.2.15.jar
mysql-connector-java-5.1.40-bin.jar
ognl-3.0.6.jar
struts2-core-2.3.15.3.jar
truc.txt
xwork-core-2.3.15.3.jar
Please help me, as I'm stumped...

One of the possible cause for this problem is that you have in your classpath several versions of the same class.
I have been looking in java2s.com and, none of the jar you listed, seems to contain the class javassist.util.proxy.Proxy, so I will try to find if any other jar contains that class using jarscan with the following command:
jarscan -d PATH_TO_YOUR_CLASSPATH_DIR -j javassist.util.proxy.Proxy
You can download jarscan from here https://java.net/projects/jarscan/downloads
If you found that several packages contains the same class, you are done.

Malaguna,
Thanks for your help. Here's the result of jarscan :
D:\temp\java\jarscan>jarscan -j javassist.util.proxy.Proxy
......................
+javassist-3.18.1-GA.jar
javassist-3.18.1-GA.jar\javassist/util/proxy/Proxy.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$1.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$2.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$3.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$ClassLoaderProvide
r.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$Find2MethodsArgs.c
lass
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$ProxyDetails.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory$UniqueName.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyFactory.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyObject.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyObjectInputStream.class
javassist-3.18.1-GA.jar\javassist/util/proxy/ProxyObjectOutputStream.class
----------------------------------------------
Scanned archives: 22
Errors: 0
Archives with hits: 12
So, it means that no other jar contains that class.

Related

openjpa enhancer fails on one-to-many, but only if its after one-to-one?

I get the following exception when I have my open JPA entity mapping set up with a one-to-many mapping listed after a one-to-one mapping. When I switch the order, I don't get the exception.
I'm assuming that there is an XSD rule being applied, but what is the purpose of that rule?
Failed to execute goal
org.apache.openjpa:openjpa-maven-plugin:2.2.2:enhance (enhancer) on
project: Execution enhancer of goal
org.apache.openjpa:openjpa-maven-plugin:2.2.2:enhance failed:
org.xml.sax.SAXException: Invalid content was found starting with
element 'one-to-many'. One of
'{"http://java.sun.com/xml/ns/persistence/orm":one-to-one,
"http://java.sun.com/xml/ns/persistence/orm":many-to-many,
"http://java.sun.com/xml/ns/persistence/orm":element-collection,
"http://java.sun.com/xml/ns/persistence/orm":embedded,
"http://java.sun.com/xml/ns/persistence/orm":transient}' is expected.
<!-- FAILED -->
<entity class="com.test.comm">
<table schema="dbo" name="tbl_comm_data"/>
<attributes>
<id name="commId">
<column name="comm_id"/>
</id>
<basic name="commName">
<column name="comm_name"/>
</basic>
<one-to-one name="CommType" target-entity="com.test.TblCommType" mapped-by="TblComm" fetch="LAZY">
<cascade>
<cascade-merge/>
</cascade>
</one-to-one>
<one-to-many name="CommDtls" target-entity="com.test.TblCommDtl" mapped-by="tblCommFreq">
<cascade>
<cascade-merge/>
</cascade>
</one-to-many>
</attributes>
</entity>
<!-- WORKED -->
<entity class="com.test.comm">
<table schema="dbo" name="tbl_comm_data"/>
<attributes>
<id name="commId">
<column name="comm_id"/>
</id>
<basic name="commName">
<column name="comm_name"/>
</basic>
<one-to-many name="CommDtls" target-entity="com.test.TblCommDtl" mapped-by="tblCommFreq">
<cascade>
<cascade-merge/>
</cascade>
</one-to-many>
<one-to-one name="CommType" target-entity="com.test.TblCommType" mapped-by="TblComm" fetch="LAZY">
<cascade>
<cascade-merge/>
</cascade>
</one-to-one>
</attributes>
</entity>

Sakai Hibernate lazy initialize

I'm having some issues building my database. I have this two hbm mappings:
<class name="br.unicamp.iel.model.Module" table="readinweb_modules">
<id name="id" type="java.lang.Long">
<generator class="increment" />
</id>
<many-to-one name="course" class="br.unicamp.iel.model.Course"
column="course_id" fetch="select" />
<property name="position" type="integer" />
<property name="module_grammar" type="text" />
</class>
<class name="br.unicamp.iel.model.Course" table="readinweb_courses">
<id name="id" type="java.lang.Long">
<generator class="increment" />
</id>
<property name="title" length="255" not-null="true" type="string" />
<property name="idiom" length="255" not-null="true" type="string" />
<property name="description" type="text" />
<set name="courseModules" table="readinweb_modules"
inverse="true" lazy="true" fetch="select">
<key column="id" not-null="true" />
<one-to-many class="br.unicamp.iel.model.Module" />
</set>
</class>
and when I try to access data on my logic bean as:
List modules = new ArrayList(dao.findById(Course.class,
course).getCourseModules());
it gives me a
org.hibernate.LazyInitializationException: failed to lazily initialize
a collection of role: br.unicamp.iel.model.Course.courseModules, no
session or session was closed
We need to see the complete code of the
List modules = new ArrayList(dao.findById(Course.class, course).getCourseModules())
Do you open and close a Session (or a EntityManager) inside the dao.findById method? The session must still be open to resolve lazy relationships

EntityFramework Model First, Composite Primary Key From Foreign Keys

I have a Entity Framework 5 Model First project, with the following tables:
Extension
Id : Int32, PK
Number: String
User
Id: Int32, PK
Name: String
UserExtensionMap
UserId : Int32 (FK -> User.Id), PK
ExtensionId : Int32 (FK -> Extension.Id), PK
When setting the above tables and associations to UserExtensionMap I get the error that UserId and ExtensionId are not mapped.
So I right click on UserExtensionMap table and select table mapping and get the following screen
I can set the maps for Users.Id to UserExtensionUser.Id and Extensions.Id to UserExtension.ExtensionId but there is nothing to map the Extensions.Number and User.Username to - I am confused!
I get with the error when trying to compile:
Error 3024: Problem in mapping fragments starting at line 119:Must specify mapping for all key properties (UserExtensionMaps.ExtensionId, UserExtensionMaps.UserId) of the EntitySet UserExtensionMaps.
When i delete these above maps for UserExtensionMap i get the error:
Error 3027: No mapping specified for the following EntitySet/AssociationSet - UserExtensionMaps.
My model XML file is below:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="Server.Store" Alias="Self" Provider="System.Data.SqlServerCe.4.0" ProviderManifestToken="4.0" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<EntityContainer Name="ServerStoreContainer">
<EntitySet Name="Users" EntityType="Server.Store.Users" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Extensions" EntityType="Server.Store.Extensions" store:Type="Tables" Schema="dbo" />
</EntityContainer>
<EntityType Name="Users">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="Username" Type="nvarchar" Nullable="false" />
</EntityType>
<EntityType Name="Extensions">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="Number" Type="nvarchar" Nullable="false" />
</EntityType>
</Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="Server" Alias="Self" p1:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:p1="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityContainer Name="ServerEntities" p1:LazyLoadingEnabled="true" >
<EntitySet Name="Users" EntityType="Server.User" />
<EntitySet Name="Extensions" EntityType="Server.Extension" />
<EntitySet Name="UserExtensionMaps" EntityType="Server.UserExtensionMap" />
<AssociationSet Name="ExtensionUserExtensionMap" Association="Server.ExtensionUserExtensionMap">
<End Role="Extension" EntitySet="Extensions" />
<End Role="UserExtensionMap" EntitySet="UserExtensionMaps" />
</AssociationSet>
<AssociationSet Name="UserUserExtensionMap" Association="Server.UserUserExtensionMap">
<End Role="User" EntitySet="Users" />
<End Role="UserExtensionMap" EntitySet="UserExtensionMaps" />
</AssociationSet>
</EntityContainer>
<EntityType Name="User">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="String" Name="Username" Nullable="false" />
<NavigationProperty Name="UserExtensionMaps" Relationship="Server.UserUserExtensionMap" FromRole="User" ToRole="UserExtensionMap" />
</EntityType>
<EntityType Name="Extension">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="String" Name="Number" Nullable="false" />
<NavigationProperty Name="UserExtensionMaps" Relationship="Server.ExtensionUserExtensionMap" FromRole="Extension" ToRole="UserExtensionMap" />
</EntityType>
<EntityType Name="UserExtensionMap" >
<Key>
<PropertyRef Name="ExtensionId" />
<PropertyRef Name="UserId" />
</Key>
<NavigationProperty Name="Extension" Relationship="Server.ExtensionUserExtensionMap" FromRole="UserExtensionMap" ToRole="Extension" />
<Property Type="Int32" Name="ExtensionId" Nullable="false" />
<NavigationProperty Name="User" Relationship="Server.UserUserExtensionMap" FromRole="UserExtensionMap" ToRole="User" />
<Property Type="Int32" Name="UserId" Nullable="false" />
</EntityType>
<Association Name="ExtensionUserExtensionMap">
<End Type="Server.Extension" Role="Extension" Multiplicity="1" />
<End Type="Server.UserExtensionMap" Role="UserExtensionMap" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Extension">
<PropertyRef Name="Id" />
</Principal>
<Dependent Role="UserExtensionMap">
<PropertyRef Name="ExtensionId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="UserUserExtensionMap">
<End Type="Server.User" Role="User" Multiplicity="1" />
<End Type="Server.UserExtensionMap" Role="UserExtensionMap" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="User">
<PropertyRef Name="Id" />
</Principal>
<Dependent Role="UserExtensionMap">
<PropertyRef Name="UserId" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
<EntityContainerMapping StorageEntityContainer="ServerStoreContainer" CdmEntityContainer="ServerEntities">
<EntitySetMapping Name="Users">
<EntityTypeMapping TypeName="IsTypeOf(Server.User)">
<MappingFragment StoreEntitySet="Users">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="Username" ColumnName="Username" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<EntitySetMapping Name="Extensions">
<EntityTypeMapping TypeName="IsTypeOf(Server.Extension)">
<MappingFragment StoreEntitySet="Extensions">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="Number" ColumnName="Number" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping></edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
<Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</Connection>
<Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
<DesignerProperty Name="EnablePluralization" Value="True" />
<DesignerProperty Name="IncludeForeignKeysInModel" Value="True" />
<DesignerProperty Name="CodeGenerationStrategy" Value="None" />
</DesignerInfoPropertySet>
</Options>
<!-- Diagram content (shape and connector positions) -->
<Diagrams></Diagrams>
</Designer>
</edmx:Edmx>
I could not see the table mapping fields until I generated the database code right click model -> Generate Database from model, after doing this the mappings were OK.
Not sure if this is a bug or not, but it is repeatable and does fix the issue.

Issue in EF, mapping fragment,no default value and is not nullable

I am developing MVC 3 Applicaiton.
I have Model first approch.
I have Company Entity(Abstract).
Lead and Customer is inherited from the company entity.
When I tried to validate the model, Its gives an errror.
Error 41 Error 3023: Problem in mapping fragments starting at line
70:Column Companies.Status in table Companies must be mapped: It has
no default value and is not nullable.
Here is the mapping of tables.
And Here is the EDMX code in HTML View.
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="Model1.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator">
<EntityContainer Name="Model1StoreContainer">
<EntitySet Name="Companies" EntityType="Model1.Store.Companies" store:Type="Tables" Schema="dbo" />
</EntityContainer>
<EntityType Name="Companies">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="Name" Type="nvarchar(max)" Nullable="false" />
<Property Name="Status" Type="nvarchar(max)" Nullable="false" />
<Property Name="__Disc__" Type="nvarchar" MaxLength="Max" Nullable="false" />
</EntityType>
</Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Model1" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
<EntityContainer Name="Model1Container" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Companies" EntityType="Model1.Company" />
</EntityContainer>
<EntityType Name="Company" Abstract="true">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="String" Name="Name" Nullable="false" />
</EntityType>
<EntityType Name="Lead" BaseType="Model1.Company" >
<Property Type="String" Name="Status" Nullable="false" />
</EntityType>
</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="Model1StoreContainer" CdmEntityContainer="Model1Container">
<EntitySetMapping Name="Companies">
<EntityTypeMapping TypeName="IsTypeOf(Model1.Company)">
<MappingFragment StoreEntitySet="Companies">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="Name" ColumnName="Name" />
<Condition ColumnName="__Disc__" Value="Company" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="Model1.Lead">
<MappingFragment StoreEntitySet="Companies">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="Name" ColumnName="Name" />
<ScalarProperty Name="Status" ColumnName="Status" />
<Condition ColumnName="__Disc__" Value="Lead" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping></edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
<DesignerProperty Name="EnablePluralization" Value="True" />
<DesignerProperty Name="DatabaseGenerationWorkflow" Value="$(VSEFTools)\DBGen\Generate T-SQL Via T4 (TPH).xaml" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="Model1" >
<EntityTypeShape EntityType="Model1.Company" Width="1.5" PointX="2.375" PointY="0.875" Height="1.2636116536458335" />
<EntityTypeShape EntityType="Model1.Lead" Width="1.5" PointX="3.375" PointY="2.625" Height="1.0992643229166665" />
<InheritanceConnector EntityType="Model1.Lead" >
<ConnectorPoint PointX="3.125" PointY="2.1386116536458335" />
<ConnectorPoint PointX="3.125" PointY="2.325" />
<ConnectorPoint PointX="4.125" PointY="2.325" />
<ConnectorPoint PointX="4.125" PointY="2.625" />
</InheritanceConnector>
</Diagram>
</edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
Whats is the issue ?
Put the DefaultValue attribute on the SSDL Status property. It would look like this (I used the empty string):
<Property Name="Status" Type="nvarchar(max)" Nullable="false" DefaultValue=""/>
Since the base entity is abstract you won't be able to create entities of this type so this DefaultValue will not really be used but it should make EF stop complaining.
In your storage model, the Companies.Status column doesn't allow null values.
Since all entities that inherit from Company except the Lead entity will not have the Status property set, you need to either allow null in the Companies.Status column or set a default value to use for the other entities.

Entity framework stored procedure mapping results not unique entities

I have a problem where I've mapped a stored procedure named sp_getMyEntity, which takes in one parameter called Id and then maps the results to a custom entity called MyEntity
I'm using mock data to illustrate the point. When I run the stored procedure with an id of 1, I get two distinct results back from the database:
exec [dbo].[sp_getMyEntity] #Id=1
Results:
ID - AddressId
1 - 6
1 - 3
When querying the ObjectContext using LINQ, I get 2 MyEntity's back but the AddressId for both of them is 6, not 6 and 3 respectively. Here is my call using LINQ:
Entities context = new Entities();
var entities = from s in context.GetMyEntity(1)
select s;
return entities.ToList();
Here is the EDMX xml:
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="MyProjectModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="MyProjectModelStoreContainer">
<EntitySet Name="MyEntitySet" EntityType="MyProjectModel.Store.MyEntity" />
</EntityContainer>
<Function Name="sp_getMyEntity" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="Id" Type="int" Mode="In" />
</Function>
<EntityType Name="MyEntity">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" Nullable="false" />
<Property Name="AddressId" Type="int" Nullable="true" />
</EntityType>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="MyProjectModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="MyProjectViewEntities" >
<EntitySet Name="MyEntitySet" EntityType="MyProjectModel.MyEntity" />
<FunctionImport Name="GetMyEntity" EntitySet="MyEntitySet" ReturnType="Collection(MyProjectModel.MyEntity)">
<Parameter Name="Id" Mode="In" Type="Int32" />
</FunctionImport>
</EntityContainer>
<EntityType Name="MyEntity">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Int32" Nullable="false" />
<Property Name="AddressId" Type="Int32" Nullable="true" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
<EntityContainerMapping StorageEntityContainer="MyProjectModelStoreContainer" CdmEntityContainer="MyProjectViewEntities" >
<FunctionImportMapping FunctionImportName="GetMyEntity" FunctionName="MyProjectModel.Store.sp_getMyEntity" />
<EntitySetMapping Name="MyEntitySet">
<EntityTypeMapping TypeName="IsTypeOf(MyProjectModel.MyEntity)">
<MappingFragment StoreEntitySet="MyEntitySet">
<ScalarProperty Name="AddressId" ColumnName="AddressId" />
<ScalarProperty Name="Id" ColumnName="Id" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="MyProjectModel" ZoomLevel="100" >
<EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.25" PointY="0.5" Height="7.8375048828125" />
<EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.5" PointY="1.375" Height="1.2636116536458335" /></Diagram></edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
Any ideas why the MyEntity's are not unique?
Because the SP column which your SSDL identifies as the key is not a unique column. You cannot have a "key" with duplicate values.
As Craig stated above my Key property was not unique, so I ended up changing the SQL to look at follows:
SELECT
ROW_NUMBER() OVER(ORDER BY Id) AS KeyId
, Id
, AddressId
FROM
MyTable
WHERE
Id = #Id
Then the EDMX xml looks as follows, notice the new key is a bigint and Int64:
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="MyProjectModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="MyProjectModelStoreContainer">
<EntitySet Name="MyEntitySet" EntityType="MyProjectModel.Store.MyEntity" />
</EntityContainer>
<Function Name="sp_getMyEntity" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
<Parameter Name="Id" Type="int" Mode="In" />
</Function>
<EntityType Name="MyEntity">
<Key>
<PropertyRef Name="KeyId" />
</Key>
<Property Name="KeyId" Type="bigint" Nullable="false" />
<Property Name="Id" Type="int" Nullable="false" />
<Property Name="AddressId" Type="int" Nullable="true" />
</EntityType>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="MyProjectModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="MyProjectViewEntities" >
<EntitySet Name="MyEntitySet" EntityType="MyProjectModel.MyEntity" />
<FunctionImport Name="GetMyEntity" EntitySet="MyEntitySet" ReturnType="Collection(MyProjectModel.MyEntity)">
<Parameter Name="Id" Mode="In" Type="Int32" />
</FunctionImport>
</EntityContainer>
<EntityType Name="MyEntity">
<Key>
<PropertyRef Name="KeyId" />
</Key>
<Property Name="KeyId" Type="Int64" Nullable="false" />
<Property Name="Id" Type="Int32" Nullable="false" />
<Property Name="AddressId" Type="Int32" Nullable="true" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
<EntityContainerMapping StorageEntityContainer="MyProjectModelStoreContainer" CdmEntityContainer="MyProjectViewEntities" >
<FunctionImportMapping FunctionImportName="GetMyEntity" FunctionName="MyProjectModel.Store.sp_getMyEntity" />
<EntitySetMapping Name="MyEntitySet">
<EntityTypeMapping TypeName="IsTypeOf(MyProjectModel.MyEntity)">
<MappingFragment StoreEntitySet="MyEntitySet">
<ScalarProperty Name="AddressId" ColumnName="AddressId" />
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="KeyId" ColumnName="KeyId" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="MyProjectModel" ZoomLevel="100" >
<EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.25" PointY="0.5" Height="7.8375048828125" />
<EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.5" PointY="1.375" Height="1.2636116536458335" /></Diagram></edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>