Announcement Announcement Module
Collapse
No announcement yet.
Inheritence, polymorphism and Id problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Inheritence, polymorphism and Id problem

    I have abstract Place,
    City extends Place
    Region extends Place.

    Place
    ^^
    | |
    | City
    |
    Region

    Place:
    Code:
    @MappedSuperclass
    public abstract class Place {
    	
    	private String name;
    	
    	@Transient
    	public abstract void setId(Long id);
    	@Transient
    	public abstract Long getId();
    	
    	@Column
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    }
    City:
    Code:
    @Entity
    public class City extends Place{
    
    	private Long id;
    	
    	@Override
    	@Column
    	@Id
    	public Long getId() {
    		return id;
    	}
    	@Override
    	public void setId(Long id) {
    		this.id = id;
    	}
    }
    Region:
    Code:
    @Entity
    public class Region extends Place{
    
    	private Long id;
    	
    	@Override
            @Column
            @Id
    	public Long getId() {
    		return id;
    	}
    	@Override
    	public void setId(Long id) {
    		this.id = id;
    	}	
    	
    }
    In Hotel is relation to Place. It can be relation to City or Region. So far, I used hibernate @Any and it works.
    Code:
    @Entity
    public class Hotel {
    
    	private Long id;
    	private Place place;
    
    	@Any(metaColumn = @Column(name = "place_type"), fetch = FetchType.LAZY)
    	@AnyMetaDef(idType = "long", metaType = "string", metaValues = {
    			@MetaValue(value = "C", targetEntity = City.class),
    			@MetaValue(value = "R", targetEntity = Region.class) })
    	@Cascade({ org.hibernate.annotations.CascadeType.ALL })
    	@JoinColumn(name = "place_id")
    	public Place getPlace() {
    		return place;
    	}
    
    	public void setPlace(Place place) {
    		this.place = place;
    	}
            //get set id
    }
    But now, I can't use @Any or any other special hibernate feature. I have to use only JPA.
    So I tried to change it.

    Code:
    @Entity 
    @Inheritance
    @DiscriminatorColumn(name="place_type", discriminatorType = DiscriminatorType.STRING)
    public abstract class Place {
    
    	@Id protected Long id;
    	private String name;
    .......
    Code:
    @Entity
    @DiscriminatorValue("C")
    public class City extends Place{
    private Long id; //?
    .........
    Code:
    @Entity
    @DiscriminatorValue("R")
    public class Region extends Place{
    private Long id; //?
    .....
    Code:
    @Entity
    public class Hotel {
    
    	private Long id;
    	private Place place;
    	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    	@JoinColumn(name = "place_id")
    	public Place getPlace() {
    		return place;
    	}
    	
    	@Column
    	@Id
    	@SequenceGenerator(name="hotel_seq", sequenceName="hotel_seq")
    	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="hotel_seq")
    	public Long getId() {
    		return id;
    	}
    ........
    Exception:
    Code:
    java.lang.ClassCastException: org.hibernate.mapping.SingleTableSubclass cannot be cast to org.hibernate.mapping.RootClass
    I know that the problem is id field. I found that id should be placed only in superclass. But how to do it in my case? In each subclasses I have to indicate @SequenceGenerator name, city_seq and region_seq.

  • #2
    Hello

    Could you post the complete error stack trace?

    I know that the problem is id field. I found that id should be placed only in superclass
    Is mandatory

    But how to do it in my case? In each subclasses I have to indicate @SequenceGenerator name, city_seq and region_seq
    I dont know if overriding the variable in the subclasses could work, but if not, then your subclasses not share common annotation attributes, therefore here hierarchy fails due this, particularly due the you are working with the PK.

    I suggest post in Hibernate Forums

    Comment

    Working...
    X