Posts

Drools spring hibernate load rules from database

Drools is a business rule management system (rules engine). there are multiple ways to define and process rules. 1. define rules in drl file and this .drl file can be saved in resources. 2. define template in .drt file (in code resources) and load rules from excel file. 3. define template in .drt file and load rules from database. option 3 is little tricky in hibernate where it takes input only in java.sql.ResultSet format. Implementation for option 3 is as below. sample drt file template header id rule_type agenda_group activation_group salience when_logic then_logic package com.sample; import java.util.Arrays; import java.util.ArrayList; import java.lang.Long; template "sample_rule" rule "sample_@{id}_@{rule_type}" agenda-group "@{agenda_group}" activation-group "@{activation_group}" salience @{salience} when @{when_logic} then @{then_logic} end end template assume you have columns in database table (eg- rule) id,rule_typ

Custom auditing with spring and hibernate on specific field

There are multiple ways to audit table fields with hibernate envers @Audited annotation. If this is used at entity level it will store audit of all fields like below(Example 1) @Audited public class SampleClass { @Id private Long id; @Column(name = "field_name_1") private String fieldName1; . . . } Example 1 If @Audited is used at field level, it will store audit of that specific field. In below example(Example 2) it will store audit of only fieldName2 field. public class SampleClass { @Id private Long id; @Column(name = "field_name_1") private String fieldName1; @Audited @Column(name = "field_name_2") private String fieldName2; . } Example 2 But in above two example, it will store the audit of all(Example 1) or few fields(Example 2) on change of ANY field. in example 2 if fieldName1 is changed then also it will create row in audit table with recent value of fieldName2.