通过外键返回实例列表

时间:2019-03-25 10:34:34

标签: java hibernate spring-data-jpa

我有两个模型,所有者和合同。合同具有所有者的实例,所有者没有合同列表。我正在尝试查询合同列表,以返回按所有者过滤的列表,即按所有者过滤的合同列表。

我曾尝试遵循先前的示例并使用Criteria编写自定义查询,但是,按照建议,我检查了底座并尝试使用命名查询,但是,我仍然很努力。

There was an unexpected error (type=Internal Server Error, status=500).
Named parameter not bound : ownerId; nested exception is org.hibernate.QueryException: Named parameter not bound : ownerId

我的模型如下:


@Entity
@Table(name="Contracts")
@NamedQueries({
        @NamedQuery(
                name = "Contract.allContractsByOwner",
                query = "SELECT c FROM Contract c WHERE c.owner.id LIKE :ownerId"
        )
})

public class Contract {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Long id;

    @ManyToOne
    private Owner owner;

    @Column
    private double price;

    @Column
    private String deliverDate;

public Contract(Owner owner, double price, String deliverDate) {
        this.id = id;
        this.owner = owner;
        this.price = price;
        this.deliverDate = deliverDate;
    }


@Entity
@Table(name="Owners")
public class Owner {
  @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Long id;

    @Column
    private String name;

    public Owner(String name){
        this.name = name;
    }

我的contractRepoImpl


@Service
    public class ContractRepositoryImpl implements ContractRepositoryCustom {


        ContractRepository contractRepository;

        @Autowired
        EntityManager entityManager;

        public List allContractsByOwner(Long ownerId) {
            List contracts = entityManager.createQuery(
                    "SELECT c FROM Contract c WHERE c.owner.id LIKE :ownerId", Contract.class)
                    .getResultList();
            return contracts;
        }
    }
我在我的ContractRepo和ContractRepoCustom文件中命名

,然后在我的控制器中将它映射为它。但是,当我在浏览器中查询它时,我的终端出现了错误。

  @GetMapping(value="/owners/{ownerId}/contracts")
    public List allContractsByOwner(@PathVariable("ownerId") Long ownerId){
        return contractRepository.allContractsByOwner(ownerId);
    }

我很欣赏这可能是初学者的错误,我正在尝试关注文档,但对语法和需要注释的地方有些困惑。

1 个答案:

答案 0 :(得分:0)

感谢JB Nizet,最后到达了

我向我的contractRepoImpl添加了参数

  @Service
    public class ContractRepositoryImpl implements ContractRepositoryCustom {


        ContractRepository contractRepository;

        @Autowired
        EntityManager entityManager;


        public List allContractsByOwner(Long id) {
            List contracts = entityManager.createQuery(
                    "SELECT c FROM Contract c WHERE c.owner.id = :ownerId", Contract.class)
                    .setParameter("ownerId", id)
                    .getResultList();
            return contracts;
        }
    }

然后产生一个SQL错误,我通过在我的Contract类中将@NamedQuery从'LIKE'更改为'='来解决此问题...


@NamedQueries({
        @NamedQuery(
                name = "Contract.allContractsByOwner",
                query = "SELECT c FROM Contract c WHERE c.owner.id = :ownerId"
        )
})