我是新的Hibernate和Drools,我希望這些流水評估給定Person的規則,並且只有在給定表上存在Person ID時纔會觸發。Drools休眠JPA註釋多對多示例DRL
例如:如果我有一個
表中調用 '人' 與字段:PERSONID = '1015'。 一個名爲'likes'的字段:id = 1,name = sports。
和一箇中間值表格稱爲'personLikes'字段:personID = 1015,idLikes = 1。
我加載具有三種不同的人:personID=1015
,personID=1020
,personID=1035
。
在該情況下,僅對人code=1015
執行規則。
問題是:我該如何構造該規則?
這是我的代碼。
DRL FILE:
rule healthy when
p: Person()
i: Interes(nombre=="Sports")
InteresPersonaId(persona==p,interes==i)
//InteresesPersona(pk==ip)
then
System.out.println("OK" + p.id + "LIKES" +i.nombre);
end
DB:
CREATE TABLE IF NOT EXISTS `screb`.`person` (
`idPerson` VARCHAR(15) NOT NULL,
`gender` CHAR(1) NULL DEFAULT NULL,
`age` INT(11) NULL DEFAULT NULL,
`estrato` INT(11) NULL DEFAULT NULL,
`idLocalizacion` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`idPerson`),
INDEX `FK_location_idx` (`idLocalizacion` ASC),
CONSTRAINT `FK_location`
FOREIGN KEY (`idLocalizacion`)
REFERENCES `screb`.`localizacion` (`idLocalizacion`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
USE `screb` ;
-- -----------------------------------------------------
-- Table `screb`.`interes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `screb`.`interes` (
`idInteres` INT(11) NOT NULL AUTO_INCREMENT,
`nombre` VARCHAR(45) NULL DEFAULT NULL,
`descripcion` VARCHAR(45) NULL DEFAULT NULL,
PRIMARY KEY (`idInteres`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `screb`.`interesesxpersona`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `screb`.`interesesxpersona` (
`persona` VARCHAR(15) NOT NULL DEFAULT '',
`Interes` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`persona`, `Interes`),
INDEX `FK_PERS_INT_idx` (`Interes` ASC),
CONSTRAINT `FK_IN_PER`
FOREIGN KEY (`persona`)
REFERENCES `screb`.`person` (`idPerson`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_PERS_INT`
FOREIGN KEY (`Interes`)
REFERENCES `screb`.`interes` (`idInteres`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
類加載事實:
public class Campaign {
public static final void main(String[] args) {
KieContainer kc = KieServices.Factory.get().getKieClasspathContainer();
KieSession ksession = kc.newKieSession("CampaignKS");
PersonManager person = new PersonManager();
List<Person> list = person.listEvents();
for(Person p :list){
ksession.insert(p);
}
InteresManager interes = new InteresManager();
List<Interes> listaIntereses = interes.listIntereses();
for(Interes i :listaIntereses){
ksession.insert(i);
}
ksession.fireAllRules();
ksession.dispose();
}
}
映射類:
@Entity
@Table(name="interes",schema="screb")
public class Interes implements java.io.Serializable {
public int idInteres;
public String nombre;
public String descripcion;
public Set<InteresesPersona> interesesP = new HashSet<InteresesPersona>(0);
public Interes(){}
public Interes(int interes,String nombre, String descripcion){
this.idInteres=interes;
this.nombre=nombre;
this.descripcion=descripcion;
}
public Interes(int interes,String nombre, String descripcion, Set<InteresesPersona>interesesPersona){
this.idInteres=interes;
this.nombre=nombre;
this.descripcion=descripcion;
this.interesesP=interesesPersona;
}
@Id @GeneratedValue
@Column(name="idInteres")
public int getIdInteres() {
return idInteres;
}
public void setIdInteres(int idInteres) {
this.idInteres = idInteres;
}
@Column(name="nombre")
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
@Column(name="descripcion")
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.interes")
public Set<InteresesPersona> getInteresesP(){
return this.interesesP;
}
public void setInteresesP(Set<InteresesPersona> interesesPersona){
this.interesesP=interesesPersona;
}
}
類InteresesPersona(PersonLikes):
@Entity
@Table(name="interes_persona",schema = "screb")
@AssociationOverrides({
@AssociationOverride(name = "pk.persona",
joinColumns = @JoinColumn(name = "idPerson")),
@AssociationOverride(name = "pk.interes",
joinColumns = @JoinColumn(name = "idInteres")) })
public class InteresesPersona implements java.io.Serializable{
public InteresPersonaId pk = new InteresPersonaId();
private Date createdDate;
public InteresesPersona(){}
@EmbeddedId
public InteresPersonaId getPk() {
return pk;
}
public void setPk(InteresPersonaId pk) {
this.pk = pk;
}
@Transient
public Person getPersona() {
return getPk().getPersona();
}
public void setPersona(Person persona) {
getPk().setPersona(persona);
}
@Transient
public Interes getInteres() {
return getPk().getInteres();
}
public void setInteres(Interes interes) {
getPk().setInteres(interes);
}
@Temporal(TemporalType.DATE)
@Column(name = "createdDate", nullable = false, length = 10)
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
InteresesPersona that = (InteresesPersona) o;
if (getPk() != null ? !getPk().equals(that.getPk())
: that.getPk() != null)
return false;
return true;
}
public int hashCode() {
return (getPk() != null ? getPk().hashCode() : 0);
}
}
類InteresesPersonaID(PersonLikesID)
@Embeddable
public class InteresPersonaId implements java.io.Serializable {
public Person persona;
public Interes interes;
@ManyToOne
public Person getPersona() {
return persona;
}
public void setPersona(Person persona) {
this.persona = persona;
}
@ManyToOne
public Interes getInteres() {
return interes;
}
public void setInteres(Interes interes) {
this.interes = interes;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
InteresPersonaId that = (InteresPersonaId) o;
if (persona != null ? !persona.equals(that.persona) : that.persona!= null) return false;
if (interes != null ? !interes.equals(that.interes) : that.interes != null)
return false;
return true;
}
public int hashCode() {
int result;
result = (persona != null ? persona.hashCode() : 0);
result = 31 * result + (interes != null ? interes.hashCode() : 0);
return result;
}
}
類PERSON
@Entity
@Table(name="person",schema="screb")
public class Person implements java.io.Serializable{
public String id;
public char gender;
public int age;
public int estrato;
public Set<InteresesPersona> interesesP = new HashSet<InteresesPersona>(0);
public Person(){}
public Person(String id,char genero,int edad,int estrato){
this.id=id;
this.gender=genero;
this.age=edad;
this.estrato=estrato;
}
public Person(String id,char genero,int edad,int estrato,Set<InteresesPersona>interesesPersona){
this.id=id;
this.gender=genero;
this.age=edad;
this.estrato=estrato;
this.interesesP = interesesPersona;
}
@Id
@Column(name="idPerson")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(name="gender")
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
@Column(name="age")
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Column(name="estrato")
public int getEstrato() {
return estrato;
}
public void setEstrato(int estrato) {
this.estrato = estrato;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy= "pk.persona",cascade=CascadeType.ALL)
public Set<InteresesPersona> getInteresesP(){
return this.interesesP;
}
public void setInteresesP(Set<InteresesPersona> interesesPersona){
this.interesesP=interesesPersona;
}
@Override
public boolean equals(Object o){
if(this==o){return true;}
if(o==null||getClass()!=o.getClass()){return false;}
Person person = (Person) o;
if (!id.equals(person.id)){return false;}
if (gender!=person.gender){return false;}
if (age!=person.age){return false;}
if (estrato!=person.estrato){return false;}
return true;
}
@Override
public int hashCode(){
int result = id.hashCode();
return result;
}
}
類的PersonManager:
public class PersonManager {
public List listEvents(){
Session session= HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query= session.createQuery("from Person");
List<Person> list = query.list();
session.getTransaction().commit();
return list;
}
}
是的,有不同的,'InteresPersonaId'包含中間關係'InteresesPersona'的鍵。它的Means包含來自'Interes'表的'Person'和key'idInteres'的映射關鍵字'idPerson'。和'InteresPersona'表示InteresPersona和Person之間的中間表關係,InteresPersona和Interes包含'InteresPersonaId'(KEY)和附加字段'createdDate'。 – Wilisumo 2015-04-06 15:07:01
當我運行規則時,拋出錯誤無法懶惰地初始化集合。所以我將'FetchType.LAZY'改爲'FetchType.EAGER',現在運行。但規則不會觸發我不知道如果事實加載正確(類Campaign)或者我也必須從'InteresesPersona'加載事實 – Wilisumo 2015-04-06 15:10:45
隨着第一條規則,你不應該插入'InteresesPersona'作爲事實,但是當你插入一個Person作爲事實時,它們必須存儲在'Person'的集合interesesP中。根據第二條規則,它們必須作爲事實插入。 – laune 2015-04-06 15:35:56