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.
Is it possible to obtain the same result using only JPA, without @Any?
Inheritance types:
TABLE_PER_CLASS - I found that this strategy doesn't offer polymorphism, so it is not for my case.
JOINED - Trying to use this strategy, I have problem with id. Field with @Id must be declared in superclass. So I can't put id in subclasses? But, I use PostgreSQL and I have to indicate sequence generator name in subclasses.
Code:
@Id
@SequenceGenerator(name="city_seq", sequenceName="city_seq") // for City
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="city_seq")
public Long getId() {
return id;
}
and
region_seq for Region.
SINGLE_TABLE - the same problem as with joined strategy.
MAPPEDSUPERCLASS - Class with this annotatnion is not entity, so it can not be place as relation @ManyToOne
Code:
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "place_id")
public Place getPlace() {
return place;
}
Help please...