标签:embeddedid idclass ssh
通过以前的文章,我们了解到@Table、@Column、@Id实现了单表的映射,并且书剑有一个@Id指定的唯一字段。有时我们的数据表也许是有多个主键联合组成的,因此对于单表映射的主键,还可以进行如下两种联合主键映射。======================================================================
(一)联合主键:使用@IdClass指定外部主键
步骤:以实现外部主键的引用。
package com.tgb.itoo.exam.entity; import java.io.Serializable; @SuppressWarnings("serial") public class FamilyPK implements Serializable { private String man;//丈夫 private String woman;//妻子 public String getMan() { return man; } public void setMan(String man) { this.man = man; } public String getWoman() { return woman; } public void setWoman(String woman) { this.woman = woman; } public FamilyPK() { } public FamilyPK(String man, String woman) { this.man = man; this.woman = woman; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((man == null) ? 0 : man.hashCode()); result = prime * result + ((woman == null) ? 0 : woman.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; FamilyPK other = (FamilyPK) obj; if (man == null) { if (other.man != null) return false; } else if (!man.equals(other.man)) return false; if (woman == null) { if (other.woman != null) return false; } else if (!woman.equals(other.woman)) return false; return true; } }
package com.tgb.itoo.exam.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.IdClass; import javax.persistence.Table; @SuppressWarnings("serial") @Entity @Table(name="family") @IdClass(FamilyPK.class) public class Family implements Serializable { private String man;// 丈夫 private String woman;// 棋子 private String address;// 地址 @Id public String getMan() { return man; } public void setMan(String man) { this.man = man; } @Id public String getWoman() { return woman; } public void setWoman(String woman) { this.woman = woman; } @Column(name="address" ,length=100) public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
package com.tgb.itoo.exam.service; import javax.ejb.Remote; import com.tgb.itoo.exam.entity.Family; @Remote public interface FamilyDAORemote { // 新增 public boolean insert(Family family); // 插入 public Family selectByPK(String man, String woman); }
package com.tgb.itoo.exam.papermanage.serviceimpl; import javax.ejb.Stateless; import javax.persistence.EntityManager; import com.tgb.itoo.exam.entity.Family; import com.tgb.itoo.exam.entity.FamilyPK; import com.tgb.itoo.exam.service.FamilyDAORemote; @Stateless public class FamilyDAO implements FamilyDAORemote { protected EntityManager em; @Override public boolean insert(Family family) { try { em.persist(family); } catch (Exception e) { e.printStackTrace(); return false; } return true; } @Override public Family selectByPK(String man, String woman) { FamilyPK epk = new FamilyPK(man, woman); return em.find(Family.class, epk); } }
5)测试:客户端调用
package com.tgb.itoo.exam.papermanage.serviceimpl; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.NamingException; import com.tgb.itoo.exam.entity.Family; import com.tgb.itoo.exam.service.FamilyDAORemote; public class FamilyDAOClient { public static void main(String[] args) throws NamingException { //........ InitialContext ctx=new InitialContext(); FamilyDAORemote familyDAO=(FamilyDAORemote) ctx.lookup("FamilyDAO/remote"); //新增 Family family=new Family(); family.setMan("丈夫"); family.setWoman("妻子"); family.setAddress("地址"); familyDAO.insert(family); //查询 Family family2=familyDAO.selectByPK("丈夫的名称", "妻子的名称"); System.out.println(family2.getAddress()); } }
下文中将通过Demo演示《联合主键:使用@EmbeddedId嵌入外部主键》
持久化API(JPA)系列(七)实体关系映射(ORM)之单表映射@IdClass
标签:embeddedid idclass ssh
原文地址:http://blog.csdn.net/zhaolijing2012/article/details/45396151