Spring数据和查询方法

时间:2015-10-20 14:57:35

标签: java mysql spring spring-data

我是Spring数据的新手,我正在研究如何使用它。 我以为我需要两种类型的查询: 1)我可以使用像这样的查询方法获得的简单查询

public interface CarRepository extends JpaRepository<Car, Integer> {
    List<Car> findByid_Fleet(int idFleet);
}

但我获得了无效的派生查询!没有为String类型找到属性舰队!遍历路径:Car.id. 在car table中有id_fleet,一个外键。

2)我需要复杂的查询,如何编写它们? 我的代码是如此结构化(一个域类的示例):

public interface CarRepository extends JpaRepository<Car, Integer> {
        List<Car> findByid_Fleet(int idFleet);
    }

汽车服务

public interface CarServices extends GeneralServices<Car, Integer>{

    /**
     * Return list of cars for one fleet
     * @param idFleet
     * @return
     */
    public List<Car> findCarsByIdFleet(int idFleet);

}

carServicesImpl

@Service
public class CarServicesImpl implements CarServices {
    @Resource
    private CarRepository carRepository;

    @Override
    public Car create(Car car) {
        return carRepository.save(car);
    }

    @Override
    public Car findById(Integer id) {
        return carRepository.getOne(id);
    }

    @Override
    public boolean exists(Integer id) {
        return carRepository.exists(id);
    }

    @Override
    public List<Car> findCarsByIdFleet(int idFleet) {

        return carRepository.findByid_Fleet(idFleet);
    }
}

以及我将数据库方法分组的数据库服务

@Service
public class DatabaseFleetsAndCarsServicesImpl implements DatabaseFleetsAndCarsServices {
    static final Logger LOG = LoggerFactory.getLogger(DatabaseFleetsAndCarsServicesImpl.class);
    @Autowired
    private CarServices carServices;
    @Autowired
    private FleetServices fleetServices;

    @Override
    public List<Fleet> getFleets() {        
        return fleetServices.getFleets();
    }

    @Override
    public List<Car> getCars(int idFleet) {
        return carServices.findCarsByIdFleet(idFleet);
    }
}

我的汽车课程的一部分:

/**
 * Car generated by hbm2java
 */
@Entity
@Table(name = "car", catalog = "ATS")
public class Car implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer idCar;
    private CarType carType;
    private Fleet fleet;
    private String id;
    private int initialKm;
    private String carChassis;
    private String note;
    private Set<Acquisition> acquisitions = new HashSet<Acquisition>(0);
....

1 个答案:

答案 0 :(得分:0)

对于你的第一个问题:你没有把外键名称作为方法名称,而是把成员的名字:在你的汽车类中,你有一个名为Fleet的字段,在Fleet类中有一个名为idFleet的字段? 那么名字应该是:

`List<Car> findByFleetIdFleet(int idFleet);`

对于第二个问题,您需要一个自定义存储库,您将在其中实现这些“复杂”查询:

1 - public interface CarRepository extends JpaRepository<Car, Integer>, CarRepositoryCustom

2 - public interface CarRepositoryCustom

3 - public class CarRepositoryImpl implements CarRepositoryCustom

在自定义界面中,只需声明方法......并在类中实现它们.Spring会做一些魔法(按名称代理接口/类)并且您的自定义查询可用

自定义类中的

代码应该是这样的:

@PersistenceContext
private EntityManager entityManager;

public List<Car> findAllCars() {
  String jpql = "select c from Car c";
  Query query = entityManager.createQuery(jpql);
  return query.getResultList();
}

(和参数):

public List<Car> findAllByCarChassis(String chassis) {
      String jpql = "select c from Car c where c.carChassis = :chassisParam";

      Query query = entityManager.createQuery(jpql);
      query.setParameter("chassisParam", chassis);

      return query.getResultList();
}