Some print.
Some print.
~SingleForum~无废话记录~ + ~19~记录点有用的好吗?少说废话少装b~

~SingleForum~无废话记录~ » java

Hibernate复合主键的配置及调用

Started 2 years ago by admin. | Tags: . .

实体

JAVA:
  1. import java.io.Serializable;
  2.  
  3. import javax.persistence.Column;
  4. import javax.persistence.EmbeddedId;
  5. import javax.persistence.Entity;
  6. import javax.persistence.Table;
  7.  
  8. @Table(name = "FOR_GD_CONFIG")
  9. public class DbConfigEntity implements Serializable {
  10.  
  11.     @EmbeddedId
  12.     private DbConfigPk allPk;
  13.  
  14.     // @AttributeOverrides( {
  15.     // @AttributeOverride(name = "fid", column = @Column(name = "FLOWID",
  16.     // nullable = false)),
  17.     // @AttributeOverride(name = "m", column = @Column(name = "MARK", nullable =
  18.     // false)) })
  19.     public DbConfigPk getAllPk() {
  20.         return this.allPk;
  21.     }
  22.  
  23.     public void setAllPk(DbConfigPk allPk) {
  24.         this.allPk = allPk;
  25.     }
  26.  
  27.     @Column(name = "MARK_CN", nullable = true)
  28.     private String mC;
  29.  
  30.     public String getMc() {
  31.         return this.mC;
  32.     }
  33.  
  34.     public void setMc(String mC) {
  35.         this.mC = mC;
  36.     }
  37.  
  38.     @Column(name = "COLUMN_NAME", nullable = false)
  39.     private String cn;
  40.  
  41.     public String getCn() {
  42.         return this.cn;
  43.     }
  44.  
  45.     public void setCn(String cn) {
  46.         this.cn = cn;
  47.     }
  48.  
  49. }

实体中的复合主键

JAVA:
  1. package com.rizon.lyj.gwgd.db;
  2.  
  3. import java.io.Serializable;
  4.  
  5. import javax.persistence.Column;
  6. import javax.persistence.Embeddable;
  7.  
  8. /**
  9. * - 19. -
  10. *
  11. * 复合主键~good luck~
  12. *
  13. * @author <a href="http://www.LL19.com/">LL19.com</a>
  14. * @version 1.0
  15. */
  16.  
  17. @Embeddable
  18. public class DbConfigPk implements Serializable {
  19.  
  20.     @Column(name = "FLOWID", nullable = false)
  21.     private Long fid;
  22.  
  23.     public Long getFId() {
  24.         return this.fid;
  25.     }
  26.  
  27.     public void setFId(Long fid) {
  28.         this.fid = fid;
  29.     }
  30.  
  31.     @Column(name = "MARK", nullable = false)
  32.     private String m;
  33.  
  34.     public String getM() {
  35.         return this.m;
  36.     }
  37.  
  38.     public void setM(String m) {
  39.         this.m = m;
  40.     }
  41.  
  42. }

BEAN

JAVA:
  1. public class DbConfigBean {
  2.  
  3.     private Long fid;
  4.  
  5.     public Long getFId() {
  6.         return this.fid;
  7.     }
  8.  
  9.     public void setFId(Long fid) {
  10.         this.fid = fid;
  11.     }
  12.  
  13.     private String m;
  14.  
  15.     public String getM() {
  16.         return this.m;
  17.     }
  18.  
  19.     public void setM(String m) {
  20.         this.m = m;
  21.     }
  22.  
  23.     private String mC;
  24.  
  25.     public String getMc() {
  26.         return this.mC;
  27.     }
  28.  
  29.     public void setMc(String mC) {
  30.         this.mC = mC;
  31.     }
  32.  
  33.     private String cn;
  34.  
  35.     public String getCn() {
  36.         return this.cn;
  37.     }
  38.  
  39.     public void setCn(String cn) {
  40.         this.cn = cn;
  41.     }
  42.  
  43. }

实体和BEAN的转换类

JAVA:
  1. public class DbTools {
  2.  
  3.     public static DbConfigEntity toDbConfigEntity(DbConfigBean bean) {
  4.         if (bean == null) {
  5.             return null;
  6.         }
  7.         DbConfigEntity entity = new DbConfigEntity();
  8.  
  9.         entity.setCn(bean.getCn());
  10.         DbConfigPk dbConfigPk = new DbConfigPk();
  11.         dbConfigPk.setFId(bean.getFId());
  12.         dbConfigPk.setM(bean.getM());
  13.         entity.setAllPk(dbConfigPk);
  14.         entity.setMc(bean.getMc());
  15.  
  16.         return entity;
  17.     }
  18.  
  19.     public static DbConfigBean toDbConfigBean(DbConfigEntity entity) {
  20.         if (entity == null) {
  21.             return null;
  22.         }
  23.  
  24.         DbConfigBean bean = new DbConfigBean();
  25.         bean.setCn(entity.getCn());
  26.         bean.setFId(entity.getAllPk().getFId());
  27.         bean.setM(entity.getAllPk().getM());
  28.         bean.setMc(entity.getMc());
  29.  
  30.         return bean;
  31.     }
  32.  
  33. }

调用

JAVA:
  1. System.out.println(service.find0().size());
  2.  
  3.         DbConfigBean dbConfigBean = (DbConfigBean) service.find0().get(0);
  4.         System.out.println(dbConfigBean.getFId());
  5.         System.out.println(dbConfigBean.getM());
  6.         System.out.println(service.findMark("WHN").size());

SERVICE中

JAVA:
  1. public List< DbConfigBean > find0() {
  2.         Criterion[] criterion = new Criterion[1];
  3.         criterion[0] = Comparison.eq("allPk.fid", (long) 0);
  4.         List< DbConfigEntity > entitys = this.gbConfigDao.find(criterion);
  5.         List< DbConfigBean > beans = new ArrayList< DbConfigBean >(entitys.size());
  6.         for (DbConfigEntity entity : entitys) {
  7.             beans.add(DbTools.toDbConfigBean(entity));
  8.         }
  9.         return beans;
  10.  
  11.     }
  12.  
  13.     public List< DbConfigBean > findMark(String Mark) {
  14.         Criterion[] criterion = new Criterion[1];
  15.         criterion[0] = Comparison.eq("allPk.m", Mark);
  16.         OrderBy[] orders = new OrderBy[1];
  17.         orders[0] = (OrderBy.asc("allPk.fid"));
  18.         List< DbConfigEntity > entitys = this.gbConfigDao.find(orders, criterion);
  19.         List< DbConfigBean > beans = new ArrayList< DbConfigBean >(entitys.size());
  20.         for (DbConfigEntity entity : entitys) {
  21.             beans.add(DbTools.toDbConfigBean(entity));
  22.         }
  23.         return beans;
  24.     }

RSS feed for this topic