1

i have the problem i have a base abstract Entity Station with Inheritance TABLE_PER_CLASS and three child Tables StationCompany StationAnalysis StationVariant

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS )
public abstract class Station {

@Entity
public class StationCompany extends Station {

@ManyToOne(optional = false, fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
private Company company;

@Entity
public class StationAnalysis extends StationCompany {

@ManyToOne(optional = false, fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
private Analysis analysis;

@Entity
public class StationVariant extends StationAnalysis {

@ManyToOne(optional = false, fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
private Variant variant;

public interface IStationCompanyRepository extends JpaRepository<StationCompany, Long> {

@Service
public class StationService implements IStationService<StationCompany> {

    @Autowired
    IStationCompanyRepository stationCompanyRepository;

Then i search findAll on StationCompany, hibernate make a query with union select. i will search only for the StationCompany entrys.

select x from ( select a from StationCompany union select b from StationVariant union select c from StationAnalysis )
Monito
  • 41
  • 6
  • 1
    But StationAnalysis is a StationCompany, cannot blame hibernate for that, can you? – maress Jun 20 '14 at 11:42
  • i need this. StationAnalysis has a @ManyToOne to Company + Analysis and StationVariant to Company + Analysis + Variant Entity – Monito Jun 20 '14 at 12:02
  • Please elaborate exactly what you want – maress Jun 20 '14 at 12:11
  • I have three Services ( StationCompanyService / StationAnalysisService / StationVariantService ) I want search / create / save on the Table. But currently only the create + save are okay, the search is on all three tables, not only on one. select a from StationCompany JpaRepository loads all Table data, but i need only one Table data. – Monito Jun 20 '14 at 12:24

2 Answers2

3

the problem was the hibernate mapping. i think had a problem with the structure from Station to StationCompany to Station... I solve the problem with aditional Abstract @MappedSuperclass classes. After that the hibernate select the correct table and no more union selects.

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Station {

@MappedSuperclass
public abstract class AbstractStationCompany extends Station {
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @Fetch(FetchMode.SELECT)
    private Company company;

@Entity
public class StationCompany extends AbstractStationCompany {

@MappedSuperclass
public class AbstractStationAnalysis extends AbstractStationCompany {
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @Fetch(FetchMode.SELECT)
    private Analysis analysis;

@Entity
public class StationAnalysis extends AbstractStationAnalysis {

@Entity
public class StationVariant extends AbstractStationAnalysis {
Monito
  • 41
  • 6
0

Dont perform search all.

Create a NamedQuery on Station:

@NamedQuery(name="Station.findStationByType", 
            query="SELECT s FROM Station s WHERE TYPE(s) = :type")

Then in the main facade of your station hierarchy:

public class StationFacade<Station>{
  @PersistenceContext("contextName")
  private EntityManager em;

  public List<Station> findAll(Class<? extends Station> stationType){
    return em.createNamedQuery("Station.findStationByType")
             .setParameter("type", stationType.getSimpleName())
             .getResultList();
  }
}
maress
  • 3,533
  • 1
  • 19
  • 37
  • sry your solution has an error, the @NamedQuery does not known the TYPE(s). I found the solution after execute the Named Query with @NamedQuery(name = "StationCompany.findAll", query = "SELECT s FROM StationCompany s") this query also make a union select. in next post is the answer ( i cannot post the answer ) – Monito Jun 20 '14 at 16:12
  • the problem was the hibernate mapping. i think had a problem with the structure from Station to StationCompany to Station... I solve the problem with aditional Abstract @MappedSuperclass classes. (at)MappedSuperclass (at)Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class Station { (at)MappedSuperclass public abstract class AbstractStationCompany extends Station { (at)ManyToOne(optional = false, fetch = FetchType.EAGER) (at)Fetch(FetchMode.SELECT) private Company company; – Monito Jun 20 '14 at 16:14
  • (at)Entity public class StationCompany extends AbstractStationCompany { (at)MappedSuperclass public class AbstractStationAnalysis extends AbstractStationCompany { (at)ManyToOne(optional = false, fetch = FetchType.EAGER) (at)Fetch(FetchMode.SELECT) private Analysis analysis; (at)Entity public class StationAnalysis extends AbstractStationAnalysis { (at)Entity public class StationVariant extends AbstractStationAnalysis { – Monito Jun 20 '14 at 16:15