I have a table T that has composite key (col1, col2)
I mapped it to Java class like following. Please note the use of @Id @GeneratedValue on one column while in DB the primary key is composed of both columns(can I do that ??)
Code:
@Entity
@Table(name="SSLast")
public class SSLast implements Serializable{
@Id @GeneratedValue
@Column(name="CID")
private int companyId;
@Column(name="SID")
private Integer serverId;
public int getCompanyId() {
return companyId;
}
public void setCompanyId(int companyId) {
this.companyId = companyId;
}
public Integer getServerId() {
return serverId;
}
public void setServerId(Integer serverId) {
this.serverId = serverId;
}
}
Now I have following code in some other class
Code:
Criteria criteria = getSession().createCriteria(SSLast.class);
criteria.add(Expression.eq("serverId", 551492));
Now I expect 5 rows and I get 5 rows in fact but companyId is same for all records. It always fetches correct number of rows but some how companyId is same for all records irrespective of what I pass for serverId
Similarly if I do following then I get the correct no of rows for companyId but serverId for all records are same.
Code:
Criteria criteria = getSession().createCriteria(SSLast.class);
criteria.add(Expression.eq("companyId", 1));
If I specify both serverId and companyId as follows then I always get 1 record that is correct behavior because companyId and serverId combination is Primary key
Code:
Criteria criteria = getSession().createCriteria(SSLast.class);
criteria.add(Expression.eq("serverId", 551492));
criteria.add(Expression.eq("companyId", 1));