0

I am experimenting with Springboot, I have several entities with several properties that are very similar, example: The entity "Car" has model and price properties so do the "Motorcycle" model. I would like to use a a class to define just those common attributes, class "Vehicle" with the properties model and price.

In another words:

Car                             Motorcicle                             Vehicle
price                              price                                price
model                              model                                 model
year                              year
color                              color
fuel_type                         fuel_type
... 30 more properites                ...30 more properties

I want to use vehicle to map the search result of a SQL union between Car and Motorcycle tables.

public interface ImovelRepositorio extends JpaRepository<Vehicle, Long> {

      @Query(value = "here select query with union between car and motorcycle", nativeQuery = true)
  List<Vehicle> searchVehicle();

}

Here Vehicle class is a normal class without any annotation. I am getting errors saying that Vehicle is not managed type. Of course my repository doesn't make sense, I expected this error. But how can I achieve what I want?

When I perform a union I am not getting a list of models and am getting a list of one or more models. I want to create a class to "combine" the result of a search.

Diego Alves
  • 2,462
  • 3
  • 32
  • 65
  • you may try the following query, but i'm not sure that is it works `SELECT new Vehicle(price, model) FROM car UNION SELECT price, model FROM motorycycle` – Bilal Ekrem Harmanşa Sep 29 '19 at 12:34
  • I am not concerned about the sql query. I am brand new to Springboot but I think the error is because repository expects an entity and I am using a normal class. Besides used the Car, motorcycle and Vehicle to summarize my problem and make it easier to understand. My problem is a little bit different. – Diego Alves Sep 29 '19 at 12:45

3 Answers3

0

You can use @MappedSuperClass where the Vehicle can be the mapped super class. The remaining classes can just extend it. Somewhat like below:

The super class:

@MappedSuperClass
class Vehicle {
// constructor, getters, setters 
}

The entity classes:

@Entity
class Car extends Vehicle {
// constructor, getters, setters 
}

@Entity
class MotorCicle extends Vehicle {
// constructor, getters, setters 
}
jagbandhuster
  • 677
  • 2
  • 7
  • 20
0

FluentJPA has a solution for this case.

Konstantin Triger
  • 1,576
  • 14
  • 11
0

I see that you use SpringData JPA. In JPQL you can implement this by writing select new fully.qualified.name.of.your.Class(field1, field2) from Entity etc. You have to have proper constructor in your class fully.qualified.name.of.your.Class. This class does NOT have to be an @Entity - it can be any usual POJO or anything else you want.

However, I see that you also use native query. For native query this will not work - you'll have to declare return type as List<Object[]> and do mapping from that yourself, or use @NamedNativeQuery with @SqlResultSetMapping. But do you really need a native query there?

mvmn
  • 3,717
  • 27
  • 30
  • See also https://stackoverflow.com/questions/46171583/jpa-data-repositories-with-sqlresultsetmapping-and-native-queries – mvmn Sep 29 '19 at 22:16