Auditing #OneToMany parent child relation with composite key and owner on single side not working - hibernate-envers

I have a parent child #OneToMany relation with the one side as owner. The relation is bidirectional.
The reference table ParentChild has a composite key (parentId, childId) and has an additional attribute name. All changes should be audited by envers, therefore we have chosen the owner side on the parent side that adding and removing a child creates a revision of the parent.
But generating the DDL throws the following exception:
rg.hibernate.MappingException: Repeated column in mapping for entity: com.comfone.audit.test.audittest.data.ParentChild_AUD column: parent_id (should be mapped with insert="false" update="false")
Envers tries to generate the ParentChild_AUD table containing the composite key and the audited fields but it also tries to add two columns parent_id and child_id which conflicts with the composite key.
Is this a bug or do I something wrong?
Thanks for your help!
Cheers Andy
Generated DDL:
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping auto-import="false">
<class entity-name="com.comfone.audit.test.audittest.data.ParentChild_AUD" discriminator-value="ParentChild" table="parent_child_AUD" abstract="false">
<composite-id name="originalId">
<key-property name="childId" type="long">
<column name="child_id" length="255" scale="2" precision="19"/>
</key-property>
<key-property name="parentId" type="long">
<column name="parent_id" length="255" scale="2" precision="19"/>
</key-property>
<key-many-to-one type="integer" class="org.hibernate.envers.DefaultRevisionEntity" name="REV">
<column name="REV"/>
</key-many-to-one>
</composite-id>
<property insert="true" update="false" name="REVTYPE" type="org.hibernate.envers.internal.entities.RevisionTypeType"/>
<property insert="true" update="false" name="name" type="string">
<column name="name" length="255" scale="2" precision="19"/>
</property>
<property insert="false" update="false" name="child_id" type="long">
<column name="child_id" length="255" scale="2" precision="19"/>
</property>
<property insert="true" update="false" name="parent_id" type="long">
<column name="parent_id" length="255" scale="2" precision="19"/>
</property>
</class>
</hibernate-mapping>
Parent.java:
Entity
#Audited
#Builder
#NoArgsConstructor
#AllArgsConstructor
#Getter
#Setter
#EqualsAndHashCode(of = {"id"})
public class Parent implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
#AuditMappedBy(mappedBy = "parent")
#Builder.Default
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "PARENT_ID", insertable = false, updatable = false)
private Set<ParentChild> parentChildren = new HashSet<>();
#Version
#Setter(AccessLevel.NONE)
private Long version;
public void addParentChild(ParentChild parentChild) {
parentChild.setParent(this);
this.parentChildren.add(parentChild);
}
public void removeParentChild(ParentChild parentChild) {
parentChild.setParent(null);
this.parentChildren.remove(parentChild);
}
}
ParentChild.java:
#Entity
#Audited
#Builder
#NoArgsConstructor
#AllArgsConstructor
#Getter
#Setter
#EqualsAndHashCode(of = {"parentChildId"})
public class ParentChild implements Serializable {
#EmbeddedId
private final ParentChildId parentChildId = new ParentChildId();
private String name;
#ManyToOne(optional = false, fetch = FetchType.LAZY)
#MapsId("parentId")
#JoinColumn(insertable = false, updatable = false)
private Parent parent;
#ManyToOne(optional = false, fetch = FetchType.LAZY)
#MapsId("childId")
#JoinColumn(insertable = false, updatable = false)
private Child child;
#Version
#Setter(AccessLevel.NONE)
private Long version;
}
Child.java:
#Entity
#Audited
#Builder
#NoArgsConstructor
#AllArgsConstructor
#Getter
#Setter
#EqualsAndHashCode(of = {"id"})
public class Child {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
Test project:
https://github.com/hotkeeper/jpa-envers-test
It should not generate the two additional columns:
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping auto-import="false">
<class entity-name="com.comfone.audit.test.audittest.data.ParentChild_AUD" discriminator-value="ParentChild" table="parent_child_AUD" abstract="false">
<composite-id name="originalId">
<key-property name="childId" type="long">
<column name="child_id" length="255" scale="2" precision="19"/>
</key-property>
<key-property name="parentId" type="long">
<column name="parent_id" length="255" scale="2" precision="19"/>
</key-property>
<key-many-to-one type="integer" class="org.hibernate.envers.DefaultRevisionEntity" name="REV">
<column name="REV"/>
</key-many-to-one>
</composite-id>
<property insert="true" update="false" name="REVTYPE" type="org.hibernate.envers.internal.entities.RevisionTypeType"/>
<property insert="true" update="false" name="name" type="string">
<column name="name" length="255" scale="2" precision="19"/>
</property>
</class>
</hibernate-mapping>

Related

Olingo V2 - Referential Constraint check on entities' properties

I am using Olingo 2.0.11 on top of Hibernate. I've built relationship between entities and the navigation property works fine by define the #OneToOne annotation to entities' properties:
public class MetaClass {
#OneToOne(mappedBy = "metaClassEntity", cascade = {CascadeType.MERGE, CascadeType.REFRESH}, optional = false)
private MetaClassReport metaClassReport;
}
public class MetaClassReport {
#OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = MetaClass.class)
#JoinColumns({
#JoinColumn(name = "meta_class", referencedColumnName = "id", insertable = false, updatable = false),
#JoinColumn(name = "tenant_id", referencedColumnName = "tenant_id", insertable = false, updatable = false)})
private MetaClass metaClassEntity;
}
and the Edm looks like (notice the navigation property):
<EntityType Name="MetaClassReport">
<Key>
<PropertyRef Name="Id"></PropertyRef>
<PropertyRef Name="TenantId"></PropertyRef>
</Key>
<Property Name="AppCode" Type="Edm.String" Nullable="true" MaxLength="50"></Property>
<Property Name="CreateTime" Type="Edm.DateTime" Nullable="false"></Property>
<Property Name="Creator" Type="Edm.String" Nullable="false" MaxLength="36"></Property>
<Property Name="DataRelation" Type="Edm.String" Nullable="true" MaxLength="20"></Property>
<Property Name="Id" Type="Edm.String" Nullable="false" MaxLength="22"></Property>
<Property Name="JoinGeneral" Type="Edm.String" Nullable="true" MaxLength="255"></Property>
<Property Name="Links" Type="Edm.String" Nullable="true" MaxLength="255"></Property>
<Property Name="MainEntity" Type="Edm.String" Nullable="true" MaxLength="22"></Property>
<Property Name="MetaClass" Type="Edm.String" Nullable="false" MaxLength="22"></Property>
<Property Name="MicroServiceCode" Type="Edm.String" Nullable="true" MaxLength="50"></Property>
<Property Name="Modifier" Type="Edm.String" Nullable="true" MaxLength="36"></Property>
<Property Name="ModifyTime" Type="Edm.DateTime" Nullable="true"></Property>
<Property Name="QuerySchema" Type="Edm.String" Nullable="true" MaxLength="22"></Property>
<Property Name="Version" Type="Edm.Int32" Nullable="false"></Property>
<Property Name="TenantId" Type="Edm.String" Nullable="false" MaxLength="36"></Property>
<NavigationProperty Name="MetaClassDetails" Relationship="default.MetaClass_MetaClassReport_One_One0" FromRole="MetaClassReport" ToRole="MetaClass"></NavigationProperty>
</EntityType>
<Association Name="MetaClass_MetaClassReport_One_One0">
<End Type="default.MetaClass" Multiplicity="1" Role="MetaClass"></End>
<End Type="default.MetaClassReport" Multiplicity="0..1" Role="MetaClassReport"></End>
<ReferentialConstraint>
<Principal Role="MetaClass">
<PropertyRef Name="Id"></PropertyRef>
<PropertyRef Name="TenantId"></PropertyRef>
</Principal>
<Dependent Role="MetaClassReport">
<PropertyRef Name="MetaClass"></PropertyRef>
<PropertyRef Name="TenantId"></PropertyRef>
</Dependent>
</ReferentialConstraint>
</Association>
Now I have the requirement to do the referential check when update MetaClassReport associated to a MetaClass by:
localhost:8080/odata/MetaClassReports
and the body is:
{
"Id": "7s",
"YtenantId": "1qaz2wsx",
"Links": "",
"Version": 1,
"CreateTime": "2022-12-31T18:59:59",
"Creator": "chenhem",
"MetaClassDetails": {
"Id": "5s",
"YtenantId": "1qaz2wsx",
"LegacyId": "12121212",
"TableName": "classTable",
"TableType": 0,
"Uri": "aa.bb.testmetaclass",
"Name": "testmetaclass",
"MetaComponentId": "11111111111",
"Version": 1,
"CreateTime": "2022-12-31T18:59:59",
"Creator": "chenhem"
}
}
then I notice that Olingo would not check the reference constraint between MetaClass and MetaClass report, the MetaClass will be inserted into DB no matter the "MetaClass":"1s" exists in MetaClass table or not.
I am wondering if the <ReferentialConstraint> in Edm is just an illustration, or indeed it will do some effects just because I missed some configuration or implementation?
If the <ReferentialConstraint> is just a metadata illustration, then how should I implement such a key reference value check? (DB foreign key is not an option for me), thanks in advance.

Error when using sequence for primary key in postgres and Hibernate

I'm using JPA/Hibernate and postgres as my DB
I created a sequence in postgres like this:
CREATE SEQUENCE player_sequence
INCREMENT 20
START 1;
I want Hibernate to use the above sequence for primary key.
The id part of the entity is:
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "player_seq")
#SequenceGenerator(name = "player_seq", sequenceName = "player_sequence", allocationSize = 20)
private Integer id;
When I create a new player and try to save it via EntityManger.persist method I get the following error:
Hibernate: select next_val as id_val from player_sequence for update
org.hibernate.id.enhanced.TableStructure$1$1 execute
ERROR: could not read a hi value
org.postgresql.util.PSQLException: ERROR: column "next_val" does not exist
I don't understand what I do wrong
EDIT:
Here is content of persistence.xml:
Try to correct your persistence.xml in this way:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
version="2.2">
<persistence-unit name="my-persistence-unit">
<description>...</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<!-- It was wrong !!! -->
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL10Dialect" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/basketball" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="1234" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>

There is no statement named UserEntity.insertUser in this SqlMap

I am trying to start with mybatis. I did all the configurations but when i want to test the connection to the database i have that error There is no statement named UserEntity.insertUser in this SqlMap.
confif file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- changes from the defaults -->
<setting name="lazyLoadingEnabled" value="false" />
</settings>
<typeAliases>
<typeAlias alias="User" type="com.hosto.Entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hospidigest"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/hosto/mapper/UserMapper.xml"/>
</mappers>
</configuration>
sql mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<sqlMap namespace="com.hosto.dao.UserDao">
<resultMap id="UserResult" class="User">
<result property="lastName" column="LASTNAME"/>
<result property="firstName" column="FIRSTNAME"/>
<result property="password" column="PASSWORD"/>
<result property="email" column="EMAIL"/>
<result property="month" column="MONTH"/>
<result property="day" column="DAY"/>
<result property="year" column="YEAR"/>
<result property="prefixes" column="PREFIXE"/>
<result property="gender" column="GENDER"/>
<result property="personal" column="BASICPERSONNALINFORMATION"/>
</resultMap>
<select id="getAllUsers" resultClass="java.util.ArrayList">
SELECT * FROM user
</select>
<insert id="insertUser" parameterType="com.hosto.Entity.User">
INSERT INTO user (LASTNAME,FIRSTNAME,USERNAME,PASSWORD,EMAIL,MONTH,DAY,YEAR,PREFIXE,GENDER,BASICPERSONNALINFORMATION)
VALUES (#{lastName},#{firstName},#{userName}, #{password}, #{email}, #{month}, #{day}, #{year}, #{prefixes}, #{gender},#{personal})
</insert>
<update id="updateUser" parameterClass="UserDaotest">
UPDATE user SET
LASTNAME= #{lastName},
FIRSTNAME= #{firstName},
USERNAME= #{userName},
PASSWORD= #{password},
EMAIL= #{email},
MONTH= #{month},
DAY= #{day},
YEAR= #{year},
PREFIXE= #{prefixes},
GENDER= #{gender},
BASICPERSONNALINFORMATION= #{personal},
WHERE ID = #{id}
</update>
<delete id="deleteUser" parameterClass="int">
DELETE FROM user where
ID = #{id}
</delete>
<select id="getUserById" resultClass="UserInterface" parameterClass="int">
SELECT *
FROM user
WHERE ID= #{id}
</select>
</sqlMap>
Mybatis Factory
package com.hosto.daoconfig;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class MyBatisConnectionFactory {
private static SqlSessionFactory sqlSessionFactory;
private static SqlMapClient sqlMapClient = null;
public static SqlMapClient getSqlMapClient() {
try {
String resource = "SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
}
catch (IOException iOException) {
iOException.printStackTrace();
}
return sqlMapClient;
}
}
User Dao
package com.hosto.dao;
import java.util.List;
import org.springframework.dao.DataAccessException;
import com.hosto.Entity.User;
/**
* #author Dorian
*
*/
public interface UserDao {
public void insertUser(User user) throws DataAccessException;
public User getUserById(Integer id) throws DataAccessException;
public List<User> getAllUsers()throws DataAccessException;
public void updateUser(User user)throws DataAccessException;
public void deleteUser(Integer userId)throws DataAccessException;
}
It is telling you that it is not defined, you need to create one.
see this for an example here and here

OpenJPA orphan removal not working

I am trying out a master child test case with a OneToMany unidirectional relationship.
I have a scenario where I want to update the master by removing some entries from the children list. When I do a merge, I want these child entries to be automatically deleted.
I have tried in vain to get this working. I tried orphanremoval too, but that too didn't help. There were some suggestions to add equals and hashcode, which didn't work either.
Any hint or help in this regard is highly appreciated. Thank you.
I am using JPA2.0 with OpenJPA 2.1.0
Here is my code.
//Parent class
public class Account {
private String accountId;
private String accountNumber;
private List<SubAccount> subAccounts;
//followed by getters/setters
}
//Child class
public class SubAccount {
private String subAccountId;
private String subAccountNumber;
//followed by getters/setters
}
My orm.xml is as follows.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<entity-mappings version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
<entity name="Account" class="test.data.Account">
<table name="ACCOUNT" />
<sequence-generator name="AccountSeq"
sequence-name="ACCOUNT_TEST_SEQ" />
<attributes>
<id name="accountId">
<column name="ACCOUNT_ID" />
<generated-value strategy="SEQUENCE" generator="AccountSeq" />
</id>
<basic name="accountNumber">
<column name="ACCOUNT_NO" />
</basic>
<one-to-many name="subAccounts" fetch="EAGER"
target-entity="test.data.SubAccount" orphan-removal="true" >
<join-column name="ACCOUNT_ID" referenced-column-name="ACCOUNT_ID" nullable="false" updatable="true"/>
<cascade>
<cascade-all />
</cascade>
</one-to-many>
</attributes>
</entity>
<entity name="SubAccount" class="test.data.SubAccount">
<table name="SUBACCOUNT" />
<sequence-generator name="SubAccountSeq"
sequence-name="SUBACCOUNT_TEST_SEQ" />
<attributes>
<id name="subAccountPK">
<column name="SUBACCOUNT_ID" />
<generated-value strategy="SEQUENCE" generator="AccountSeq" />
</id>
<basic name="subAccountNumber">
<column name="SUBACCOUNT_NO" />
</basic>
</attributes>
</entity>
</entity-mappings>
And this is what I am trying to do.
Account acc = (Account)entityMgr.find(Account.class, "1100")
acc.getSubAccounts().remove(0);
entityMgr.merge(acc)
I expect the subaccount to be deleted.
OpenJPA overrides the orphan-removal behavior if the cascade type is set to cascade-all or cascade-remove.
Instead of cascade-all, I had to configure cascade-type with cascade-persist, cascade-merge, cascade-refresh and cascade-detach.

The entity type <class> is not part of the model for the current context

This is a MODEL first approach. I have already researched this extensiely and have not come up with an answer. I have tried all the suggestions at the following links:
This appears to be the same problem but with no resolution
The entity type <classname> is not part of the model for the current context
These are the links I have already researched so please don't answer as duplicate of ----
EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context
The entity type is not part of the model, EF 5
Here is the EDMX (Abbreviated for brevity):
<?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="Insight_Model.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/2009/02/edm/ssdl">
<EntityContainer Name="Insight_ModelStoreContainer">
<EntitySet Name="vwCompanyHeader" EntityType="Insight_Model.Store.vwCompanyHeader" store:Type="Views" store:Schema="dbo" store:Name="vwCompanyHeader">
<DefiningQuery>SELECT
[vwCompanyHeader].[ID] AS [ID],
[vwCompanyHeader].[Name] AS [Name],
[vwCompanyHeader].[Active] AS [Active],
[vwCompanyHeader].[Creator] AS [Creator],
[vwCompanyHeader].[Created] AS [Created],
[vwCompanyHeader].[Modifier] AS [Modifier],
[vwCompanyHeader].[Modified] AS [Modified]
FROM [dbo].[vwCompanyHeader] AS [vwCompanyHeader]</DefiningQuery>
</EntitySet>
<EntitySet Name="vwWorkOrderHeader" EntityType="Insight_Model.Store.vwWorkOrderHeader" store:Type="Views" store:Schema="dbo" store:Name="vwWorkOrderHeader">
<DefiningQuery>SELECT
[vwWorkOrderHeader].[ID] AS [ID],
[vwWorkOrderHeader].[Active] AS [Active],
[vwWorkOrderHeader].[Name] AS [Name],
[vwWorkOrderHeader].[Creator] AS [Creator],
[vwWorkOrderHeader].[Created] AS [Created],
[vwWorkOrderHeader].[Modifier] AS [Modifier],
[vwWorkOrderHeader].[Modified] AS [Modified],
[vwWorkOrderHeader].[Leader] AS [Leader],
[vwWorkOrderHeader].[LeadTakenDate] AS [LeadTakenDate],
[vwWorkOrderHeader].[ProjectID] AS [ProjectID],
[vwWorkOrderHeader].[ProjectName] AS [ProjectName],
[vwWorkOrderHeader].[WorkOrderStatus] AS [WorkOrderStatus],
[vwWorkOrderHeader].[WorkOrderSubStatus] AS [WorkOrderSubStatus],
[vwWorkOrderHeader].[WorkOrderType] AS [WorkOrderType],
[vwWorkOrderHeader].[WorkOrderSubType] AS [WorkOrderSubType],
[vwWorkOrderHeader].[WorkOrderPriority] AS [WorkOrderPriority],
[vwWorkOrderHeader].[RefNumber] AS [RefNumber],
[vwWorkOrderHeader].[Request] AS [Request]
FROM [dbo].[vwWorkOrderHeader] AS [vwWorkOrderHeader]</DefiningQuery>
</EntitySet>
</EntityContainer>
<EntityType Name="vwCompanyHeader">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="int" Nullable="false" />
<Property Name="Name" Type="varchar" Nullable="false" MaxLength="50" />
<Property Name="Active" Type="bit" Nullable="false" />
<Property Name="Creator" Type="varchar" Nullable="false" MaxLength="51" />
<Property Name="Created" Type="date" Nullable="false" />
<Property Name="Modifier" Type="varchar" Nullable="false" MaxLength="51" />
<Property Name="Modified" Type="date" Nullable="false" />
</EntityType>
<EntityType Name="vwWorkOrderHeader">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="int" Nullable="false" />
<Property Name="Active" Type="bit" Nullable="false" />
<Property Name="Name" Type="varchar" Nullable="false" MaxLength="15" />
<Property Name="Creator" Type="varchar" Nullable="false" MaxLength="51" />
<Property Name="Created" Type="date" Nullable="false" />
<Property Name="Modifier" Type="varchar" Nullable="false" MaxLength="51" />
<Property Name="Modified" Type="date" Nullable="false" />
<Property Name="Leader" Type="varchar" Nullable="false" MaxLength="51" />
<Property Name="LeadTakenDate" Type="date" Nullable="false" />
<Property Name="ProjectID" Type="int" Nullable="false" />
<Property Name="ProjectName" Type="varchar" Nullable="false" MaxLength="15" />
<Property Name="WorkOrderStatus" Type="varchar" Nullable="false" MaxLength="35" />
<Property Name="WorkOrderSubStatus" Type="varchar" Nullable="false" MaxLength="35" />
<Property Name="WorkOrderType" Type="varchar" Nullable="false" MaxLength="35" />
<Property Name="WorkOrderSubType" Type="varchar" Nullable="false" MaxLength="35" />
<Property Name="WorkOrderPriority" Type="varchar" Nullable="false" MaxLength="35" />
<Property Name="RefNumber" Type="varchar" Nullable="false" MaxLength="25" />
<Property Name="Request" Type="varchar" Nullable="false" MaxLength="1500" />
</EntityType>
</Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="Insight_Model" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="Insight_Entities" annotation:LazyLoadingEnabled="false">
<EntitySet Name="CompanyHeaders" EntityType="Insight_Model.CompanyHeader" />
<EntitySet Name="WorkOrderHeaders" EntityType="Insight_Model.WorkOrderHeader" />
</EntityContainer>
<EntityType Name="CompanyHeader">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Type="String" Name="Name" MaxLength="50" FixedLength="false" Unicode="false" Nullable="false" />
<Property Type="Boolean" Name="Active" Nullable="false" />
<Property Type="String" Name="Creator" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="Created" Nullable="false" />
<Property Type="String" Name="Modifier" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="Modified" Nullable="false" />
<Property Type="Int32" Name="ID" Nullable="false" />
</EntityType>
<EntityType Name="WorkOrderHeader">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Type="Int32" Name="ID" Nullable="false" />
<Property Type="Boolean" Name="Active" Nullable="false" />
<Property Type="String" Name="Name" Nullable="false" MaxLength="15" FixedLength="false" Unicode="false" />
<Property Type="String" Name="Creator" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="Created" Nullable="false" />
<Property Type="String" Name="Modifier" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="Modified" Nullable="false" />
<Property Type="String" Name="Leader" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="LeadTakenDate" Nullable="false" />
<Property Type="Int32" Name="ProjectID" Nullable="false" />
<Property Type="String" Name="ProjectName" Nullable="false" MaxLength="15" FixedLength="false" Unicode="false" />
<Property Type="String" Name="WorkOrderStatus" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
<Property Type="String" Name="WorkOrderSubStatus" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
<Property Type="String" Name="WorkOrderType" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
<Property Type="String" Name="WorkOrderSubType" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
<Property Type="String" Name="WorkOrderPriority" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
<Property Type="String" Name="RefNumber" MaxLength="25" Nullable="false" FixedLength="false" Unicode="false" />
<Property Type="String" Name="Request" Nullable="false" MaxLength="1500" FixedLength="false" Unicode="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="Insight_ModelStoreContainer" CdmEntityContainer="Insight_Entities">
<EntitySetMapping Name="CompanyHeaders">
<EntityTypeMapping TypeName="Insight_Model.CompanyHeader">
<MappingFragment StoreEntitySet="vwCompanyHeader">
<ScalarProperty Name="ID" ColumnName="ID" />
<ScalarProperty Name="Modified" ColumnName="Modified" />
<ScalarProperty Name="Modifier" ColumnName="Modifier" />
<ScalarProperty Name="Created" ColumnName="Created" />
<ScalarProperty Name="Creator" ColumnName="Creator" />
<ScalarProperty Name="Active" ColumnName="Active" />
<ScalarProperty Name="Name" ColumnName="Name" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<EntitySetMapping Name="WorkOrderHeaders">
<EntityTypeMapping TypeName="Insight_Model.WorkOrderHeader">
<MappingFragment StoreEntitySet="vwWorkOrderHeader">
<ScalarProperty Name="Request" ColumnName="Request" />
<ScalarProperty Name="RefNumber" ColumnName="RefNumber" />
<ScalarProperty Name="WorkOrderPriority" ColumnName="WorkOrderPriority" />
<ScalarProperty Name="WorkOrderSubType" ColumnName="WorkOrderSubType" />
<ScalarProperty Name="WorkOrderType" ColumnName="WorkOrderType" />
<ScalarProperty Name="WorkOrderSubStatus" ColumnName="WorkOrderSubStatus" />
<ScalarProperty Name="WorkOrderStatus" ColumnName="WorkOrderStatus" />
<ScalarProperty Name="ProjectName" ColumnName="ProjectName" />
<ScalarProperty Name="ProjectID" ColumnName="ProjectID" />
<ScalarProperty Name="LeadTakenDate" ColumnName="LeadTakenDate" />
<ScalarProperty Name="Leader" ColumnName="Leader" />
<ScalarProperty Name="Modified" ColumnName="Modified" />
<ScalarProperty Name="Modifier" ColumnName="Modifier" />
<ScalarProperty Name="Created" ColumnName="Created" />
<ScalarProperty Name="Creator" ColumnName="Creator" />
<ScalarProperty Name="Name" ColumnName="Name" />
<ScalarProperty Name="Active" ColumnName="Active" />
<ScalarProperty Name="ID" ColumnName="ID" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
The repository code:
Imports System.Linq.Expressions
Imports System.Data.Entity.Infrastructure
Imports System.Data.Entity
Public Class Repository(Of T As {Class, IEntity})
Implements IRepository(Of T)
#Region "Attributes"
Private objectContext As IDatabaseContext
Protected objectSet As IDbSet(Of T)
#End Region
Public Sub New(objContext As IDatabaseContext)
objectContext = objContext
objectSet = objectContext.Set(Of T)()
End Sub
Public Function FindAll() As IQueryable(Of T) Implements IRepository(Of T).FindAll
Return objectSet
End Function
Public Function FindWhere(predicate As Expressions.Expression(Of System.Func(Of T, Boolean))) As System.Linq.IQueryable(Of T) Implements Interfaces.IRepository(Of T).FindWhere
Return objectSet.Where(predicate)
End Function
Public Function FindById(id As Integer) As T Implements IRepository(Of T).FindById
Return objectSet.Single(Function(o) o.ID = id)
End Function
Public Sub Add(newEntity As T) Implements IRepository(Of T).Add
objectSet.Add(newEntity)
End Sub
Public Sub Remove(entity As T) Implements IRepository(Of T).Remove
objectSet.Remove(entity)
End Sub
Public Function Count(Optional predicate As Expression(Of Func(Of T, Boolean)) = Nothing) As Integer Implements IRepository(Of T).Count
Return If((predicate Is Nothing), objectSet.Count, objectSet.Where(predicate).Count)
End Function
Public Function Exist(Optional predicate As Expression(Of Func(Of T, Boolean)) = Nothing) As Boolean Implements IRepository(Of T).Exist
Return If((predicate Is Nothing), objectSet.Any, objectSet.Where(predicate).Any)
End Function
End Class
The DatabaseContext code:
Imports System.Data.Entity
Imports Insight.DataLayer.Interfaces
Imports System.Data.Objects
Imports System.Configuration
Public Class DatabaseContext
Inherits DbContext
Implements IDatabaseContext
Const defaultConnectStringName = "Insight_Entities"
Public Sub New()
Me.New(ConfigurationManager.ConnectionStrings(defaultConnectStringName).ConnectionString)
End Sub
Public Sub New(connectionString As String)
MyBase.New(connectionString)
MyBase.Configuration.LazyLoadingEnabled = False
MyBase.Configuration.ProxyCreationEnabled = False
End Sub
Public Overloads Sub SaveChanges() Implements Interfaces.IDatabaseContext.SaveChanges
MyBase.SaveChanges()
End Sub
Public Overloads Function [Set](Of T As Class)() As System.Data.Entity.IDbSet(Of T) Implements Interfaces.IDatabaseContext.Set
Return MyBase.Set(Of T)()
End Function
End Class
And the 2 class files:
WorkOrderHeader:
Imports System.ComponentModel.DataAnnotations
Imports System.Collections.ObjectModel
Imports System.Runtime.Serialization
Imports System.Data.Services.Common
Partial Public Class WorkOrderHeader
Inherits DataTransferObjectHeaderBase
Public Sub New()
End Sub
Public Sub New(iID As Integer, sName As String, bActive As Boolean, sCreator As String, dtCreated As DateTime, sModifier As String, dtModified As DateTime, sLeader As String, dtLeadTaken As Date, iProjectID As Integer, sProjectName As String, sWorkOrderStatus As String, sWorkOrderSubStatus As String, sWorkOrderType As String, sWorkOrderSubType As String, sWorkOrderPriority As String, sRequest As String)
Me.New(iID, sName, bActive, sCreator, dtCreated, sModifier, dtModified, sLeader, dtLeadTaken, iProjectID, sProjectName, sWorkOrderStatus, sWorkOrderSubStatus, sWorkOrderType, sWorkOrderSubType, sWorkOrderPriority, "", sRequest)
End Sub
Public Sub New(iID As Integer, sName As String, bActive As Boolean, sCreator As String, dtCreated As DateTime, sModifier As String, dtModified As DateTime, sLeader As String, dtLeadTaken As Date, iProjectID As Integer, sProjectName As String, sWorkOrderStatus As String, sWorkOrderSubStatus As String, sWorkOrderType As String, sWorkOrderSubType As String, sWorkOrderPriority As String, sRefNumber As String, sRequest As String)
ID = iID
Name = sName
Active = bActive
Creator = sCreator
Created = dtCreated
Modifier = sModifier
Modified = dtModified
Leader = sLeader
LeadTaken = dtLeadTaken
ProjectID = iProjectID
ProjectName = sProjectName
WorkOrderStatus = sWorkOrderStatus
WorkOrderSubStatus = sWorkOrderSubStatus
WorkOrderType = sWorkOrderType
WorkOrderSubType = sWorkOrderSubType
WorkOrderPriority = sWorkOrderPriority
RefNumber = sRefNumber
Request = sRequest
End Sub
<DataMember()> _
Public Property Leader() As String
Get
Return _leader
End Get
Set(value As String)
_leader = value
End Set
End Property
Private _leader As String
<DataMember()> _
Public Property LeadTaken() As Date
Get
Return _leadTaken
End Get
Set(value As Date)
_leadTaken = value
End Set
End Property
Private _leadTaken As Date
<DataMember()> _
Public Property ProjectID() As Int32
Get
Return _projectID
End Get
Set(value As Int32)
If (_projectID <> value) Then
_projectID = value
End If
End Set
End Property
Private _projectID As Int32
<DataMember()> _
Public Property ProjectName() As String
Get
Return _projectName
End Get
Set(value As String)
If (_projectName <> value) Then
_projectName = value
End If
End Set
End Property
Private _projectName As String
<DataMember()> _
Public Property WorkOrderStatus() As String
Get
Return _workOrderStatus
End Get
Set(value As String)
_workOrderStatus = value
End Set
End Property
Private _workOrderStatus As String
<DataMember()> _
Public Property WorkOrderSubStatus() As String
Get
Return _workOrderSubStatus
End Get
Set(value As String)
_workOrderSubStatus = value
End Set
End Property
Private _workOrderSubStatus As String
<DataMember()> _
Public Property WorkOrderType() As String
Get
Return _workOrderType
End Get
Set(value As String)
_workOrderType = value
End Set
End Property
Private _workOrderType As String
<DataMember()> _
Public Property WorkOrderSubType() As String
Get
Return _workOrderSubType
End Get
Set(value As String)
_workOrderSubType = value
End Set
End Property
Private _workOrderSubType As String
<DataMember()> _
Public Property WorkOrderPriority() As String
Get
Return _workOrderPriority
End Get
Set(value As String)
_workOrderPriority = value
End Set
End Property
Private _workOrderPriority As String
<DataMember()> _
Public Property RefNumber() As String
Get
Return _refNumber
End Get
Set(value As String)
_refNumber = value
End Set
End Property
Private _refNumber As String
<DataMember()> _
Public Property Request() As String
Get
Return _request
End Get
Set(value As String)
_request = value
End Set
End Property
Private _request As String
End Class
#End Region
CompanyHeader:
Imports System.ComponentModel.DataAnnotations
Imports System.Collections.ObjectModel
Imports System.Runtime.Serialization
Imports System.Data.Services.Common
<DataServiceKey("ID")> _
<MetadataTypeAttribute(GetType(CompanyHeader.CompanyHeaderMetadata))> _
Partial Public Class CompanyHeader
Inherits DataTransferObjectHeaderBase
Friend NotInheritable Class CompanyHeaderMetadata
'Metadata classes are not meant to be instantiated.
Private Sub New()
MyBase.New()
End Sub
<DataMember()> _
Public Property Active As Boolean
<DataMember()> _
Public Property ID As Integer
<DataMember()> _
Public Property Created As DateTime
<DataMember()> _
Public Property Creator As String
<DataMember()> _
Public Property Modified As DateTime
<DataMember()> _
Public Property Modifier As String
<DataMember()> _
Public Property Name As String
End Class
Public Sub New()
End Sub
Public Sub New(iID As Integer, sName As String, bActive As Boolean, sCreator As String, dtCreated As DateTime, sModifier As String, dtModified As DateTime)
ID = iID
Name = sName
Active = bActive
Creator = sCreator
Created = dtCreated
Modifier = sModifier
Modified = dtModified
End Sub
End Class
Unit Test Code:
<TestMethod()> _
Public Sub WorkOrderServiceTests_TestConnectivityThruService5()
Dim woService As New DatabaseContext
Dim woHeaders = New Repository(Of WorkOrderHeader)(woService)
Dim woTests = New Repository(Of WorkOrderTest)(woService)
Dim coHeaders = New Repository(Of CompanyHeader)(woService)
Assert.IsTrue(woHeaders.Count <> 0)
End Sub
Things I have tried to resolve the issue:
1) Checked database - Both queries return data correctly.
select * from vwWorkOrderHeader
select * from vwCompanyHeader
2) Deleted WorkOrderHeader from the EDMX then recreated it from database.
3) MOQed out the DBContext to make sure that it isn't the service query doing something funny. This correctly returns the object, so it means that it is something at the context level or below.
4) Stepped through both tests to ensure that they use the same method of building the context - Thy do.
4) Stepped through both tests to ensure that they use the same method of building the repository - Thy do.
5) The only difference is during the following Function of DatabaseContext:
Public Overloads Function [Set](Of T As Class)() As System.Data.Entity.IDbSet(Of T) Implements Interfaces.IDatabaseContext.Set
Return MyBase.Set(Of T)()
End Function
The WorkOrderHeader code has the following in the watch for MyBase.Set(Of T)()
{System.Data.Entity.DbSet(Of Insight.Model.WorkOrderHeader)} System.Data.Entity.DbSet(Of Insight.Model.WorkOrderHeader)
The CompanyHeader code has the following in the watch for MyBase.Set(Of T)()
{SELECT [Extent1].[Name] AS [Name], [Extent1].[Active] AS [Active], [Extent1].[Creator] AS [Creator], [Extent1].[Created] AS [Created], [Extent1].[Modifier] AS [Modifier], [Extent1].[Modified] AS [Modified], [Extent1].[ID] AS [ID] FROM (SELECT [vwCompanyHeader].[ID] AS [ID], [vwCompanyHeader].[Name] AS [Name], [vwCompanyHeader].[Active] AS [Active], [vwCompanyHeader].[Creator] AS [Creator], [vwCompanyHeader].[Created] AS [Created], [vwCompanyHeader].[Modifier] AS [Modifier], [vwCompanyHeader].[Modified] AS [Modified] FROM [dbo].[vwCompanyHeader] AS [vwCompanyHeader]) AS [Extent1]} System.Data.Entity.DbSet(Of Insight.Model.CompanyHeader)
I am at a complete loss on this one and am entering my 3rd day of working on the same problem. I am hoping that some fresh eyes on the problem might see an error that I have made or point me in a direction of testing the problem that I have not thought of.
UPDATE:
I removed the service code to ensure it was something in the DBContext. I have isolated the service out of the equation so that only the repositories are being used and they are all using the same DatabaseContext, so it can't be anything related to the connection string. I verified that in the Model Browser under Insight_Model/EntityContainer:Insight_Entities/Entity Sets/WorkOrderHeaders exists and that under Insight_Model/Entity Types/WorkOrderHeader exists. It almost appears as though the DatabaseContext isn't even attempting to query the conceptual model for the entity set.
After 3 (4) days (depending on how you view days, I separate days by when I sleep and not necessarily when the sun goes down/comes up), I found the answer. In the EDMX file above, I have a column named LeadTakenDate, in the WorkOrderHeader class, I used the field name LeadTaken. This was a stupid error on my part.
How can this benefit you possibly? Here is how I resolved the problem:
First I isolated the problem to exactly one area by MOQing out parts to see how far down the rabbit hole the problem was. Once I figured out the problem was in the DatabaseContext, I wrote a test case that created a working and a non working use case. This isolated the problem and allowed me to test theories to fix it quicker. I tried a thousand different things before trying to use the EF 5.x persistance ignorant poco generator. I commented out all of my code in the WorkOrderHeader class and copied the generated code in. Reran my tests and they worked, so I started uncommenting my properties and commenting out the generated code then rerunning my test until I hit the appropriate property.
The error message is cryptic enough that is can be very difficult to trouble shoot.