如何使用@OneToOne正确注释JPA enities?

时间:2016-09-30 15:07:01

标签: java hibernate jpa annotations

同事,我有两张桌子并选择获取CHAIN

SELECT OD_CHAINS_REQ.CHAIN FROM OD_CHAINS_REQ JOIN od_face_accs ON od_face_accs.ID = OD_CHAINS_REQ.ACCOUNT WHERE od_face_accs.ACNT='1608290029' ORDER BY OD_CHAINS_REQ.NUM;

OD_CHAINS_REQ表中的记录少于10条,od_face_accs表中的记录多于1000条。

我正试图通过JPA执行此选择。但是,正如我所说,我需要创建实体OdChainsReqEntity

    @Entity
    @Table(name = "OD_CHAINS_REQ", schema = "", catalog = "")
    @IdClass(OdChainsReqEntityPK.class)
    public class OdChainsReqEntity {
        private Integer chain;
        private Integer num;
        private Integer account;

        @Id
        @Column(name = "CHAIN")
        public Integer getChain() {
            return chain;
        }

        public void setChain(Integer chain) {
            this.chain = chain;
        }

        @Basic
        @Column(name = "NUM")
        public Integer getNum() {
            return num;
        }

        public void setNum(Integer num) {
            this.num = num;
        }

        @Id
        @Column(name = "ACCOUNT")
        public Integer getAccount() {
            return account;
        }

        public void setAccount(Integer account) {
            this.account = account;
        }


    @OneToOne
    @JoinColumn(name = "ACCOUNT", referencedColumnName = "ID")
    private OdFaceAccsEntity odFaceAccsEntity;
    public OdFaceAccsEntity getOdFaceAccsEntity() {
        return odFaceAccsEntity;
    }

    public void setOdFaceAccsEntity(OdFaceAccsEntity odFaceAccsEntity) {
        this.odFaceAccsEntity = odFaceAccsEntity;
    }

    /*@Overrides*/*/

}

OdFaceAccsEntity实体:

@Entity
@Table(name = "OD_FACE_ACCS", schema = "", catalog = "")
public class OdFaceAccsEntity {
    private Integer id;
    private String acnt;



    @Id
    @Column(name = "ID")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }



    @Basic
    @Column(name = "ACNT")
    public String getAcnt() {
        return acnt;
    }

    public void setAcnt(String acnt) {
        this.acnt = acnt;
    }

    /*@Overrides*/


    @OneToOne
    @JoinColumn (referencedColumnName = "ACCOUNT")
    private OdChainsReqEntity odChainsReqEntity;

    public OdChainsReqEntity getOdChainsReqEntity() {
        return odChainsReqEntity;
    }

    public void setOdChainsReqEntity(OdChainsReqEntity odChainsReqEntity) {
        this.odChainsReqEntity = odChainsReqEntity;
    }

}

所以我试图通过测试来获得链

 @Test
    public void getChainByDepoAcc() throws Exception {

        OdFaceAccsEntity odFaceAccsEntity = new OdFaceAccsEntity();
        odFaceAccsEntity.setAcnt("1608290029");


        List<OdFaceAccsEntity> odFaceAccsEntities = odFaceAccsDAO.getAcc(odFaceAccsEntity);

        for (int i = 0; i < odFaceAccsEntities.size(); i++) {
            LOG.info(odFaceAccsEntities.get(i).getId());
            LOG.info(odFaceAccsEntities.get(i).getOdChainsReqEntity().getChain());
        }
    }

但它返回异常:

  

引起:org.hibernate.MappingException:无法确定类型   for:com.alcap.app.JPA.Entities.OdFaceAccsEntity,at table:   OD_CHAINS_REQ,用于列:   [org.hibernate.mapping.Column(odFaceAccsEntity)]

如何正确指定enities中的关联以避免异常?

更新 我尝试过的最后一个DAO实现:

  @Transactional(value = "txMan", readOnly = true)
    public List <OdChainsReqEntity> getOdChainsReqByDEPOAcc(String in$depoAccnt) {
        Query query = em.createQuery("SELECT cre FROM OdChainsReqEntity cre " +
            "join OdFaceAccsEntity fae on fae.id = cre.account "+
           "where fae.acnt = :depoAcc");
         query.setParameter("depoAcc", in$depoAccnt);
        List<OdChainsReqEntity> result =  query.getResultList();
        return result;
    }

我也试过这个(但我不知道它是否适合join选择):

public List<OdChainsReqEntity> getOdChainsReqByAnyParam(OdChainsReqEntity odChainsReqEntity) {

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<OdChainsReqEntity> criteriaQuery = cb.createQuery(OdChainsReqEntity.class);
        Root<OdChainsReqEntity> entityRoot = criteriaQuery.from(OdChainsReqEntity.class);

        Predicate criteria = cb.conjunction();

        if (odChainsReqEntity.getChain() != null) {Predicate p = cb.equal(entityRoot.get("chain"), odChainsReqEntity.getChain()); criteria = cb.and(criteria, p);}
        if (odChainsReqEntity.getNum() != null) {Predicate p = cb.equal(entityRoot.get("num"), odChainsReqEntity.getNum()); criteria = cb.and(criteria, p);}
        if (odChainsReqEntity.getAccount() != null) {Predicate p = cb.equal(entityRoot.get("account"), odChainsReqEntity.getAccount()); criteria = cb.and(criteria, p);}


        criteriaQuery.where(criteria);
        List<OdChainsReqEntity> result = em.createQuery(criteriaQuery).getResultList();

        return result;

    }

1 个答案:

答案 0 :(得分:1)

您还没有发布您的DAO实施。假设它是正确的,可能是以下问题之一:

  1. 您定义了access type。您将映射注释(@Column@OneToOne,...)放在getter方法以及未由规范定义的实体字段中。因此,在字段或getter上移动映射注释,但不在两者上移动:

    @Entity
    @Table(name = "OD_FACE_ACCS", schema = "", catalog = "")
    @Access(AccessType.PROPERTY)
    public class OdFaceAccsEntity {
        private Integer id;
        private String acnt;
        private OdChainsReqEntity odChainsReqEntity;
    
        @Id
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getAcnt() {
            return acnt;
        }
    
        public void setAcnt(String acnt) {
           this.acnt = acnt;
        }
    
        /*@Overrides*/
    
        @OneToOne(mappedBy = "odFaceAccsEntity")
        public OdChainsReqEntity getOdChainsReqEntity() {
            return odChainsReqEntity;
        }
    
        public void setOdChainsReqEntity(OdChainsReqEntity odChainsReqEntity) {
            this.odChainsReqEntity = odChainsReqEntity;
         }
    
    }
    
    
    
    @Entity
    @Table(name = "OD_CHAINS_REQ", schema = "", catalog = "")
    @IdClass(OdChainsReqEntityPK.class)
    @Access(AccessType.PROPERTY)
    public class OdChainsReqEntity {
        private Integer chain;
        private Integer num;
        private Integer account;
        private OdFaceAccsEntity odFaceAccsEntity;
    
        // the rest of the code here
    
        @OneToOne
        @JoinColumn(name = "ACCOUNT", referencedColumnName = "ID")
        public OdFaceAccsEntity getOdFaceAccsEntity() {
            return odFaceAccsEntity;
        }
    
        // the rest of the code
    
    }
    
  2. 正如您所看到的,我在getter方法上移动了所有必要的映射注释(我刚删除了一些注释以节省空间,因为它们默认存在)。

    1. 第二个问题是您在两个不正确的实体上放置了@JoinColumn注释。您必须将此注释仅放置在关系的一侧,该关系应该是关系的所有者;在一对一的关系中,放置注释的哪一方并不重要。需要将关系的另一侧映射为@OneToOne(mappedBy = "")。我只是将OdFaceAccsEntity实体作为关系的反面并相应地更改(请参阅上面代码中的@OneToOne)。如果你不希望这个实体是反面,只需反转注释
    2. 以下是您可能阅读以了解如何正确映射的JPA 2.0 specification 部分:

      • 2.3访问类型
      • 2.10.1双向OneToOne关系