I am making an application for employee rostering using Optaplanner. Everything worked just fine without persistence. Now, I want to add Hibernate integration. I want to take information from MySQL database and use it as a schedule input.
In database I have Position and Skill tables.
Employees data, TimeSlots and Assignments are now hardcoded in app.
My domain classes, Skill:
import javax.persistence.*;
@Entity
@Table(name = "SKILL")
public class Skill {
private long skillId;
private String name;
public Skill(String name) {
this.name = name;
}
public Skill() {}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "SKILL_ID")
public long getSkillId() {
return this.skillId;
}
public void setSkillId(long skillId) {
this.skillId = skillId;
}
@Column(name = "SKILL_NAME", nullable = false, length=250)
public String getSkillName() {
return this.name;
}
public void setSkillName(String skillName) {
this.name = skillName;
}
@Override
public String toString() {
return name;
}
}
and Position:
import javax.persistence.*;
@Entity
@Table(name = "POSITION")
public class Position {
private long positionId;
private String name;
private Skill requiredSkill;
public Position(String name, Skill requiredSkill) {
this.name = name;
this.requiredSkill = requiredSkill;
}
public Position() {}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "POSITION_ID")
public long getPositionId() {
return this.positionId;
}
public void setPositionId(long positionId) {
this.positionId = positionId;
}
@Column(name="POSITION_NAME", nullable=false, length = 100)
public String getPositionName() {
return this.name;
}
public void setPositionName(String positionName) {
this.name = positionName;
}
@OneToOne(cascade = CascadeType.ALL)
public Skill getRequiredSkill() {
return this.requiredSkill;
}
public void setRequiredSkill(Skill requiredSkill) {
this.requiredSkill = requiredSkill;
}
@Override
public String toString() {
return name;
}
}
Rule which is not working:
rule "Employee works only on positions with skills he has"
when
Assignment(
employee != null,
!getEmployee().getSkillSet().contains(getPosition().getRequiredSkill()))
then
scoreHolder.addHardConstraintMatch(kcontext, -100);
end
I think database is ok, because data in it was created using this app before. Also, I have another DRL rule which is working. I don't get any warnings/errors - just the aforementioned rule is not taken into account during solving.
During solving I get:
08:40:11.453 [main] DEBUG org.drools.core.common.DefaultAgenda - State was INACTIVE is now FIRING_ALL_RULES
08:40:11.453 [main] DEBUG org.drools.core.common.DefaultAgenda - State was FIRING_ALL_RULES is now HALTING
08:40:11.453 [main] DEBUG org.drools.core.common.DefaultAgenda - State was HALTING is now INACTIVE
08:40:11.453 [main] DEBUG org.optaplanner.core.impl.constructionheuristic.DefaultConstructionHeuristicPhase - CH step (1), time spent (134), score (-14init/-202hard/0soft), selected move count (7), picked move (domain.Assignment@4d354a3e {null -> domain.Employee@1511d157}).
Also, at the beggining I get this warning:
Failed to add the foreign key constraint. Missing index for constraint 'FKqx7n7alhjrnaw173dc48i47y0' in the referenced table 'skill'