Greetings,
ADF BC (Business Components) allows you to write code declaratively instead of writing spaghetti code in Java.
One of the feature is Alternate key constraint which is different from Primary and Unique key constraints available in database. Usually Primary key values are coming from database sequence so you don't have to worry about their uniqueness but if you like to validate uniqueness on other columns e.g. email address,Region or Country Name that's where Alternate key comes into action.
Following are some properties of alternate keys:
Lets look into example (Download the complete example)
Use case:
Database schema: HR @ Oracle XE
Column Country name must be unique in Countries table. We have primary key CountryId in the table as shown in the slide
Solution:
Creating Alternate Key:
Right click the Entity Object and Choose "New Entity Constraint"
Enter the name of the constraint, Enter the Attribute where you want to add constraint in our case select Country Name
You can verify the Alternate key constraint Under general tab of entity object properties
Creating Unique Validation:
Go to Business Rules tab in Entity Object properties and Click Create New Validation by selecting "Entity Validators" as shown in the slide
Select "Unique Key" from Rule Type list. Select the Alternate key constraint from Keys
Go to Failure tab and Enter custom error message
Run the Application Module, Create new Record and Enter Unique country name like Brazil, You would see the similar error message.
Also on JSF page you would see error message on Commit
Happy JDeveloping,
Zeeshan Baig
ADF BC (Business Components) allows you to write code declaratively instead of writing spaghetti code in Java.
One of the feature is Alternate key constraint which is different from Primary and Unique key constraints available in database. Usually Primary key values are coming from database sequence so you don't have to worry about their uniqueness but if you like to validate uniqueness on other columns e.g. email address,Region or Country Name that's where Alternate key comes into action.
Following are some properties of alternate keys:
- You can check Uniqueness at record level e.g. Department Name must be unique
- You can have many alternate keys as compare to Primary keys
- You can look for a row in entity object using findByKey() method
- If the key is found in entity objects ADF will throw TooManyObjectsException
Lets look into example (Download the complete example)
Use case:
Database schema: HR @ Oracle XE
Column Country name must be unique in Countries table. We have primary key CountryId in the table as shown in the slide
Solution:
- Create an Alternate key constraint on Entity object.
- Add Unique Key validation on Entity Object using alternate Key
Creating Alternate Key:
Right click the Entity Object and Choose "New Entity Constraint"
Enter the name of the constraint, Enter the Attribute where you want to add constraint in our case select Country Name
You can verify the Alternate key constraint Under general tab of entity object properties
Creating Unique Validation:
Go to Business Rules tab in Entity Object properties and Click Create New Validation by selecting "Entity Validators" as shown in the slide
Select "Unique Key" from Rule Type list. Select the Alternate key constraint from Keys
Go to Failure tab and Enter custom error message
Run the Application Module, Create new Record and Enter Unique country name like Brazil, You would see the similar error message.
Also on JSF page you would see error message on Commit
Happy JDeveloping,
Zeeshan Baig
LinkedIn: http://www.linkedin.com/in/baigzeeshan