简体   繁体   中英

Spring Boot Abstract Class beans service problem

All right I bean working in a simple project I previously worked in ths way and It worked fine, now it doesnt. Im using java 11, spring-boot-v2.7.0.

Here is the clases Im using

@Entity
@Table(name = "premios")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Prize implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_premio")
    private Integer id;
    @Column(name = "titulo")
    private String title;
    @Column(name = "descripcion")
    private String description;
    @Column(name = "fecha_creacion")
    private LocalDateTime createDate;
    @Column(name = "fecha_actualizacion")
    private LocalDateTime updateDate;
//Getters and setters 
///Extended class
@Entity
@Table(name = "carros")
@PrimaryKeyJoinColumn(name = "id_premio")
public class Car extends Prize{
    @Column(name = "marca")
    private String brand;
    @Column(name = "modelo")
    private String model;
    @Column(name = "anio")
    private String year;
///Extended Class
@Entity
@Table(name = "casas")
@PrimaryKeyJoinColumn(name = "id_premio")
public class House extends Prize{

    @Column(name = "metros_cuadrados")
    private double sqrMeters;
    @Column(name = "habitaciones")
    private double rooms;
    @Column(name = "banios")
    private double baths;
    @Column(name = "costo_real")
    private String actualCost;

This are my repositories:

@NoRepositoryBean
public interface PrizeRepository extends PagingAndSortingRepository<Prize,Integer> {
    Iterable<Prize> findByRaffle_id(Integer id);
}

@Repository("repositoryCar")
public interface CarRepository extends PrizeRepository{
}

@Repository("repositoryHouse")
public interface HouseRepository extends PrizeRepository{
}

My DAO's

public interface PrizeDAO extends GenericDAO<Prize> {
    Iterable<Prize> findByRaffle_id(Integer id);
}

public interface HouseDAO extends PrizeDAO{
}

public interface CarDAO extends PrizeDAO{
}

And the Implenetations:

public class PrizeDAOImpl extends GenericDAOImpl<Prize, PrizeRepository> implements PrizeDAO {

    
    public PrizeDAOImpl(PrizeRepository repository) {
        super(repository);
    }

    @Override
    public Iterable<Prize> findByRaffle_id(Integer id) {
        return repository.findByRaffle_id(id);
    }
}

@Service
public class HouseDAOImpl extends PrizeDAOImpl implements HouseDAO {

    public HouseDAOImpl(@Qualifier("repositoryHouse")PrizeRepository repository) {
        super(repository);
    }
}

@Service
public class CarDAOImpl extends PrizeDAOImpl implements CarDAO {

    public CarDAOImpl(@Qualifier("repositoryCar") PrizeRepository repository) {
        super(repository);
    }
}

The problem is that when ever I try to pull data from Houses or Cars, the response I get is the entire class "Prize", so I get both. I allready try anoteting the PrizeDAOImp.class with @Service and defind a Primary bean but the problem keeps coming.

Is there a better way to get the desire results of getting just the specified class or can I fix this current structure I have?

Fix your structure

Replaces:

@Entity
@Table(name = "premios")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Prize implements Serializable

By

@MappedSuperclass
public abstract class Prize implements Serializable

Remove the following annotation from the child classes

@PrimaryKeyJoinColumn(name = "id_premio")

For repositories use this:

@Repository("repositoryPrize")
public interface PrizeRepository extends PagingAndSortingRepository<Prize, Integer> {
}

@Repository("repositoryCar")
public interface CarRepository extends PrizeRepository{
}

@Repository("repositoryHouse")
public interface HouseRepository extends PrizeRepository{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM