jpa 如果bean里有联合主键需要自己写handler
/** * 查找学生的课程 * 没有加上学生类型 * */public boolean findStdCourseByScId(int uid, int cid) {//String sql = "select * from 7east_usercourse_right where userid=? and courseid=? and type=1";String sql = "select * from 7east_usercourse_right where userid=? and courseid=?";UserCourseMapper argTypes =new UserCourseMapper();Object[] args = new Object[] { uid, cid };List<UserCourse> list = getJdbcTemplate().query(sql, args, argTypes );if(list!=null){for(UserCourse c:list){if(list.size()>0){System.out.println("findStdCourseByScId:"+c.toString());}}return list.size()>0?true:false;}else{return false;}}package com.qieast.platform.admincp.dao.impl;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;import com.qieast.platform.model.Teacher;import com.qieast.platform.model.UserCourse;import com.qieast.platform.model.UserCourseRightPK;public class UserCourseMapper implements RowMapper<UserCourse>{public UserCourse mapRow(ResultSet rs, int rowNum) throws SQLException {UserCourse userCourse =new UserCourse();UserCourseRightPK userCourseRightPK = new UserCourseRightPK();userCourseRightPK.setCourseid(rs.getInt("courseid"));userCourseRightPK.setType(rs.getString("type"));userCourseRightPK.setUserid(rs.getInt("userid"));userCourse.setId(userCourseRightPK);userCourse.setEndtime(rs.getInt("endtime"));userCourse.setLessonnumber(rs.getInt("lessonnumber"));userCourse.setListentimes(rs.getInt("listentimes"));userCourse.setStarttime(rs.getInt("starttime"));userCourse.setTotallessonnumber(rs.getInt("totallessonnumber"));return userCourse;}}@Datapublic class UserCourse implements Serializable {private static final long serialVersionUID = 1L;private int endtime;@EmbeddedIdprivate UserCourseRightPK id;private int lessonnumber;private int listentimes;private int starttime;private int totallessonnumber;public UserCourse() { }@Datapublic class UserCourseRightPK implements Serializable {//default serial version id, required for serializable classes.private static final long serialVersionUID = 1L;private int courseid;private String type;private int userid;/** * 联合主键 * * 1、必须实现Serializable序列化 2、必须提示无参的构造方法 3、必须重写hashCode和equals方法 * * @Embeddable 表示该类中所有属性在应用该联合主键的类中作为它的属性(字段) * @author 张明学 * */@Embeddablepublic class AirLinePK implements Serializable {private String staCity;private String endCity;public AirLinePK() {}public AirLinePK(String staCity, String endCity) {this.staCity = staCity;this.endCity = endCity;}@Column(nullable = false, length = 32)public String getStaCity() {return staCity;}public void setStaCity(String staCity) {this.staCity = staCity;}@Column(nullable = false, length = 32)public String getEndCity() {return endCity;}public void setEndCity(String endCity) {this.endCity = endCity;}@Overridepublic int hashCode() {final int PRIME = 31;int result = 1;result = PRIME * result + ((endCity == null) ? 0 : endCity.hashCode());result = PRIME * result + ((staCity == null) ? 0 : staCity.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;final AirLinePK other = (AirLinePK) obj;if (endCity == null) {if (other.endCity != null)return false;} else if (!endCity.equals(other.endCity))return false;if (staCity == null) {if (other.staCity != null)return false;} else if (!staCity.equals(other.staCity))return false;return true;}}@Entity@Table(name = "AIRLINE")public class AirLineEntity {private AirLinePK a_id;private String a_name;// 联合主键@EmbeddedIdpublic AirLinePK getA_id() {return a_id;}public void setA_id(AirLinePK a_id) {this.a_id = a_id;}@Column(length = 50)public String getA_name() {return a_name;}public void setA_name(String a_name) {this.a_name = a_name;}}public class CompositePKTest {@Testpublic void save(){EntityManagerFactory factory = Persistence.createEntityManagerFactory("mengya");EntityManager em = factory.createEntityManager();em.getTransaction().begin();AirLineEntity airLine = new AirLineEntity();airLine.setA_id(new AirLinePK("BeJing","ShangHai"));airLine.setA_name("北京至上海");em.persist(airLine);em.getTransaction().commit();em.close();factory.close();}}