通过指定关系来组合两个模型类

时间:2017-01-30 08:56:04

标签: hibernate

// DAOIMPL Class //

StringBuffer queryString = new StringBuffer();
    queryString .append("SELECT s.state_id, s.state_name, c.country_name, s.active_status ");   
    queryString.append("   FROM Country c, State s ");
    queryString.append("  WHERE s.country_id = c.country_id ");
    queryString.append("    AND s.deleted_status ='A' ");

try {
session = sessionFactory.openSession();
SQLQuery sqlQuery = session.createSQLQuery(queryString.toString());
List<Object[]> StateList = (List<Object[]>) sqlQuery.list();
statelist = new ArrayList<>();
State st= null;
for (Object[] sList : StateList) {
st = new State();
st.setState_id((Integer)sList[0]);
st.setState_name((String)sList[1]);
st.setCountry_name((String)sList[2]);
st.setActive_status((Integer)sList[3]);                 
statelist.add(st);
}

两个表是:

//Country Table//

@Id
@GeneratedValue
@Column(name = "country_id")
private int country_id;

@Column(name = "country_name")
private String country_name;    

@Column(name = "active_status")
private int active_status;

@Column(name = "deleted_status")
private String deleted_status;

@Column(name ="created_by")
private String created_by;

@Temporal(TemporalType.DATE)
@Column(name ="created_at")
private Date created_at;    

@Column(name ="modified_by")
private String modified_by;

@Temporal(TemporalType.DATE)
@Column(name ="modified_at")
private Date modified_at;

//State Table//

@Id
@GeneratedValue
@Column(name = "state_ID")
private int state_id;

@Column(name = "state_Name")
private String state_name;      

@Column(name="state_Status")
private int active_status;

@Column(name="state_DeleteStatus")
private String deleted_status;

@Column(name ="created_by")
private String created_by;

@Temporal(TemporalType.DATE)
@Column(name ="created_at")
private Date created_at;

@Column(name ="modified_by")
private String modified_by;

@Temporal(TemporalType.DATE)
@Column(name ="modified_at")
private Date modified_at;



here in query i am taking country name state model class does not contain the country name attribute so how can i join two model class 

0 个答案:

没有答案