使用外键查找所有约束

find all constraints using foreign key

我有两个实体,即:

@Entity
@Table(name = "State")
public class StateEntity {

   @Column(name = "id", length = 36, nullable = false, unique = true)
   private String id;

  @ManyToOne (fetch = FetchType.LAZY)
  @JoinColumn(name = "InsurerId", nullable = false)
  private InsurerEntity insurer;

  @Column(name ="StateName", length = 50, nullable = false)
  private String stateName;

//getters and setters

}

保险公司

@Entity
@Table(name = "Insurer")
public class InsurerEntity {

  @Column(name = "InsurerId", length = 36, nullable = false, unique = true)
  private String insurerId;

  @Column(name = "InsurerName", length = 100, nullable = true)
  private String insurerName;

  @OneToMany(mappedBy = "state", fetch = FetchType.LAZY)
  private List<StateEntity> stateEntityList;

//getters and setters

}

保险公司的 ID 作为 'insurerid' 保存在状态数据库中,我想使用休眠条件查询来检索它,但我似乎无法弄清楚。使用 statename 很容易获得外键,但是当我尝试反过来时,没有任何效果

我正在尝试执行此查询:SELECT StateName FROM Mydb.state where InsurerId='74'

我试过了this:1

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @Transactional(readOnly = true)
    public List<StateEntity> findByForeignId(String id) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<StateEntity> criteriaQuery = criteriaBuilder.createQuery(StateEntity.class);
        Root<StateEntity> root = criteriaQuery.from(StateEntity.class);

        criteriaQuery = criteriaQuery.select(root).where(criteriaBuilder.equal(root.get("insurer"), id));
        try {
            return entityManager.createQuery(criteriaQuery).getResultList();
        } catch (NoResultException nre) {
            return null;
        } catch (NonUniqueResultException nure) {
            return null;
        }
    }

但是 returns:

java.lang.IllegalArgumentException: Parameter value [74] did not match expected type [in.unlimitdigital.poleasy.entities.InsurerEntity (n/a)]

和:2

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    @Transactional(readOnly = true)
    public List<ModelEntity> findByForeignId(String id) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<ModelEntity> criteriaQuery = criteriaBuilder.createQuery(ModelEntity.class);
        Root<BrandEntity> root = criteriaQuery.from(BrandEntity.class);

        criteriaQuery = criteriaQuery.multiselect(root).where(criteriaBuilder.equal(root.get("id"), id));
        try {
            return entityManager.createQuery(criteriaQuery).getResultList();
        } catch (NoResultException nre) {
            return null;
        } catch (NonUniqueResultException nure) {
            return null;
        }
    }

它returns:

java.lang.ClassCastException: org.hibernate.hql.internal.ast.tree.SqlNode cannot be cast to org.hibernate.hql.internal.ast.tree.FromReferenceNode

请帮我解决这个...

问题是您在 InsurerEntity class.

中遗漏了 id 变量

更改此行:

criteriaQuery = criteriaQuery.select(root).where(criteriaBuilder.equal(root.get("insurer"), id));

对此:

criteriaQuery = criteriaQuery.select(root).where(criteriaBuilder.equal(root.get("insurer").get("id"), id));