Here is the mapping info.
Code:
@Entity
@Table(name = "DATE_DIM")
public class DateDim implements Serializable {
private int dateDimId;
private List<NonWorkingDate> nwd;
private int year;
@Id
@Column(name="date_dim_id", insertable = false, updatable = false, nullable = false)
public int getDateDimId() {
return dateDimId;
}
public void setDateDimId(int dateDimId) {
this.dateDimId = dateDimId;
}
@Column(name = "year")
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@OneToMany( fetch = FetchType.EAGER, targetEntity = NonWorkingDate.class )
@JoinColumn(name="date_dim_id")
public List<NonWorkingDate> getNwd() {
return nwd;
}
public void setNwd(List<NonWorkingDate> nwd) {
this.nwd = nwd;
}
}
@Entity
@Table(name = "NON_WORKING_DATE")
public class NonWorkingDate implements Serializable {
private int dateDimId;
private DateDim dateDim;
private int div;
@Id
@Column(name = "date_dim_id")
public int getDateDimId() {
return dateDimId;
}
public void setDateDimId(int dateDimId) {
this.dateDimId = dateDimId;
}
@ManyToOne( fetch = FetchType.EAGER, targetEntity = DateDim.class )
@JoinColumn(name="date_dim_id", insertable = false, updatable = false, nullable = false)
public DateDim getDateDim() {
return dateDim;
}
public void setDateDim(DateDim dateDim) {
this.dateDim = dateDim;
}
@Column(name = "div")
public int getDiv() {
return div;
}
public void setDiv(int div) {
this.div = div;
}
}
In the NonWorkingDate entity(or table) the primary key date_dim_id is also the foreignkey in DateDim table.
In DateDim table date_dim_id is the primary key.
Here is the Hibernate criteria:
Code:
Criteria ddc = Session().createCriteria(DD.class);
dateDimCriteria.add(Restrictions.eq("year", 2007));
Criteria n = ddc.createCriteria("nwd", Criteria.LEFT_JOIN);
n.add(Restrictions.eq("div", 17));
n.add(Restrictions.isNull(id));
return ddc.list();
I have to get rows in DateDim that are not in NonWorkingDate. The sql using join will be
select * from dd
left outer join nwd
on dd.Id = nwd.Id and nwd.div=17
where and dd.year=2007 and nwd.Id is null
projyal,
Code:
n.list();
will perform outerjoin on nonWorkingDate instead of DateDim. It will translate to select * from nwd
left outer join nwd on dd. This is not what I want.