0

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);
}

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)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);
    }

car services

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);
    }
}

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);
    }
}

Part of my car class:

/**
 * 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);
....
luca
  • 3,248
  • 10
  • 66
  • 145

1 Answers1

0

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<Car, Integer>, CarRepositoryCustom

2 - public interface CarRepositoryCustom

3 - public class CarRepositoryImpl implements CarRepositoryCustom

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();
}

(and with parameter) :

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();
}
Pras
  • 1,068
  • 7
  • 18
  • that error , I have Fleet object in my car class. So I have to retrieve Fleet by id and put this object into findByFleet method? – luca Oct 20 '15 at 15:08
  • no, just name your method : findByFleetId ... it will first check the member Fleet in Car class, end then check Id member in Fleet class ... – Pras Oct 20 '15 at 15:09
  • if I use List findByFleetId(int idFleet); I receive Invalid derived query! No property id found for type Fleet! Traversed path: Car.fleet. – luca Oct 20 '15 at 15:14
  • do you have a property id in your Fleet class ? – Pras Oct 20 '15 at 15:15
  • I had to rename List findByFleetIdFleet(int idFleet); because Fleet has idFleet....it is correct? – luca Oct 20 '15 at 15:16
  • thanks, for the second question could you give me an example of query?(also stupid query) – luca Oct 20 '15 at 15:23
  • i'll update my answer .. (hoping that you have configured the entitymanager in spring files) – Pras Oct 20 '15 at 15:31
  • Isnt it better to just create the custom query inside the repository? `@Query("select c from Car where c.carChassis = ?1") List getCarListByChassis(String carChassis);` – dubonzi Oct 20 '15 at 17:34
  • this is not my query, I would have to retrieve Fleet by idFleet and then the cars by Fleet – luca Oct 21 '15 at 09:32
  • @Pras thanks for the example, only one error into String jpql, i forgot to insert from Car c – luca Oct 21 '15 at 15:27