Retrieve only the superclass from a class hierarchy

Posted by user1792724 on Stack Overflow See other posts from Stack Overflow or by user1792724
Published on 2012-11-01T22:37:31Z Indexed on 2012/11/05 23:00 UTC
Read the original article Hit count: 245

Filed under:
|
|
|

I have an scenario as the following:

@Entity
@Table(name = "ANIMAL")
@Inheritance(strategy = InheritanceType.JOINED)

public class Animal implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "S_ANIMAL")
    @SequenceGenerator(name = "S_ANIMAL", sequenceName = "S_ANIMAL", allocationSize = 1)
    public int getNumero() {
        return numero;
    }

    public void setNumero(int numero) {
        this.numero = numero;
    }
        .
        .
        .
}

and as the subclass:

@Entity
@Table(name = "DOG")
public class Dog extends Animal {

    private static final long serialVersionUID = -7341592543130659641L;
        .
        .
        .
}

I have a JPA Select statement like this:

SELECT a FROM Animal a;

I'm using Hibernate 3.3.1

As I can see the framework retrieves instances of Animal and also of Dog using a left outer join.

Is there a way to Select only the "part" Animal? I mean, the previous Select will get all the Animals, those that are only Animals but not Dogs and those that are Dogs.

I want them all, but in the case of Dogs I want to only retrieve the "Animal part" of them.

I found the @org.hibernate.annotations.Entity(polymorphism = PolymorphismType.EXPLICIT) but as I could see this only works if Animal isn't an @Entity.

Thanks a lot.

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate