问题描述
我是 Spring 数据的新手,正在研究如何使用它.我认为我需要两种类型的查询:1)我可以使用这样的查询方法获得的简单查询
I'm new in Spring data and I'm studying how to use it.I thought that I need two type of query:1)Simple query that I can obtain with query methods like this
public interface CarRepository extends JpaRepository<Car, Integer> {
List<Car> findByid_Fleet(int idFleet);
}
但我获得了无效的派生查询!未找到 String 类型的物业车队!遍历路径:Car.id.在 car 表中有 id_fleet,一个外键.
but I obtain Invalid derived query! No property fleet found for type String! Traversed path: Car.id.In car table there is id_fleet, a foreign key.
2)我需要复杂的查询,我该怎么写?我的代码是如此结构化(例如一个域类):
2)I need complex query, how can I write them?My code is so structured (example for one domain class):
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
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);
}
}
以及我对数据库方法进行分组的数据库服务
and database services where I group my database method
@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);
....
推荐答案
对于你的第一个问题:你没有把外键名作为方法名,而是成员名:在你的汽车类中,你有一个名为 Fleet 的字段,在 Fleet 类中,一个名为 idFleet 的字段?那么名字应该是:
For your first question : you don't put the foreign key name as the method name, but the name of the member : in your car class, you have a field named Fleet and in Fleet class, a field named idFleet ?then the name should be :
`List<Car> findByFleetIdFleet(int idFleet);`
对于第二个问题,您需要一个自定义存储库,您将在其中实现那些复杂"查询:
And for the second question, you need a custom repository where you will implement those "complex" queries :
1 - public interface CarRepository extends JpaRepository, CarRepositoryCustom
2 - 公共接口 CarRepositoryCustom
3 - 公共类 CarRepositoryImpl 实现 CarRepositoryCustom
在自定义接口中,只需声明方法……然后在类中实现它们……spring 会做一些魔术(按名称代理接口/类)并且您的自定义查询将可用
in the custom interface, just declare methods ... and in the class, implement them .. spring will do some magic (proxying interfaces / classes by name) and your custom queries will be available
自定义类中的代码应该是这样的:
code in custom class should be like this :
@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();
}
这篇关于Spring数据和查询方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!