Filters hibernate don’t works

I have two entity with link onetoone

Category

@Entity
@Table(name = "category")
@FilterDef(name = "currentLang", parameters = {
@ParamDef(name = "lang", type = "string")
})
public class Category implements Serializable {

@Id
@NotNull
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "dt")
private String dt;
@Column(name = "enable", length = 5)
private String enable;
@OneToOne()
@Filter(name = "currentLang", condition = ":lang = lang")
@JoinColumn(name = "context_id", nullable = false, updatable = false, referencedColumnName = "link")
private Context context;

public int getId() {
    return id;//test
}

public void setId(int id) {
    this.id = id;
}

public String getDt() {
    return dt;
}

public void setDt(String dt) {
    this.dt = dt;
}

public String getEnable() {
    return enable;
}

public void setEnable(String enable) {
    this.enable = enable;
}

public Context getContext() {
    return context;
}

public void setContext(Context context) {
    this.context = context;
}

public Category() {
}

public Category(String dt, String enable) {
    this.dt = dt;
    this.enable = enable;
}

}

Context

@Entity
@Table(name = "context")
@Component
@Scope("session")
public class Context implements Serializable {

@Id
@NotNull
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "text")
private String text;
@Column(name = "lang")
private String lang;
@Column(name = "link")
private Integer link;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getLang() {
    return lang;
}

public void setLang(String lang) {
    this.lang = lang;
}

public Context(String text, String lang) {
    this.text = text;
    this.lang = lang;
}

public Context() {
}

public Integer getLink() {
    return link;
}

public void setLink(Integer link) {
    this.link = link;
}

}

In this place i get model category.
List categories = categoryService.getAll();
com.blog.blog.entity.Category category = categories.get(0);

    org.apache.log4j.Logger.getRootLogger().addAppender(new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
    Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
    logger.info(category.getEnable());
    logger.info(category.getContext().getText());

But filter don’t work, i get wrong result and wrong query record in log.

Info:   Hibernate: select category0_.id as id1_0_, category0_.context_id as context_4_0_, category0_.dt as dt2_0_, category0_.enable as enable3_0_ from category category0_
Info:   Hibernate: select context0_.id as id1_1_0_, context0_.lang as lang2_1_0_, context0_.link as link3_1_0_, context0_.text as text4_1_0_ from context context0_ where context0_.link=?


Source: java

Leave a Reply