Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
Mapping Fields to the Same Columns
Programmer's Guide > OpenAccess ORM Classic (Old API) > Programming With OpenAccess > Mapping > Simple and Reference Fields > Mapping Fields to the Same Columns

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

OpenAccess ORM supports mapping different fields to the same database columns. This makes it easier to map to complex legacy database schemas and can also improve performance.

When a set of fields all share a common column, OpenAccess ORM decides which field to use in insert and update SQL statements when the server starts. Updates to the common column for the other fields are ignored. This is much faster than deferring this decision to commit time and looking at the dirty fields in each instance. The following algorithm is used to select the field with the updateable column from the fields in the set:

1.

 

If one of the fields is a part of the primary key then it is selected

2.

 

Fields in a class are used ahead of fields from its subclasses

3.

 

Simple fields are selected before reference fields (e.g., String countryCode will be chosen ahead of Country country)

4.

 

Reference fields with fewer columns are selected before those with more columns

5.

 

The field names are sorted alphabetically and the first one selected

Normally it is best to update all fields with columns in common when you want the instance to change. That way you are sure that your change will reach the database and the instance is as it will be next time it is loaded from the database.

Using a Reference in the Primary Key

Using a reference in the primary key is not supported in the current release of OpenAccess. This will be supported in future releases.

Using a Foreign Key as a Value

A simple field can be mapped to a foreign key column used by a reference field. This can improve performance when the value of the foreign key column is useful on its own. A good example of this is a reference to a Country class using the ISO country code as its primary key. You may need to display just the country code from the Country instance and want to avoid fetching the Country instance to do this.

The branch and country tables as defined in the datastore:

SQL Copy Code
create table branch (
    branch_no SMALLINT not null,
    nme VARCHAR(30) not null,
    country_code VARCHAR(2),
    constraint pk_branch primary key (branch_no)
);

create table country (
    code VARCHAR(2) not null,
    nme VARCHAR(40) not null,
    constraint pk_country primary key (code)
);

alter table branch add constraint ref_branch_country
    foreign key (country_code)
    references country(code);

Next are the important parts of the Branch and Country classes that are mapped to these tables:

C# Copy Code
[OpenAccess.Persistent]
public class Branch {
   
private string name;
   
private Country country;
   
private string countryCode;
   ...
   
public void SetCountry(Country country) {
       
this.country = country;
       countryCode = country ==
null ? null : country.getCode();
   }

   
public String getCountryCode() {
       
return countryCode;
   }
   ...
}

[OpenAccess.Persistent( Identity = typeof( Country.ID ) )]
public class Country {
   
private string code; // primary key
   
private string name;
   ...
   
public class ID : OpenAccess.IObjectId {
       
public string code;
       ...
   }
}
VB .NET Copy Code
<OpenAccess.Persistent> _
Public Class Branch
 Private name As String
 Private country As Country
 Private countryCode As String
   ... Public Sub SetCountry(ByVal country As Country)
  Me.country = country
  countryCode = If(country Is Nothing, Nothing, country.getCode())
   End Sub
 Public Function getCountryCode() As String
  Return countryCode
 End Function
 Private ...
End Class
<OpenAccess.Persistent(Identity := GetType(Country.ID))> _
Public Class Country
 Private code As String ' primary key
 Private name As String
   ... Public Class ID
    Inherits OpenAccess.IObjectId
  Public code As String
  Private ...
   End Class
End Class

The countryCode field is the same type as the primary key of Country

The setCounty sets the country and countryCode fields. Setting only the countryCode field would work but then the instance would look different before and after commit. If only the country field was set the update would be ignored.

The primary key of Country is code

The objectid-class for Country

The countryCode field in the branch table is mapped to same column as the country reference.

Using a Foreign Key as a Value

Here is the relevant XML metadata for the example:

Copy Code
<class name="Branch">
 
<field name="branchNo">
   
<extension key="db-column">
     
<extension key="db-type" value="SMALLINT" />
   
</extension>
 
</field>
 
<field name="country">
   
<extension key="db-column">
     
<extension  key="db-column-name" value="country_code" />
   
</extension>
 
</field>
 
<field name="countryCode">
   
<extension key="db-column">
     
<extension key="db-column-name" value="country_code" />
     
<extension key="db-length" value="2" />
   
</extension>
 
</field>
</
class>